Thread: RMB Context Menu

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Hawaii
    Posts
    2

    RMB Context Menu

    Aloha,

    I'm having a devil of a time trying to do a simple RMB context menu - and I'm sure I'm misunderstanding something basic. Although the menu displays on RMB - the lowest menu item's text doesn't display - I just get vertical bars. But the 2nd level menu displays fine when hovering over the associated parent menu's vertical bar. A jpg of the situation is attached in case I'm not explaining myself clearly.

    In case it makes a difference - I'm using Windows Vista Home premium (32 bit).
    Compiled using Visual C++ 2008 Express

    Thanks in advance for any help in solving this problem.

    Aloha

    link to RMB Context menu problem


    Code:
    #include <windows.h>
    #include <commctrl.h>
    
    LRESULT CALLBACK mainWndProc	(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    void	ConstructMenu();
    
    const char	szAppName[]		= "NodeDiagram";
    
    HINSTANCE	hInstance;
    HWND		mainWindow;
    HMENU		hMenu;
    HMENU		hsmFile, hsmEdit, hsmMessage, hsmConnection, hsmMain;
    
    
    int WINAPI	WinMain	(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) {
    	WNDCLASSEX	wc;
    	MSG			msg;
    
    	wc.cbSize		= sizeof(wc);
    	wc.style		= 0;
    	wc.lpfnWndProc		= mainWndProc;
    	wc.cbClsExtra		= 0;
    	wc.cbWndExtra		= 0;
    	wc.hInstance		= hInst;
    	wc.hIcon		= LoadIcon (NULL, IDI_APPLICATION);
    	wc.hCursor		= LoadCursor (NULL, IDC_SIZEWE);
    	wc.hbrBackground	= (HBRUSH)(COLOR_3DFACE+1);;
    	wc.lpszMenuName		= 0;
    	wc.lpszClassName	= szAppName;
    	wc.hIconSm		= LoadIcon (NULL, IDI_APPLICATION);
    	RegisterClassEx(&wc);
    	hInstance = hInst;
    
    	ConstructMenu();
    	mainWindow = CreateWindowEx(
    				0,						// extended style (not needed)
    				szAppName,				// window class name
    				szAppName,				// window caption
    				WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,		// window style
    				CW_USEDEFAULT,			// initial x position
    				CW_USEDEFAULT,			// initial y position
    				CW_USEDEFAULT,			// initial x size
    				CW_USEDEFAULT,			// initial y size
    				NULL,					// parent window handle
    				NULL, 					// a menu handle here will put the menu on the window
    				hInst,					// program instance handle
    				NULL);					// creation parameters
    
    	ShowWindow(mainWindow, nShowCmd);
    
    	while(GetMessage(&msg, NULL,0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return 0;
    }
    
    LRESULT CALLBACK	mainWndProc	(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
    	 POINT			point;
    	switch(msg)	{
    		case WM_CREATE:
    			return 0;
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    		case WM_RBUTTONDOWN:
    			point.x = LOWORD (lParam) ;
    			point.y = HIWORD (lParam) ;
    			ClientToScreen (hwnd, &point) ;
    			TrackPopupMenu (hMenu, TPM_RIGHTBUTTON|TPM_LEFTALIGN|TPM_NONOTIFY|TPM_RETURNCMD, point.x, point.y, 0, hwnd, NULL) ;
    			return 0;
    		default:
    			break;
    	}
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    void				ConstructMenu() {
    	MENUITEMINFO mii;
    	int rc;
    	hMenu	= CreateMenu();
    
    	hsmMain			= CreatePopupMenu();
    	hsmFile			= CreatePopupMenu();
    	hsmEdit			= CreatePopupMenu();
    	hsmMessage		= CreatePopupMenu();
    	hsmConnection	= CreatePopupMenu();
    
    	memset(&mii, 0, sizeof(MENUITEMINFO));
    	mii.cbSize	= (UINT) sizeof(MENUITEMINFO);
    	mii.fMask	= MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU;
    	mii.fType	= MFT_STRING;
    	mii.hSubMenu= NULL;
    
    	//*** File ****************************************************************
    	mii.dwTypeData	= TEXT("New");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 101;
    	rc=InsertMenuItem(hsmFile, 0, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Open");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 102;
    	rc=InsertMenuItem(hsmFile, 1, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Save");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 103;
    	rc=InsertMenuItem(hsmFile, 2, TRUE, (LPCMENUITEMINFO) &mii);
    
    	//*** Edit *****************************************************************
    	mii.dwTypeData	= TEXT("Copy");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 201;
    	rc=InsertMenuItem(hsmEdit, 0, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Cut");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 202;
    	rc=InsertMenuItem(hsmEdit, 1, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Paste");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 203;
    	rc=InsertMenuItem(hsmEdit, 2, TRUE, (LPCMENUITEMINFO) &mii);
    
    	//*** Message **************************************************************
    	mii.dwTypeData	= TEXT("New...");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 301;
    	rc=InsertMenuItem(hsmMessage, 0, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Refresh");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 302;
    	rc=InsertMenuItem(hsmMessage, 1, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Copy to clipboard");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 303;
    	rc=InsertMenuItem(hsmMessage, 2, TRUE, (LPCMENUITEMINFO) &mii);
    
    	//*** Connection ***********************************************************
    	mii.dwTypeData	= TEXT("Connect");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 401;
    	rc=InsertMenuItem(hsmConnection, 0, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Disconnect");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 402;
    	rc=InsertMenuItem(hsmConnection, 1, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= "Settings";
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 403;
    	rc=InsertMenuItem(hsmConnection, 2, TRUE, (LPCMENUITEMINFO) &mii);
    
    	//*** Main Menu *************************************************************
    	mii.dwTypeData	= TEXT("File");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 900; //100;
    	mii.hSubMenu= hsmFile;
    	rc=InsertMenuItem(hMenu, 0, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Edit");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 901; //200;
    	mii.hSubMenu= hsmEdit;
    	rc=InsertMenuItem(hMenu, 1, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Message");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 902; //300;
    	mii.hSubMenu= hsmMessage;
    	rc=InsertMenuItem(hMenu, 2, TRUE, (LPCMENUITEMINFO) &mii);
    
    	mii.dwTypeData	= TEXT("Connection");
    	mii.cch		= (UINT) strlen(mii.dwTypeData)+1;
    	mii.wID		= (UINT) 903; //400;
    	mii.hSubMenu= hsmConnection;
    	rc=InsertMenuItem(hMenu, 3, TRUE, (LPCMENUITEMINFO) &mii);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    IIRC menu's 'position' is calced in a strange way (so you are inserting the popups in the wrong position).

    Zero is the first menu item across (File), then count the menu items below it (in this case 3 items).

    The next 'top' item (Edit) will be 4, then count the items below it (3) so the next top item will be 8, etc.

    Usually a right mouse button popup menu has an empty title and a list of sub items (your menu may not display the way you want it to even after the order is corrected).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Hawaii
    Posts
    2
    Thanks novacain,

    BTW, the non-display problem I was having was caused by using

    hMenu = CreateMenu();
    instead of
    hMenu = CreatePopupMenu();

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. Context Menu cursor problem
    By dWorkVan in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2003, 11:42 AM
  4. Double click for context menu?
    By mepaco in forum Windows Programming
    Replies: 1
    Last Post: 12-19-2002, 06:17 PM