开发者

Empty Win32 Popup Menu

开发者 https://www.devze.com 2022-12-22 07:30 出处:网络
I\'m trying to create a dynamic popup menu within my application, the generation code I use is something like that :

I'm trying to create a dynamic popup menu within my application, the generation code I use is something like that :

    HMENU   menu;
    POINT   pt;

    menu = CreatePopupMenu();

    SetForegroundWindow( receivingWindow );
    GetCursorPos( &pt );
    int i = 19;
    AppendMenu( menu, MF_STRING, i++, _TEXT("meh meh") );
    AppendMenu( menu, MF_STRING, i++, _TEXT("testo") );
    AppendMenu( menu, MF_STRING, i++, _TEXT("foobar foobar") );
    TrackPopupMenuEx( menu
                    , 0
                    , pt.x, pt.y
                    , receivingWindow
                    , NULL );

    DestroyMenu( menu );

_TEXT is used to ensure text is in Unicode and receivingWindow is a Layered window created before and working well.

When calling TrackPopupMenuEx the menu is displayed with the good size and at the good position, but absolutely no text appear in the popup menu. Did someone got an idea why, and how to fix this problem?

EDIT: more information regarding my environment :

EDIT2: I've tested the same on Windows XP x86, and it works like a charm, and after further test, the menu is well displayed in Windows 7 x64 with the classic look.


Make sure ::DefWindowProc() is called for messages that aren't handled otherwise. I had a similar issue with TrackPopupMenu() where the menu would popup with the correct size and allow items to be selected, but there was no text on the items because the code was ignoring some messages without calling ::DefWindowProc().


If your compiler is not set to compile for unicode (i.e #ifndef UNICODE) then winuser.h will map AppendMenu to AppendMenuA which is a non-unicode version and would interpret your strings as multi-byte. Perhaps this explains your issue? You could explicitly call AppendMenuW() (the unicode version) to check if this is your issue.


I believe the problem is that the function TrackPopUpMenuEx doesn't return immediately; thus after it initiates (and presumably chooses it's size and position) but before it first displays you are destroying the menu.

As I understand it you need to destroy the menu after your window has received the command message from a menu selection. Alternately use the TPM_RETURNCMD flag in TrackPopUpMenuEx as this forces trackpopupex to only return after a menu item has been selected (as opposed to immediately).


I found a workaround for this problem. Instead of using my main window (receivingWindow), I'm using a message only window to receive the event. For a reason that I don't understand, the text is displayed normally this way.


Try:

call    GetSubMenu,mnu,0
call    TrackPopupMenu,eax,TPM_CENTERALIGN or TPM_BOTTOMALIGN,cposx,cposy,0,MainhWnd,0


In my case i created the menu with "CreateMenu" but i must use "CreatePopupMenu" to use it with TrackPopupMenuEx.

If the menu is created in the resource editor check if the flag is set.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号