65.9K
CodeProject is changing. Read more.
Home

Subclassing menu without hooks

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.71/5 (24 votes)

Feb 26, 2003

2 min read

viewsIcon

79437

Subclassing menu without hooks

Subclassing menus without hooks

Hello,

I have read an article "A Revolutionary New Approach to Custom Drawn Menus - DanG". Yes, it seems to be a good article, but why should i use hooks? Can't i find a different way without hooks?

Window gets wm_INITMENUPOPUP, when menu window is already created. Lets try to find that window:

mh:=FindWindow(0,0,'#32768',nil);
If mh=0 then ; // process error

Wow, it seems - mh<>0.

Lets go further.

OldMenuProc:=SetWindowLong(mh,GWL_WNDPROC,Integer(@MenuProc));
If OldMenuProc=nil then ; // process error

There are no errors. Lets go to next step:

Function MenuProc(h:HWND;uMsg:UINT;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;
Begin
  Result:=1;
  Case uMsg of
    482:Begin // just for fun
        SetWindowLong(h,GWL_EXSTYLE,GetWindowLong(h,GWL_EXSTYLE) or WS_EX_LAYERED);
        SetLayeredWindowAttributes(h,0,127,LWA_ALPHA);
    End;
  End;
  If Result=1 then
    Result:=CallWindowProc(OldMenuProc,h,uMsg,wp,lp);
End;

At last, we succeeded. We have mentioned, that this part of code makes menu transparent.

Notes:

1. MenuProc doesnt get all commmon messages. We will not get wm_CREATE and other messages because menu is created already.

2. Messages which came to MenuProc ( menu is being called and killed almost at the same time )

19:23:13 : MenuProc: 482 wParam - 1 lParam - 0
19:23:13 : MenuProc: 124 wParam - -20 lParam - 1243400 // appears only of WS_EX_* style changes
19:23:13 : MenuProc: 125 wParam - -20 lParam - 1243400 // appears only of WS_EX_* style changes
19:23:13 : MenuProc: 70 wParam - 0 lParam - 1243264
19:23:13 : MenuProc: 131 wParam - 1 lParam - 1243220
19:23:13 : MenuProc: 71 wParam - 0 lParam - 1243264
19:23:13 : MenuProc: 5 wParam - 0 lParam - 3342476
19:23:13 : MenuProc: 70 wParam - 0 lParam - 1243628
19:23:13 : MenuProc: 133 wParam - 1 lParam - 0
19:23:13 : MenuProc: 20 wParam - -1392438729 lParam - 0
19:23:13 : MenuProc: 71 wParam - 0 lParam - 1243628
19:23:13 : MenuProc: 15 wParam - 0 lParam - 0
19:23:13 : MenuProc: 485 wParam - -1 lParam - 0
...
19:23:14 : MenuProc: 485 wParam - -1 lParam - 0
19:23:14 : MenuProc: 70 wParam - 0 lParam - 1243628
19:23:14 : MenuProc: 71 wParam - 0 lParam - 1243628
19:23:14 : MenuProc: 2 wParam - 0 lParam - 0
19:23:14 : MenuProc: 130 wParam - 0 lParam - 0

3. Menu should be called with TrackPopUpMenuEx(...,TPM_NOANIMATION,...); // it is used for turning of all effects, which may block your applied changes.

There aren't reviewed all possible variants (WM_NCCALCSIZE, WM_NCPAINT, WM_PAINT and so on), but they all will work properly.

If you want we could discuss with you about viewing all source code to you. You can find me at: 195.22.174.130 6667 #irca Kancleris.

Have a nice day ;)