delphi – 鼠标拖出边界后,控件如何接收鼠标事件?

delphi – 鼠标拖出边界后,控件如何接收鼠标事件?,第1张

概述我正在创建一个自定义控件,可以识别拖动鼠标的时间,特别是使用消息WM_LBUTTONDOWN,WM_LBUTTONUP和WM_MOUSEMOVE.当鼠标按下时,我捕获控件上的位置,然后当鼠标移动时,如果鼠标左键按下,我会做更多处理(在起点和终点之间计算). 问题是,我希望鼠标不受控制,甚至超出窗体,但是当鼠标离开控件时,它不再捕获鼠标事件.有没有一种方法可以处理WM_MOUSEMOVE和WM_LB 我正在创建一个自定义控件,可以识别拖动鼠标的时间,特别是使用消息WM_LbuttonDOWN,WM_LbuttonUP和WM_MOUSEMOVE.当鼠标按下时,我捕获控件上的位置,然后当鼠标移动时,如果鼠标左键按下,我会做更多处理(在起点和终点之间计算).

问题是,我希望鼠标不受控制,甚至超出窗体,但是当鼠标离开控件时,它不再捕获鼠标事件.有没有一种方法可以处理WM_MOUSEMOVE和WM_LbuttonUP消息,而鼠标不在控件之上?

解决方法 Releasecapture适用于Wincontrols,另一种方式可能是Mousehook.那只是一个演示….

unit MouseHook;// 2012 by Thomas Wassermanninterfaceuses  windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs;type  TForm3 = class(TForm)    procedure FormDestroy(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    { Private-Deklarationen }  public    { Public-Deklarationen }  end;var  Form3: TForm3;implementationvar  HookHandle: Cardinal;Type  tagMsllHOOKSTRUCT = record    POINT: TPoint;    mouseData: DWORD;    flags: DWORD;    time: DWORD;    DWExtraInfo: DWORD;  end;  TMsllHOOKSTRUCT = tagMsllHOOKSTRUCT;  PMsllHOOKSTRUCT = ^TMsllHOOKSTRUCT;{$R *.dfm}function LowLevelMouseProc(nCode: Integer; wParam: wParam; lParam: lParam): LRESulT; stdcall;var Delta:Smallint;begin  if (nCode >= 0) then  begin    Form3.Caption := Format('X: %d  Y: %d ',[PMsllHOOKSTRUCT(lParam)^.Point.X,PMsllHOOKSTRUCT(lParam)^.Point.Y]);    if wParam = WM_LbuttonDOWN then Form3.Caption := Form3.Caption + ' LD';    if wParam = WM_LbuttonUP then Form3.Caption := Form3.Caption + ' LU';    if wParam = WM_RbuttonDOWN then Form3.Caption := Form3.Caption + ' RD';    if wParam = WM_RbuttonUP then Form3.Caption := Form3.Caption + ' RU';    if wParam =  WM_MOUSEMOVE then Form3.Caption := Form3.Caption + ' Move';    Delta := PMsllHOOKSTRUCT(lParam)^.mouseData shr 16;    if wParam =  WM_MOUSEWHEEL then          begin            Form3.Caption := Form3.Caption + ' Wheel ' ;            if Delta=120 then Form3.Caption := Form3.Caption + ' KliCK'            else if Delta > 0  then Form3.Caption := Form3.Caption +' UP'            else if Delta < 0  then Form3.Caption := Form3.Caption +' DOWN'          end;    if wParam =  WM_MOUSEHWHEEL then          begin            Form3.Caption := Form3.Caption + ' HWheel';            if Delta=120 then Form3.Caption := Form3.Caption + ' KliCK'            else if Delta > 0  then Form3.Caption := Form3.Caption +' UP'            else if Delta < 0  then Form3.Caption := Form3.Caption +' DOWN'          end;     Form3.Caption := Form3.Caption +' >> '+ IntToStr(Delta)  end;  Result := CallNextHookEx(HookHandle,nCode,wParam,lParam);end;function InstallMouseHook: Boolean;begin  Result := False;  if HookHandle = 0 then  begin    HookHandle := SetwindowsHookEx(WH_MOUSE_LL,@LowLevelMouseProc,hInstance,0);    Result := HookHandle <> 0;  end;end;procedure TForm3.FormCreate(Sender: TObject);begin  InstallMouseHook;end;procedure TForm3.FormDestroy(Sender: TObject);begin  if HookHandle <> 0 then    UnhookwindowsHookEx(HookHandle);end;end.
总结

以上是内存溢出为你收集整理的delphi – 鼠标拖出边界后,控件如何接收鼠标事件?全部内容,希望文章能够帮你解决delphi – 鼠标拖出边界后,控件如何接收鼠标事件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/langs/1270857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存