Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C++
Article

Subclassing menu without hooks

Rate me:
Please Sign up or sign in to vote.
2.71/5 (24 votes)
17 Jun 20032 min read 78.2K   13   18
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 ;)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Damian Suess21-Jan-22 9:19
Damian Suess21-Jan-22 9:19 
GeneralOverride Menu class Pin
thucnh29-May-05 22:07
thucnh29-May-05 22:07 
GeneralAbout not using hooks Pin
.dan.g.26-Feb-03 12:30
professional.dan.g.26-Feb-03 12:30 
GeneralRe: About not using hooks Pin
Kancleris27-Feb-03 5:30
Kancleris27-Feb-03 5:30 
GeneralRe: About not using hooks Pin
Kancleris27-Feb-03 5:30
Kancleris27-Feb-03 5:30 
GeneralA few points Pin
Chopper26-Feb-03 0:50
Chopper26-Feb-03 0:50 
GeneralRe: A few points Pin
Ernest Laurentin26-Feb-03 4:01
Ernest Laurentin26-Feb-03 4:01 
GeneralRe: A few points Pin
Kancleris26-Feb-03 4:18
Kancleris26-Feb-03 4:18 
GeneralRe: A few points Pin
P. Ple Trawler26-Feb-03 17:14
sussP. Ple Trawler26-Feb-03 17:14 
GeneralRe: A few points Pin
snakeware2-Mar-03 21:51
snakeware2-Mar-03 21:51 
GeneralRe: A few points Pin
Anonymous18-Jun-03 6:58
Anonymous18-Jun-03 6:58 
GeneralRe: A few points Pin
Ernest Laurentin26-Feb-03 18:48
Ernest Laurentin26-Feb-03 18:48 
GeneralRe: A few points Pin
Simon Steele26-Feb-03 5:06
Simon Steele26-Feb-03 5:06 
Ernest Laurentin wrote:
Don't ask too much, he's a VB programmer!

How could you possibly know this?

The code's Delphi not VB.

Just because the article isn't formatted wonderfully doesn't imply that he's a VB programmer and that in turn wouldn't imply that he doesn't know anything.

Joel Spolsky is a very respected programmer and regularly programs in Visual Basic - read his website (joelonsoftware.com[^]).

Thanks to the author for the article. It could be better formatted - but as it's your first submission I don't think you deserve a slating. Nice to see some Delphi code, it's been a while!

</rant>

--
Simon Steele
Programmers Notepad - http://www.pnotepad.org/
GeneralRe: A few points Pin
Kancleris26-Feb-03 4:17
Kancleris26-Feb-03 4:17 
GeneralRe: A few points Pin
Furan26-Feb-03 9:53
Furan26-Feb-03 9:53 
GeneralRe: A few points Pin
Roman Nurik26-Feb-03 15:42
Roman Nurik26-Feb-03 15:42 
GeneralRe: A few points Pin
Anonymous1-Dec-03 5:37
Anonymous1-Dec-03 5:37 
GeneralRe: A few points Pin
YoSilver28-Jan-04 4:27
YoSilver28-Jan-04 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.