Click here to Skip to main content
14,267,388 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 showing RMB Context Menu problem[^]

#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);
}
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try:
hMenu	= CreatePopupMenu();

Instead of:
hMenu	= CreateMenu();

CreateMenu is used to create the menu you can see below the title bar. A right click menu is a popup menu, so it should be created as such.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100