delphi 如何实现三击鼠标事件

delphi 如何实现三击鼠标事件,第1张

以下代码实现在ContentEdit(文本编辑器)中三击鼠标选中一整行的功能。

先定义4个全局变量

FIsThreeClicking: Boolean //三击事件(是否)进行中

FCurMouseX: Integer

FCurMouseY: Integer //三击时的坐标校验

FStartTime: Double //用于实现鼠标三击事件

事件FormCreate 中初始化变量

procedure TFormMDIChild.FormCreate(Sender: TObject)

begin

FIsThreeClicking := False

FStartTime := 0

......

end

控件的KeyDown事件中

procedure TFormMDIChild.ContentEditKeyDown(Sender: TObjectvar Key: Word

Shift: TShiftState)

begin

if FIsThreeClicking then //正处于三击事件时,屏蔽掉按键

begin

Key := 0

Exit

end

......

end

控件的MouseUp事件中

procedure TFormMDIChild.ContentEditMouseUp(Sender: TObject

Button: TMouseButtonShift: TShiftStateX, Y: Integer)

var

pointBB, pointBE: TPoint

bSelectFromTop: Boolean

begin

if FIsThreeClicking then

begin

FIsThreeClicking := False //三击事件结束

FStartTime := 0

with ContentEdit do

begin

//恢复为正常选择模式,并选中指定块

if SelectionMode = smLine then

begin

SelectionMode := smNormal

//if SelAvail then

begin

if (BlockBegin.y >BlockEnd.y)

or ((BlockBegin.y = BlockEnd.y)

and (BlockBegin.x >BlockEnd.x)) then

begin

pointBB := BlockEnd

pointBE := BlockBegin

end //如果块是从下往上选择

else begin

pointBB := BlockBegin

pointBE := BlockEnd

end //否则正常

if CaretY = pointBE.y then

bSelectFromTop := True

else

bSelectFromTop := False

pointBB.x := 1

if pointBE.y <Lines.Count then

begin

pointBE.x := 1

pointBE.y := pointBE.y + 1

end

else begin

pointBE.x := Length(Lines[pointBE.y-1]) + 1

pointBE.y := pointBE.y

end

BlockBegin := pointBB

BlockEnd := pointBE

if bSelectFromTop then

CaretXY := pointBE

else

CaretXY := pointBB

end

end //三击后恢复选择模式

if [eoNoCaret] * Options = [eoNoCaret] then

begin

Options := Options - [eoNoCaret]

end //三击后恢复光标(不是鼠标)

end

end

end

procedure TFormMDIChild.ContentEditMouseDown(Sender: TObject

Button: TMouseButtonShift: TShiftStateX, Y: Integer)

begin

if Button = mbLeft then

begin

with ContentEdit do

begin

if (GetTickCount - FStartTime <GetDoubleClickTime)

{如果当前时间 - 双击时的时间 <系统的双击间隔时间——即当前动作为三击}

and (PixelsToRowColumn(Point(FCurMouseX, FCurMouseY)).x = PixelsToRowColumn(Point(X, Y)).x)

and (PixelsToRowColumn(Point(FCurMouseX, FCurMouseY)).y = PixelsToRowColumn(Point(X, Y)).y)

{当前坐标 = 双击时的坐标}

then begin

Options := Options - [eoDragDropEditing] //不允许拖动(MouseDown 事件中处理中用到)

PostMessage(Self.Handle, WM_THREECLICK, 0, 0) //发送消息到消息队列

end //因为 MouseDown 后 SynEdit 要进行一些处理,而这些处理与三击事件的处理

//代码在顺序上有冲突,故通过消息队列来安排事件处理顺序

else begin

FCurMouseX := X

FCurMouseY := Y //保存当前 Mouse X,Y

end

end

end

end

procedure WmContentEditThreeClick(var Message: TMessage)message WM_THREECLICK //处理鼠标三击事件

procedure TFormMDIChild.WmContentEditThreeClick(var Message: TMessage)

{三击时选中全行}

var

pointCaretXY: TPoint

begin

FIsThreeClicking := True //三击事件进行中

with ContentEdit do

begin

Options := Options + [eoDragDropEditing] //恢复拖动

SelectionMode := smLine //改为双击选中行模式

pointCaretXY.x := 1

pointCaretXY.y := CaretXY.y

BlockBegin := pointCaretXY

pointCaretXY.x := Length(LineText) + 1

BlockEnd := pointCaretXY //选中全行

if [eoNoCaret] * Options <>[eoNoCaret] then

begin

Options := Options + [eoNoCaret] //隐藏光标

end

end

end

最简单的办法可以在控件的onContextPopup事件中写。

例如可以在button1的onContextPopup事件中写

Button1Click(nil)

这样在button1上点右键的时候就实现了点击左键的效果

复杂一点的可以利用消息机制,获取消息


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11446354.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存