Click here to Skip to main content
15,887,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to use JavaScript Functions in VC6 ?? Pin
CPallini18-Oct-09 20:28
mveCPallini18-Oct-09 20:28 
Questionhow anti spy wares work? Pin
Joseph Marzbani18-Oct-09 19:47
Joseph Marzbani18-Oct-09 19:47 
AnswerRe: how anti spy wares work? Pin
Adam Roderick J18-Oct-09 19:58
Adam Roderick J18-Oct-09 19:58 
GeneralRe: how anti spy wares work? Pin
Joseph Marzbani18-Oct-09 20:08
Joseph Marzbani18-Oct-09 20:08 
QuestionHow to End a Cdialog Pin
ForNow18-Oct-09 7:21
ForNow18-Oct-09 7:21 
AnswerRe: How to End a Cdialog Pin
«_Superman_»18-Oct-09 7:27
professional«_Superman_»18-Oct-09 7:27 
GeneralRe: How to End a Cdialog Pin
ForNow18-Oct-09 8:16
ForNow18-Oct-09 8:16 
QuestionToo-narrow popup menu using LoadMenuIndirect&TrackPopupMenu Pin
Lianqing18-Oct-09 5:12
Lianqing18-Oct-09 5:12 
Hi all,

I am writing a small win32 application to learn how to shown a popup menu in response to right mouse button click event. I use win32 SDK functions LoadMenuIndirect and TrackPopupMenu and borrow some menu-template-creation code from this link (
http://msdn.microsoft.com/en-us/library/90zx250s(VS.80).aspx).
The problem is that the popup menu is so narrow (looks like a narrow vertical stripe) that the first-level menu item text is invisible. Interestingly, the second-level menu items are shown properly. This issue can be reproduced with my code (PopMenu.c and PopMenu.mak) below. Any help will be greatly appreciated.

Leonard
2009-10-18

// PopMenu.c
#include <windows.h>

#define ID_APP_EXIT 30001
#define ID_EDIT_COPY 30002
#define ID_EDIT_PASTE 30003

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;
TCHAR szAppName[] = TEXT("PopMenu2");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if ( !RegisterClass(&wndclass) ) {
MessageBox(NULL, TEXT("This program requires Windows NT/2000/XP!"),
szAppName, MB_ICONERROR);
return 0;
}

hInst = hInstance;

hwnd = CreateWindow(szAppName, TEXT("Popup Menu Demonstration #2"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

while ( GetMessage(&msg, NULL, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

// This is a helper function for adding a menu item (either a popup
// or command item) to the specified menu template.
//
// MenuTemplate - pointer to a menu template
// MenuString - string for the menu item to be added
// MenuID - id for the command item. Its value is ignored if IsPopup is TRUE.
// IsPopup - TRUE for popup menu (or submenu); FALSE for command item
// LastItem - TRUE if MenuString is the last item for the popup; FALSE otherwise.
UINT AddMenuItem(LPVOID MenuTemplate, WCHAR* MenuString, WORD MenuID, BOOL IsPopup, BOOL LastItem)
{
MENUITEMTEMPLATE* mitem = (MENUITEMTEMPLATE*) MenuTemplate;

UINT bytes_used = 0;
if (IsPopup) { // for popup menu
if (LastItem)
mitem->mtOption = MF_POPUP | MF_END;
else
mitem->mtOption = MF_POPUP;
bytes_used += sizeof (mitem->mtOption);

mitem = (MENUITEMTEMPLATE*) ((BYTE*) MenuTemplate + bytes_used);
// a popup doesn't have mtID!!!

wcscpy((WCHAR*) mitem, MenuString);
bytes_used += sizeof (WCHAR) * (wcslen(MenuString) + 1); // include '\0'
}
else { // for command item
mitem->mtOption = LastItem ? MF_END : 0;
mitem->mtID = MenuID;
wcscpy(mitem->mtString, MenuString);
bytes_used += sizeof (mitem->mtOption ) + sizeof (mitem->mtID) +
sizeof (WCHAR) * (wcslen(MenuString) + 1); // include '\0'
}

return bytes_used;
}


// Create a menu handle from a menu template in memory.
HMENU LoadMenuTemplate()
{
// For simplicity, allocate 500 bytes from stack. May use
// GlobalAlloc() to allocate memory bytes from heap.
BYTE milist[500];
MENUITEMTEMPLATEHEADER* mheader;
int bytes_used;
HMENU hMenu;

memset(milist, 0, 500);

// Fill up the MENUITEMTEMPLATEHEADER structure.
mheader = (MENUITEMTEMPLATEHEADER*) milist;
mheader->versionNumber = 0;
mheader->offset = 0;

bytes_used = sizeof(MENUITEMTEMPLATEHEADER);

// Add the following menu items to menu bar:
// File Edit
// Exit Copy
// Paste
bytes_used += AddMenuItem(milist + bytes_used, L"&File", 0, TRUE, FALSE);
bytes_used += AddMenuItem(milist + bytes_used, L"E&xit", ID_APP_EXIT, FALSE, TRUE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Background", 1, FALSE, FALSE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Edit", 0, TRUE, TRUE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Copy", ID_EDIT_COPY, FALSE, FALSE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Paste", ID_EDIT_PASTE, FALSE, TRUE);

// Load resource from a menu template in memory.
hMenu = LoadMenuIndirect(milist);

return hMenu;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HMENU hMenu;
POINT point;

switch (message) {
case WM_CREATE:
hMenu = LoadMenuTemplate();
return 0;

case WM_RBUTTONUP:
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
ClientToScreen(hwnd, &point);
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

# PopMenu.mak
# Use this file as "nmake /f PopMenu.mak" in Visual Studio Command Prompt

CC = cl
LINK = link

CFLAGS = /O2 /Oi /GL /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_UNICODE" /D "UNICODE" /EHsc /MD /W3 /nologo /c
LDFLAGS = /INCREMENTAL:NO /NOLOGO /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /LTCG /DYNAMICBASE /NXCOMPAT

SYSTEM_LIB = kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

.c.obj:
$(CC) $(CFLAGS) $<

all: PopMenu.exe

PopMenu.exe: PopMenu.obj
$(LINK) /OUT:$@ $(LDFLAGS) $(SYSTEM_LIB) PopMenu.obj

clean:
del *.exe *.obj *.manifest
AnswerRe: Too-narrow popup menu using LoadMenuIndirect&TrackPopupMenu Pin
Code-o-mat18-Oct-09 9:45
Code-o-mat18-Oct-09 9:45 
GeneralRe: Too-narrow popup menu using LoadMenuIndirect&TrackPopupMenu Pin
Lianqing18-Oct-09 14:34
Lianqing18-Oct-09 14:34 
GeneralRe: Too-narrow popup menu using LoadMenuIndirect&TrackPopupMenu Pin
«_Superman_»18-Oct-09 17:53
professional«_Superman_»18-Oct-09 17:53 
Questionusing cout i get symbols instead address [Solved] Pin
Tadysas18-Oct-09 4:19
Tadysas18-Oct-09 4:19 
AnswerRe: using cout i get symbols instead address Pin
Cedric Moonen18-Oct-09 4:44
Cedric Moonen18-Oct-09 4:44 
AnswerRe: using cout i get symbols instead address Pin
Mircea Puiu18-Oct-09 4:49
Mircea Puiu18-Oct-09 4:49 
GeneralRe: using cout i get symbols instead address [solved] Pin
Tadysas18-Oct-09 7:36
Tadysas18-Oct-09 7:36 
Questionhow to release space of link nodes recursively? Pin
jianzhuhuai17-Oct-09 14:38
jianzhuhuai17-Oct-09 14:38 
AnswerRe: how to release space of link nodes recursively? Pin
Stuart Dootson17-Oct-09 15:10
professionalStuart Dootson17-Oct-09 15:10 
GeneralRe: how to release space of link nodes recursively? Pin
jianzhuhuai18-Oct-09 23:10
jianzhuhuai18-Oct-09 23:10 
GeneralRe: how to release space of link nodes recursively? Pin
Stuart Dootson19-Oct-09 0:32
professionalStuart Dootson19-Oct-09 0:32 
GeneralRe: how to release space of link nodes recursively? Pin
jianzhuhuai20-Oct-09 0:43
jianzhuhuai20-Oct-09 0:43 
QuestionHelp with a Function related with Circular Linked List Pin
Naumf17-Oct-09 12:30
Naumf17-Oct-09 12:30 
AnswerRe: Help with a Function related with Circular Linked List Pin
«_Superman_»17-Oct-09 13:08
professional«_Superman_»17-Oct-09 13:08 
GeneralRe: Help with a Function related with Circular Linked List Pin
Naumf18-Oct-09 3:00
Naumf18-Oct-09 3:00 
QuestionRe: Help with a Function related with Circular Linked List Pin
David Crow17-Oct-09 16:53
David Crow17-Oct-09 16:53 
AnswerRe: Help with a Function related with Circular Linked List Pin
Naumf18-Oct-09 3:04
Naumf18-Oct-09 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.