• Jon © (24.11.07 01:50) [0]
    Why can I not receive messages for menu's?
    I want to trap and process WM_NC* messages for PMenu.

    unit PopupEx;

    interface

    uses
     Windows, KOL, Messages;

    type
     PPopupMenuEx = ^TPopupMenuEx;
     TPopupMenuEx = object(TControl)
     public
       destructor Destroy; virtual;
     end;

    function NewPopupMenuEx(AParent: PControl; MaxCmdReserve: DWORD;
     const Template: array of PKOLChar; aOnMenuItem: TOnMenuItem): PPopupMenuEx;

    implementation

    function MenuWndProc(Sender: PControl; var Msg: TMsg; var Rslt: Integer): Boolean;
    begin
     Result := False;
    end;

    function NewPopupMenuEx;
    begin
     Result := PPopupMenuEx(NewMenu(AParent,MaxCmdReserve,Template,aOnMenuItem));
    //  Result.AttachProc(@MenuWndProc);       // <------ ERROR!!
    end;

    destructor TPopupMenuEx.Destroy;
    begin
     inherited;
    end;

    end.



    If I uncomment the line with AttachProc, a runtime error is generated.
  • Vladimir Kladov © (24.11.07 20:11) [1]
    Self is absent. Event must be a method, not a regular procedure, so if you use regular procedure in place of the method of the object, you must add (the first) parameter, which is passed for objects' methods hidden.

    A bit strange idea to inherit new successor from the TControl but name it as it would be a menu.
  • Jon © (24.11.07 21:42) [2]
    Thanks Vladimir.

    > Self is absent.
    I did not think that AttachProc needs self. I know that MakeMethod requires it.
    Code changed to:
    function MenuWndProc(Dummy: Pointer; Sender: PControl; var Msg: TMsg; var Rslt: Integer): Boolean;


    but still the same error.


    > A bit strange idea to inherit new successor from the TControl
    > but name it as it would be a menu.

    If I inherit from PMenu then I am unable to use AttachProc.

    I can find no other way to receive WM_NC* messages for a menu.
  • Vladimir Kladov © (24.11.07 22:19) [3]
    Oh, beg oardon! It is my error, I forget it (far away i used it).

    But your error seems in inheriting from incorrect domain. Your TPopupMenuEx is just declared as a successor of TControl, but really it is created calling NewMenu, so TMenu object is created (and even not your TPopupMenuEx). Typecast does not make it a TPopupMenuEx, it just instructs the compiler to think that it is of TPopupMenuEx type derived from TControl, but physically it is still a TMenu object. (Hm, yor destructor also will never be called, too).
  • Jon © (24.11.07 23:17) [4]
    It looks like I still have a lot to learn. Thank you for your words of wisdom.
    I studied examples but obviously did not understand - I shall look again.
    Do you recommend a better way to receive required messages?
    I can find no OnMessage event for PMenu.
  • Vladimir Kladov © (25.11.07 08:56) [5]
    Menu is not a window in a common sense. It should not receive any messages itself. But the owning window can receive some messages like WM_ENTERMENULOOP, WM_EXITMENULOOP, WM_COMMAND, WM_MENUCOMMAND... All this is defined in MSDN very detailed.
  • Jon © (26.11.07 02:51) [6]
    Understood. I solved it using a hook. Your information helped steer me in the right direction. Thank you.
Есть новые Нет новых   [134431   +10][b:0][p:0.002]