Click to See Complete Forum and Search --> : Creating a MenuHandle


Martin
September 24th, 1999, 03:47 AM
Hi all,

I would like to set a checked mark to the menuitem that was selected by the user.
The CheckMenuItem seems to be the correct function,CheckMenuItem(HMENU hmenu, UINT
uIDCheckItem, UINT uCheck), but how do I create handle to a menu that is defined in the resources?
Or what else is needed to get the checked mark showing up?

Thanks for your help.
Martin

mtighe
September 24th, 1999, 04:44 AM
getmenu will return a pointer to a CMenu.
CMenu has a CheckMenuItem member function.

mtighe,

mtighe@pobox.com

mtighe
September 24th, 1999, 04:48 AM
Also,
if the menu is not created yet you can use
LoadMenu, setmenucheck and then setmenu.

mtighe,

mtighe@pobox.com

Martin
September 24th, 1999, 06:09 AM
Hi,
Could you please help me with this code? It doesn't do anything... and I don't have a setmenucheck function in my help library...


HMENU hMenu;

hMenu = LoadMenu(hInstance, "IDC_F4MEM");

case IDM_START:
{
CheckMenuItem(hMenu,IDM_START,MF_CHECKED);
}
break;




When I select Start from the menu, nothing happens regarding the
checked-mark.

Thank you for your help.
Martin

mtighe
September 24th, 1999, 06:16 AM
First, is your application win32 only or mfc.
this will tell me if you have access to the
CMenu class. if not, no big deal. i will do
up a example in win32.

mtighe,

mtighe@pobox.com

Martin
September 24th, 1999, 08:27 AM
It's Win32 only. No MFC and no CMENU.

Thanks!
Martin

mtighe
September 24th, 1999, 04:08 PM
The code could have been much shorter. but i thought maybe you would want to see the whole
thing. if you have any questions on the code
let me know.



// main.cpp

#define STRICT // strict type checking
#include <windows.h> // standard header
#include <windowsx.h> // message crackers
#include "resource.h" // resource definitions

//////////////////////////////////////////////////////////////////////
//
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
static bool bChecked;

HMENU hm = GetMenu(hwnd);
CheckMenuItem(hm,IDM_CHECKTEST,(bChecked ? MF_UNCHECKED : MF_CHECKED));
bChecked = !bChecked;
}

/////////////////////////////////////////////////////////////////////
//
void OnDestroy(HWND hWnd)
{
PostQuitMessage(0);
}

/////////////////////////////////////////////////////////////////////
//
void OnPaint(HWND hWnd)
{
HDC hDC;
PAINTSTRUCT ps;

hDC = BeginPaint(hWnd,&ps);
Rectangle(hDC,50,50,100,100);
EndPaint(hWnd,&ps);
}

/////////////////////////////////////////////////////////////////////
//
long CALLBACK MainWndProc(
HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hWnd,WM_COMMAND,OnCommand);
HANDLE_MSG(hWnd,WM_DESTROY,OnDestroy);
HANDLE_MSG(hWnd,WM_PAINT,OnPaint);
}

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

/////////////////////////////////////////////////////////////////////
//
BOOL InitInstance(HINSTANCE hInst,int nCmdShow)
{
HWND hAppWnd = 0;

hAppWnd = CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("Martin"),TEXT("Martin"),
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,0,0,hInst,0);

if (!hAppWnd)
return FALSE;

ShowWindow(hAppWnd,nCmdShow);
UpdateWindow(hAppWnd);

return TRUE;
}

////////////////////////////////////////////////////////////////////
//
BOOL InitApplication(HINSTANCE hInst)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc = MainWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(0,IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcex.lpszMenuName = TEXT("AppMenu");
wcex.lpszClassName = TEXT("Martin");
wcex.hIconSm = 0;

if (0 == RegisterClassEx(&wcex))
return FALSE;

return TRUE;
}

/////////////////////////////////////////////////////////////////
//
int WINAPI WinMain(
HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine,int nCmdShow)
{
InitApplication(hInst);
InitInstance(hInst,nCmdShow);

MSG msg;

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

return msg.wParam;
}



HTH,
mtighe

mtighe@pobox.com