1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| type
THackPList = class(TPopupList);
TForm1 = class(TForm)
Label2: TLabel;
Label1: TLabel;
PopupMenu1: TPopupMenu;
MenuItem1: TMenuItem;
Label3: TLabel;
procedure MenuItem1Click(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
private
{ Déclarations privées }
MonMenu:TPopupMenu;
SelMenuItem: TMenuItem;
menuRect: TRect;
OldPopWndProc: TWndMethod;
procedure WndProcPopMenu(var Mess1: TMessage);
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
GMouseHook: Cardinal;
implementation
{$R *.dfm}
function HookFunc(Code, wParam: Integer; var MouseStrut: TMOUSEHOOKSTRUCT): Integer; stdcall;
begin
Result := 1;
if Code = HC_ACTION then
begin
if (wParam = WM_LBUTTONDOWN) then
if PtInRect(Form1.menuRect, MouseStrut.pt) then Exit;
if (wParam = WM_LBUTTONUP) then
if PtInRect(Form1.menuRect, MouseStrut.pt) then
begin
// on verifie que la souri est dans le menu
Form1.Label1.Caption := IntToStr(Form1.menuRect.Top);
if Assigned(Form1.SelMenuItem) then
Form1.SelMenuItem.OnClick(Form1.SelMenuItem);
Exit;
end;
end;
Result := CallNextHookEx(GMouseHook, Code, wParam, Integer(@MouseStrut));
// si tu ne fait pas CallNextHookEx le message de la souri est bloqué
end;
procedure TForm1.WndProcPopMenu(var Mess1: TMessage);
var
i, Item: Integer;
begin
//j'appelle l'ancien gestionnaire de message
OldPopWndProc(Mess1);
if Mess1.Msg = WM_EXITMENULOOP then
begin
//quand le menu se ferme j'enleve le hook
UnhookWindowsHookEx(GMouseHook);
Label1.Caption := 'UnHook the Hook';
end;
if Mess1.Msg = WM_MENUSELECT then
begin
if HIWORD(Mess1.wParam) and MF_POPUP <> 0 then Exit;
Label1.Caption := '';
for i := 0 to THackPList(PopupList).Count - 1 do
begin
Item := LOWORD(Mess1.wParam);
SelMenuItem := TPopupMenu(THackPList(PopupList).Items[i]).FindItem(Item, fkCommand);
if SelMenuItem <> nil then
begin
GetMenuItemRect(0, SelMenuItem.Parent.Handle, SelMenuItem.MenuIndex, menuRect);
Label2.Caption := SelMenuItem.Name+' '+ IntToStr(menuRect.Top);
end;
end;
end;
end;
procedure TForm1.MenuItem1Click(Sender: TObject);
begin
Label3.Caption := TMenuItem(Sender).Name;
end;
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
OldPopWndProc := THackPList(PopupList).WndProc;
SetWindowLong(PopupList.Window, GWL_WNDPROC, Longint(MakeObjectInstance(WndProcPopMenu)));
GMouseHook := SetWindowsHookEx(WH_MOUSE, @HookFunc, 0, GetCurrentThreadID);
end; |
Partager