TrackPopupMenuEx

This is a discussion on TrackPopupMenuEx within the Windows Programming forums, part of the Platform Specific Boards category; I needed a context menu for my program and found the function TrackPopupMenuEx on MSDN. However this function doesn't return ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16

    TrackPopupMenuEx

    I needed a context menu for my program and found the function TrackPopupMenuEx on MSDN. However this function doesn't return until the menu is closed, and I can't put all my other code on hold until it closes. So, does there exist some other function which returns immediately and just sends the result to the window's message loop?

    I tried creating a new thread which just called TrackPopupMenuEx, but for some reason the menu never appeared even though no error was returned.

    I also have another question; how can I get a handle to a child window with a specific identifier?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    TrackPopupMenuEx can do both.
    If you specify the TPM_RETURNCMD flag, it will return only when the user clicks something. If you do not, it sends a notification when a user clicks on something.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    I tried not specifying TPM_RETURNCMD however it still didn't return before the menu was closed. The only difference I could see was that it now always returned 1, rather than the identifier of the selected option.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    OK, perhaps I'm wrong (no experience with TrackPopupMenu with notifications).
    However, you can still launch another thread and pop up the menu inside it to avoid blocking the rest of the application.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    I tried the following code for creating the menu in a thread. However nothing appears. TrackPopupMenuEx returns 0, GetLastError returns 0.

    Code:
    typedef struct popupMenuStruct
    {
        HMENU *menu;
        HWND *wnd;
        int x, y;
    };
    
    DWORD WINAPI DisplayPopupMenu( void *v )
    {
        popupMenuStruct *PMS = (popupMenuStruct*) v;
        int ret = TrackPopupMenuEx( *PMS->menu,
                                    TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD |
                                    TPM_LEFTBUTTON | TPM_HORIZONTAL,
                                    PMS->x,
                                    PMS->y,
                                    *PMS->wnd,
                                    NULL );
        
        printf( "TrackPopupMenuEx: %d\n", ret );
        printf( "error: %d\n", GetLastError() );
        return 0;
    }
    
    ...
    
    popupMenuStruct *popupInfo = new popupMenuStruct;
    popupInfo->menu = &userInfoMenu;
    popupInfo->wnd = &WindowHandle;
    popupInfo->x = 100;
    popupInfo->y = 100;
    CreateThread( NULL, 0, &DisplayPopupMenu, (void*)popupInfo, 0, NULL );

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    For what kind of use the context menu is intended ?

    Is it for a TrayIcon or just for the window ... ?
    Hm, anyway ..., try this in your WinMain() instead of GetMessage() ...

    Code:
        while (TRUE) {
            
            if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
                if (msg.message == WM_QUIT)
                    break;
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            } else {
                // Your code here ...
            }
        }
        return msg.wParam;
    http://msdn.microsoft.com/en-us/libr...43(VS.85).aspx


    Greetz

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    It's intended for the window. It will popup when an on screen button is clicked.

    And I'm already using PeekMessage.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Waterbottle
    So, does there exist some other function which returns immediately and just sends the result to the window's message loop?
    WM_INITMENU and WM_INITMENUPOPUP may be of interest.
    Quote Originally Posted by Waterbottle
    how can I get a handle to a child window with a specific identifier?
    GetDlgItem.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21