Pop up menues

This is a discussion on Pop up menues within the Windows Programming forums, part of the Platform Specific Boards category; How do you create a pop-up menu? (those who usually appear when you right-click). Is it the same kind of ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Pop up menues

    How do you create a pop-up menu? (those who usually appear when you right-click).
    Is it the same kind of menu that you use under the caption-border?

    <edit>
    Another question:
    I have a dialog with one text-edit and two buttons. I'm using DialogBoxParam to create it. The first time it is created, the text-edit is selected (which is exactly what I want).
    But when I close it and later create another one, it is not selected.
    Is there a function (or message to send) that selects a control in a dialog?
    I've tried ShowWindow() and some other, but they didn't work.
    </edit>
    Last edited by Magos; 09-02-2002 at 01:34 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Use TrackPopupMenu() for the popup.....manipulate WM_INITDIALOG for the focus.....ala;


    ::for msvc++6, but you can edit the resource script and header to get rid of any non crucial stuff::

    Code:
    #include <windows.h>
    #include "resource.h"
    
    BOOL CALLBACK DialogProc2(HWND hwndDlg,UINT uMsg,WPARAM wParam,
    		 LPARAM lParam){
    
    	HWND hEdit;
    
    	switch(uMsg){
    	case WM_INITDIALOG:
    		hEdit = GetDlgItem(hwndDlg,IDC_EDIT2);//get handle of 2nd edit
    		SetFocus(hEdit);//set its focus
    		return 0;//return 0 to overide system setting default focus
    	case WM_CLOSE:
    		EndDialog(hwndDlg,0);
    		return 1;
    	}
    
    
    return 0;
    }
    
    
    BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,
    		 LPARAM lParam){
    	
    	static HMENU hMenu;
    	POINT point;
    
    	switch(uMsg){
    	case WM_INITDIALOG:
    		hMenu = LoadMenu(GetModuleHandle(NULL),//Load Menu
    			MAKEINTRESOURCE(IDR_MENU1));
    		hMenu = GetSubMenu(hMenu,0);//Get Sub Menu
    		return 1;
    	case WM_RBUTTONDOWN:
    		point.x = LOWORD(lParam);//Get cursor Pos
    		point.y = HIWORD(lParam);
    		TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_RIGHTBUTTON,
    			point.x,point.y,NULL,hwndDlg,NULL);//Create popup
    		return 1;
    	case WM_CLOSE:
    		DestroyMenu(hMenu);
    		EndDialog(hwndDlg,0);
    		return 1;
    	case WM_COMMAND:
    		if(LOWORD(wParam == WM_POPUPCLICKED)){
    			MessageBox(hwndDlg,"ClickMe!!",NULL,MB_OK);//was popup clicked
    			return 1;
    		}
    		else if(LOWORD(wParam == IDOK)){
    			DialogBoxParam(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG2),
    				hwndDlg,DialogProc2,NULL);//Launch 2nd dialog
    			return 1;
    		}
    	}
    
    
    return 0;
    }
    
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int){
    
    
    	DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_DIALOG1),
    		HWND_DESKTOP,DialogProc,NULL);
    
    	
    
    return 0;
    }
    Now resource.h

    Code:
    //{{NO_DEPENDENCIES}}
    // Microsoft Developer Studio generated include file.
    // Used by Script1.rc
    //
    #define IDD_DIALOG1                     101
    #define IDR_MENU1                       102
    #define IDD_DIALOG2                     103
    #define IDC_EDIT1                       1000
    #define IDC_BUTTON1                     1001
    #define IDC_EDIT2                       1002
    #define WM_POPUPCLICKED                 40002
    #define IDC_STATIC                      -1
    
    // Next default values for new objects
    // 
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE        104
    #define _APS_NEXT_COMMAND_VALUE         40003
    #define _APS_NEXT_CONTROL_VALUE         1003
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif
    script.rc

    Code:
    //Microsoft Developer Studio generated resource script.
    //
    #include "resource.h"
    
    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #include "afxres.h"
    
    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS
    
    /////////////////////////////////////////////////////////////////////////////
    // English (U.K.) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG)
    #ifdef _WIN32
    LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
    #pragma code_page(1252)
    #endif //_WIN32
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Dialog
    //
    
    IDD_DIALOG1 DIALOG DISCARDABLE  0, 0, 186, 95
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Dialog"
    FONT 8, "MS Sans Serif"
    BEGIN
        DEFPUSHBUTTON   "Run 2nd Dialog",IDOK,51,40,72,14
    END
    
    IDD_DIALOG2 DIALOG DISCARDABLE  0, 0, 186, 93
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Dialog2 - Second edit selected"
    FONT 8, "MS Sans Serif"
    BEGIN
        EDITTEXT        IDC_EDIT1,39,34,131,14,ES_AUTOHSCROLL
        DEFPUSHBUTTON   "Dummy Button",IDC_BUTTON1,40,7,96,16
        EDITTEXT        IDC_EDIT2,40,57,131,15,ES_AUTOHSCROLL
        LTEXT           "2nd",IDC_STATIC,14,60,18,11
        LTEXT           "1st",IDC_STATIC,15,36,22,11
    END
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // DESIGNINFO
    //
    
    #ifdef APSTUDIO_INVOKED
    GUIDELINES DESIGNINFO DISCARDABLE 
    BEGIN
        IDD_DIALOG1, DIALOG
        BEGIN
            LEFTMARGIN, 7
            RIGHTMARGIN, 179
            TOPMARGIN, 7
            BOTTOMMARGIN, 88
        END
    
        IDD_DIALOG2, DIALOG
        BEGIN
            LEFTMARGIN, 7
            RIGHTMARGIN, 179
            TOPMARGIN, 7
            BOTTOMMARGIN, 86
        END
    END
    #endif    // APSTUDIO_INVOKED
    
    
    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //
    
    1 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "resource.h\0"
    END
    
    2 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "#include ""afxres.h""\r\n"
        "\0"
    END
    
    3 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "\r\n"
        "\0"
    END
    
    #endif    // APSTUDIO_INVOKED
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Menu
    //
    
    IDR_MENU1 MENU DISCARDABLE 
    BEGIN
        POPUP "DEFAULT-Not Seen"
        BEGIN
            MENUITEM "ClickMe",                     WM_POPUPCLICKED
        END
    END
    
    #endif    // English (U.K.) resources
    /////////////////////////////////////////////////////////////////////////////
    
    
    
    #ifndef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //
    
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // not APSTUDIO_INVOKED

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Thanks!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I have some problems with the popup menu:

    1) When the popup is created, it is located relative to the upper left of the screen, not the upper left of my window.
    How can I correct this? Can I somehow recieve the position of the window so I can calculate the right point? Or is there some easier way?

    2) The popup can only be created when right clicking directly on the window, not any of it's controls. I want it to appear only when right clicking on a specific list I have. How can I do this?

    <edit>
    I tried using the WM_NOTIFYPARENT message, but it is never sent (I checked using a MessageBox directly under it).
    But I have the LBS_NOTIFY flag set on the control...
    </edit>

    3) This is not so important, but can you disable that you can select a menu-option with the right mouse button?
    Last edited by Magos; 09-07-2002 at 05:52 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    1)
    Code:
    POINT position;
    GetCursorPos(&pos);
    TrackPopupMenu(.... position.x, position.y, .....);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    2)

    Wherever the WM_RBUTTONDOWN is handled is where it will show up. You handled it in the main window apparently. Your list has to handle it. If you're using the ListBox common control there is a message you'll have to handle that comes from the control but I don't know what it is off hand
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This is the popup-menu part of my main message handler.
    Is this wrong?
    Code:
    case WM_PARENTNOTIFY:
       switch(LOWORD(wParam))
       {
          case WM_RBUTTONDOWN:
             GetCursorPos(&TempPosition);
             TrackPopupMenu(ListPopupMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, TempPosition.x, TempPosition.y, NULL, Handle, NULL);
             break;
       }
       break;
    <edit>
    I can catch the LBN_DBLCLK message just fine,
    but there doesn't seem to be a message for a
    right click...
    </edit>
    Last edited by Magos; 09-07-2002 at 11:01 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Found it.
    Its WM_CONTEXTMENU.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. push n pop
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2008, 09:41 AM
  2. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 01:08 AM
  3. Stack using push and pop
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 11-03-2003, 03:54 PM
  4. pop up menu problem
    By bigtamscot in forum Windows Programming
    Replies: 5
    Last Post: 06-10-2003, 01:46 AM
  5. pop function
    By Troll_King in forum C Programming
    Replies: 12
    Last Post: 10-15-2001, 04:45 AM

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