求一个鼠标点击的消息截获代码
我目前的工程是这样的,就是想做一个查询框,类似ComboBox但是需要多选,所以我自己组合了一个小控件,就是TEdit和TCheckListBox组合而成组合框,这样可以实现多选。
目前碰到的问题是这样的,我想实现,当鼠标在控件TCheckListBox外单击时,在消息中实现让控件TCheckListBox隐藏。
无奈,本人新手,也没写过消息函数,所以求一段代码。
要求,尽量用Hook技术,有简要的代码,但是不能用DLL库,不需要对系统消息进行处理,只对进程消息处理就可以了。由于本人对API函数很陌生,所以调用函数的时候最好能对各参数作个简要的说明!
谢谢大家!
[解决办法]
MouseEnter
MouseLeave
[解决办法]
重载WndProc,截获点击消息,判断消息的hwnd是否是TCheckListBox的句柄,如果不是,隐藏
[解决办法]
可以在Application.OnMessage事件里头处理。
- Delphi(Pascal) code
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, CheckLst;type TCheckListBox = class(CheckLst.TCheckListBox) private FOldMessageEvent: TMessageEvent; FMessageEventAssigned: Boolean; procedure DoMessage(var Msg: TMsg; var Handled: Boolean); public procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED; end; TForm1 = class(TForm) CheckListBox1: TCheckListBox; Edit1: TEdit; procedure Edit1Click(Sender: TObject); procedure FormClick(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1Click(Sender: TObject);begin CheckListBox1.Show;end;procedure TForm1.FormClick(Sender: TObject);begin Text := Classname + DateTimeToStr(now)end;{ TCheckListBox }procedure TCheckListBox.CMShowingChanged(var Message: TMessage);begin inherited; if not FMessageEventAssigned then if not (csDesigning in ComponentState) then begin if Showing then begin FMessageEventAssigned := True; FOldMessageEvent := Application.OnMessage; Application.OnMessage := DoMessage; end; end;end;procedure TCheckListBox.DoMessage(var Msg: TMsg; var Handled: Boolean);begin if Assigned(FOldMessageEvent) then FOldMessageEvent(Msg, Handled); Handled := False; if (Msg.message = WM_LBUTTONDOWN) and ((Msg.hwnd <> Self.Handle) and (Msg.hwnd <> Form1.Edit1.Handle)) then Hideend;end.
[解决办法]
用低级键盘鼠标钩子吧~
[解决办法]
不需要用Hook