Einzelnen Beitrag anzeigen

Benutzerbild von p0ke
p0ke

Registriert seit: 21. Dez 2003
Ort: Osnabrück
121 Beiträge
 
Turbo Delphi für Win32
 
#3

Re: mbLeft und mbRight bei Menü- und Popup-Items unterscheid

  Alt 26. Dez 2003, 10:59
Hi,

habe mal bei google gesucht und folgendes gefunden, vieleicht hilft dir das weiter:



Delphi-Quellcode:
private
    { Private declarations }
    hMenuUp: Integer;
    procedure EnterMenu(var Msg: TMessage); message WM_ENTERMENULOOP;
    procedure ExitMenu(var Msg: TMessage); message WM_EXITMENULOOP;
    procedure InitMenu(var Msg: TMessage); message WM_INITMENUPOPUP;


var
  Form1: TForm1;
  hMHook: Integer;



implementation

function HookFunc(Code, wParam: Integer; var MouseStrut: TMOUSEHOOKSTRUCT): Integer; stdcall;
var
MenuItem: cardinal;
begin
Result:=CallNextHookEx(hMHook, Code, wParam, Integer(@MouseStrut));
if Code < 0 then Exit;
{this test for the Right button Up and if the hMenuUp if the first sub-menu in the main menu}
if (Code = HC_ACTION) and (wParam = WM_RBUTTONUP) and
    (Form1.hMenuUp = Form1.MainMenu1.Items[0].Handle) then
      begin
   {get the menu item of the of the sub-menu clicked on}
      MenuItem := Cardinal(windows.MenuItemFromPoint(Form1.Handle, Form1.MainMenu1.Items[0].Handle, MouseStrut.pt));
      if MenuItem = 0 then // if Top Menu Item then write some numbers on the Label for a Right click on that Item
        Form1.Label1.Caption := 'WOW '+IntToStr(MouseStrut.pt.x)+' '+IntToStr(MouseStrut.pt.y)+' '+IntToStr(MenuItem) else
        Form1.Label1.Caption := 'Not Item zero';

      end;
end;


procedure TForm1.InitMenu(var Msg: TMessage);
var
MRect: TRect;
begin
Msg.Result := 0;
hMenuUp := Msg.WParam;
{get the handle of the sub-menu that is showing in the hMenuUp}
end;

procedure TForm1.EnterMenu(var Msg: TMessage);
begin
{start the hook when the menu is shown}
Msg.Result := 0;
if (Msg.WParam = 0) and (hMHook = 0) then
hMHook := SetWindowsHookEx(WH_MOUSE, @HookFunc, hInstance, 0);
end;

procedure TForm1.ExitMenu(var Msg: TMessage);
begin
{kill hook on menu close}
Msg.Result := 0;
if hMHook <> 0 then
if UnhookWindowsHookEx(hMHook) then
hMHook := 0;
end;
Zum testen einfach ein Menu auf dein Form und ein Label1. Beim Rechtsklick auf des Menuitem wird dann WOW und die Mouseposition angezeigt, wenn alles klappt.
Bei mir hats funktioniert. Ich hoffe ich hab dich richtig verstanden und es ist das was du brauchst.

Gruss p0ke
René
There are only 10 types of people in the world: Those who understand binary, and those who don't.

Mein Projekt bei DP: ScreenCaptureV2
  Mit Zitat antworten Zitat