Thread: Custom rightclick menu on a control?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Custom rightclick menu on a control?

    I haven't seen any examples of doing this on MSDN. Of course, that doesn't mean it's not there. lol

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Context Menu Strip I believe is what you should be searching for.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    I found some context/shortcut menu examples on MSDN, but I can't get anything to actually display a damn menu after loading from the resource.
    Last edited by Viper187; 10-06-2008 at 07:21 AM.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Post your codes/tell problem?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Custom controls and resources don't play well together unless you know how to do raw resource management.

    For an ordinary right-click menu there should be many tutorials.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    I haven't seen any in plain C/Win32

    Quote Originally Posted by valaris View Post
    Post your codes/tell problem?
    I got ........ed and deleted what I had. I did tell the problem. I compiled something nearly identical to the goddamn example and no menu appears. I used WM_RBUTTONUP on a subclassed listview control. I'm also curious if there's any way to force an entire menu to display somewhere like across the top of a tab or listview. I tried just including a menu on the dialog used for a tab, but it didn't appear.
    Last edited by Viper187; 10-06-2008 at 10:35 AM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Look at context menus. The platform SDK used to come with some very nice tutorials... Though m$ is really skimping down on what is being shipped with the platform SDKs nowadays. Either way, microsoft has ample code showing both how to make a standard one as well as a nifty office looking one.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Hi,

    sometimes (often) the M$ samples don't work, cause there is some stuff deficiency.
    And also often the code contains serious errors.
    Maybe they drink too much wine or smoke bad pot, I don't know ...

    However ..., here's an example following up on your TabDlg issue ...

    main.cpp
    Code:
    #include "stdafx.h"
    #include "resource.h"
    
    HINSTANCE hInst;
    TCHAR     szAppName[] = TEXT ("MyApp") ;
    
    bool InitTabCtrl (HWND hTabWnd) {
    
    	TCITEM tie;
    
    	tie.mask    = TCIF_TEXT | TCIF_IMAGE;
    	tie.iImage  = -1;
    	tie.pszText = "1st";
    
    	TabCtrl_InsertItem (hTabWnd, TabCtrl_GetItemCount(hTabWnd), &tie);
    
    	tie.pszText = "2nd";
    
    	TabCtrl_InsertItem (hTabWnd, TabCtrl_GetItemCount(hTabWnd), &tie);
    
    	tie.pszText = "3rd";
    
    	TabCtrl_InsertItem (hTabWnd, TabCtrl_GetItemCount(hTabWnd), &tie);
    
    	tie.pszText = "4th";
    
    	TabCtrl_InsertItem (hTabWnd, TabCtrl_GetItemCount(hTabWnd), &tie);
    
    	return true;
    }
    
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	static HWND  hwndTab;
    	static HMENU hMenu;
    	POINT        pt;
    
        switch(uMsg)
        {
            case WM_INITDIALOG:
    
    			hwndTab = GetDlgItem (hwndDlg, IDC_TAB1);
    			InitTabCtrl (hwndTab);
    
    			hMenu = LoadMenu (hInst, szAppName) ;
    			hMenu = GetSubMenu (hMenu, 0) ;
    			/*
                 * TODO: Add code to initialize the dialog.
                 */
                return TRUE;
    
    		case WM_RBUTTONUP:
    
    			pt.x = LOWORD (lParam);
    			pt.y = HIWORD (lParam);
    			ClientToScreen (hwndDlg, &pt);
    
    			TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt.x, pt.y,
    							0, hwndDlg, NULL);
    			return TRUE;
    
            case WM_CLOSE:
                EndDialog(hwndDlg, 0);
                return TRUE;
    
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    /*
                     * TODO: Add more control ID's, when needed.
                     */
                    case IDC_BTN_QUIT:
                        EndDialog(hwndDlg, 0);
                        return TRUE;
    
                    case IDC_BTN_TEST:
                        MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
                        return TRUE;
                        
    				case IDM_FILE_NEW:
    				case IDM_FILE_OPEN:
    				case IDM_FILE_SAVE:
    				case IDM_FILE_SAVE_AS:
    				case IDM_EDIT_UNDO:
    				case IDM_EDIT_CUT:
    				case IDM_EDIT_COPY:
    				case IDM_EDIT_PASTE:
    				case IDM_EDIT_CLEAR:
    					MessageBeep (0);
                        return TRUE;
                }
        }
    
        return FALSE;
    }
    
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        hInst = hInstance;
    
        INITCOMMONCONTROLSEX iccx;
    
        iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
        iccx.dwICC  = ICC_TAB_CLASSES | ICC_STANDARD_CLASSES;
    
        InitCommonControlsEx (&iccx);
    
        // The user interface is a modal dialog box
        return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, DialogProc);
    }
    resource.h
    Code:
    #ifndef IDC_STATIC
    #define IDC_STATIC (-1)
    #endif
    
    #define DLG_MAIN                                101
    #define IDC_BTN_TEST                            1001
    #define IDC_BTN_QUIT                            1002
    #define IDC_TAB1                                1003
    
    #define IDM_FILE_NEW                    40001
    #define IDM_FILE_OPEN                   40002
    #define IDM_FILE_SAVE                   40003
    #define IDM_FILE_SAVE_AS                40004
    #define IDM_APP_EXIT                    40005
    #define IDM_EDIT_UNDO                   40006
    #define IDM_EDIT_CUT                    40007
    #define IDM_EDIT_COPY                   40008
    #define IDM_EDIT_PASTE                  40009
    #define IDM_EDIT_CLEAR                  40010
    #define IDM_HELP_HELP                   40016
    #define IDM_APP_HELP                    40016
    #define IDM_APP_ABOUT                   40017
    rcfile.rc
    Code:
    // Generated by ResEdit 1.4.3
    // Copyright (C) 2006-2008
    // http://www.resedit.net
    
    #include "resource.h"
    #include <windows.h>
    
    
    //
    // Dialog resources
    //
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    DLG_MAIN DIALOGEX 6, 5, 194, 106
    STYLE DS_3DLOOK | DS_CENTER | DS_SETFONT | WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_GROUP | WS_THICKFRAME | WS_SYSMENU
    CAPTION "Code::Blocks Template Dialog App"
    FONT 8, "MS Sans Serif", 0, 0, 1
    BEGIN
        PUSHBUTTON      "&Test", IDC_BTN_TEST, 138, 5, 46, 15
        PUSHBUTTON      "&Quit", IDC_BTN_QUIT, 138, 29, 46, 15
        CONTROL         "", IDC_TAB1, WC_TABCONTROL, 0, 4, 4, 125, 97
    END
    
    //
    // Menu
    //
    
    MYAPP MENU DISCARDABLE
    BEGIN
        POPUP "MyMenu"
        BEGIN
           POPUP "&Datei"
           BEGIN
               MENUITEM "&Neu",						IDM_FILE_NEW
               MENUITEM "&&#214;ffnen",					IDM_FILE_OPEN
               MENUITEM "&Speichern",				IDM_FILE_SAVE
               MENUITEM "Speichern &unter",			IDM_FILE_SAVE_AS
               MENUITEM SEPARATOR
               MENUITEM "B&eenden",					IDM_APP_EXIT
           END
           POPUP "&Bearbeiten"
           BEGIN
               MENUITEM "&Widerrufen",				IDM_EDIT_UNDO
               MENUITEM SEPARATOR
               MENUITEM "A&usschneiden",			IDM_EDIT_CUT
               MENUITEM "&Kopieren",				IDM_EDIT_COPY
    		   MENUITEM "E&inf&#252;gen",				IDM_EDIT_PASTE
               MENUITEM "&L&#246;schen",					IDM_EDIT_CLEAR
           END
           POPUP "&Hilfe"
           BEGIN
               MENUITEM "&Hilfe...",				IDM_APP_HELP
               MENUITEM "I&nfo &#252;ber MyApp...",		IDM_APP_ABOUT
           END
        END
    END
    
    
    //
    // Manifest resources
    //
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    1                  RT_MANIFEST    ".\\manifest.xml"


    Greetz
    Greenhorn

    p.s. I can seriously recommend you this book from C. Petzold ...
    http://www.charlespetzold.com/pw5/
    There is NO cent wasted, believe me.
    Last edited by Greenhorn__; 10-06-2008 at 03:15 PM.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I can't argue about the M$ code being horrid at times, but the sample source files that they ship with the platform SDK all compile.... granted some with warnings... But they compile. Now the MSDN docs on the other hand. That is always a gamble.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Greenhorn to the rescue again. I think I got it. I'd still be more interested in forcing a whole menu bar to appear where I want it though. I don't want it on the main app, but on a tab itself. There's a dialog resource loaded for each tab. I tried setting the dialog in the resource file to use the menu but I haven't got the menu to appear. I don't know if it needs loaded by itself or if it's because of the dialog being discardable/etc. Last time I used a menu bar with a dialog, it was apparently loaded with the dialog, because I didn't do anything to load it and it worked.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you make a pseudo-context menu you can make it appear where ever you feel like. I had to do that in a project where I wanted the context menu to have shadows (yep. That sounds like a moronic reason to go through extra work, but I thought it would look spiffy).

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Yes, master is right ..., combine a Toolbar/Rebar and the Popup menu to create an User Defiened Menubar ...

    Many applications do this.


    Greetz
    Greenhorn

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are obviously not a rookie, Greenhorn__. You have a misleading name I like it.

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Wait. What about loading a menu for the main dialog after it's created? Like destroy the menu that's there and set a different one each time the selected tab changes. I tried slapping context menus on a header control, btw. Is there a way to force a context menu to be a certain width? If it doesn't take up the whole width of the header item, it doesn't look right.
    Last edited by Viper187; 10-07-2008 at 06:40 AM.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Hmm, I'm not sure what you wanna have as result, I'm a little bit confused, so help me to understand what you wanna do.

    You have a main window and several dialog boxes with a Tab control in the dialog box ?
    Or do you want to have the Tab controls inside the main window, like normal controls ?

    Now you want to have a menubar inside the main window that changes its content for every Tab control ?

    Could you post a pic of your current application or attach the source ?

    It's easier for us to help you if we know what you wanna get in the end, isn't it !?


    Gretz
    Greenhorn

    p.s. Thanks for the flowers, master, but I believe I am a rookie
    Last edited by Greenhorn__; 10-07-2008 at 12:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM