How do you dim a menu inside the program, not in the resource script.
Printable View
How do you dim a menu inside the program, not in the resource script.
Well if you really want to do it the hard way you are going to want to look at the CreateMenu() function, and the AppendMenu() function in the windows sdk. Here is a short code excerpt:
Code:
//......
HMENU hMenu;
hMenu = CreateMenu() //return handle to new menu
AppendMenu(hMenu, MF_STRING, IDM_FILE, "&File");
AppendMenu(hMenu, MF_STRING, IDM_EDIT, "&Edit");
SetMenu(hwnd, hMenu);
//....
Ok so I got bored. Here is a complete working program that attaches a menu created by using the above code.
Ok, so I created my menu without a resource, that was easy enough. Now how do I go about dimming an item? And how do you make a dropdown from file?
What do you mean by dimming an item? If you mean thats is grayed just use this
Code:AppendMenu(hMenu, MF_STRING | MF_GRAYED, IDM_EDIT, "&Edit");
Let's see if you mean enabling and disabling menu items then you would use EnableMenuItem(). Here is a code excerpt:
Hope that helps. Happy Coding!Code:
EnableMenuItem(hMenu, IDM_FILE, MF_ENABLED); //enable item
EnableMenuItem(hMenu, IDM_EDIT, MF_GRAYED); //disable item
[edit]
Here is another sample program. This one dynamically creates the menu, and then enables/disables menu items based on user selection.
This program has 2 dropdown menus File and Help
[/edit]
thanks
One more question, how do you make a seperator?
Happy Coding!Code:
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
[edit]
So for example in the sample program I provided if we did this:
It would put a separator between Start and StopCode:AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_START, TEXT("Start"));
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_STOP, TEXT("Stop"));
[/edit]
Thanks again.
I'm making battleship, and these menus just wont cooperate. Here is my code. I've commented when I need to switch the menus from disabled back to enabled. For some reason it will disable, but not enable itself when I click IDDN_OK from a dialog (resource).
Code:#include <iostream>
#include <windows.h>
#include "resource.h"
using namespace std;
int boats[5];
char boat1pos[2];
char boat2pos[2];
char boat3pos[2];
char boat4pos[2];
char boat5pos[2];
char computerBoard[12][12];
char playerBoard[12][12];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK DlgProc (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInstance;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASSEX wndclass;
HMENU hMenu, hMenuPopup;
wndclass.cbSize = sizeof (wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszClassName = "Battleship";
wndclass.hIcon = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
wndclass.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
wndclass.lpszMenuName = NULL;
RegisterClassEx (&wndclass);
hWnd = CreateWindow("Battleship","Battleship",WS_OVERLAPPEDWINDOW,100,100,500,500,NULL,NULL,hInstance,NULL);
hMenu = CreateMenu();
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup, MF_STRING, IDM_NEW, TEXT("&New Game"));
AppendMenu(hMenuPopup, MF_STRING, IDM_LOAD, TEXT("&Load Game"));
AppendMenu(hMenuPopup, MF_STRING, IDM_CLOSE, TEXT("&Close"));
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenuPopup, MF_STRING, IDM_SAVE, TEXT("&Save"));
AppendMenu(hMenuPopup, MF_STRING, IDM_SAVEAS, TEXT("&Save As..."));
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenuPopup, MF_STRING, IDM_LOAD, TEXT("&Statistics"));
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenuPopup, MF_STRING, IDM_QUIT, TEXT("&Quit"));
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("&File"));
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup, MF_STRING, IDM_RULES, TEXT("&Rules"));
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenuPopup, MF_STRING, IDM_ABOUT, TEXT("&About"));
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("&Help"));
SetMenu(hWnd, hMenu);
EnableMenuItem(hMenu, IDM_CLOSE, MF_GRAYED);
EnableMenuItem(hMenu, IDM_SAVE, MF_GRAYED);
EnableMenuItem(hMenu, IDM_SAVEAS, MF_GRAYED);
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
SetFocus(hWnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
UnregisterClass("Battleship",hInstance);
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch(iMsg)
{
case WM_CREATE:
{
int x, y;
for(x=0; x<12; x++)
{
for(y=0; y<12; y++)
{
computerBoard[x][y] = ' ';
playerBoard[x][y] = ' ';
}
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_COMMAND:
{
if(LOWORD(wParam) == IDM_QUIT)
{
PostQuitMessage(0);
break;
}
if(LOWORD(wParam) == IDM_RULES)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_HELP), NULL, (DLGPROC)DlgProc);
break;
}
if(LOWORD(wParam) == IDM_ABOUT)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUT), NULL, (DLGPROC)DlgProc);
break;
}
if(LOWORD(wParam) == IDM_NEW)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEW), NULL, (DLGPROC)DlgProc);
break;
}
break;
}
}
return DefWindowProc (hWnd, iMsg, wParam, lParam);
}
LRESULT CALLBACK DlgProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu;
hMenu = GetMenu(hWnd);
switch(iMsg)
{
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDDH_OK:
{
EndDialog(hWnd, FALSE);
break;
}
case IDDA_OK:
{
EndDialog(hWnd, FALSE);
break;
}
case IDDN_CANCEL:
{
EndDialog(hWnd, FALSE);
break;
}
case IDDN_OK:
{
GetDlgItemText(hWnd, IDDN_BT1EDIT, boat1pos, 2);
GetDlgItemText(hWnd, IDDN_BT2EDIT, boat2pos, 2);
GetDlgItemText(hWnd, IDDN_BT3EDIT, boat3pos, 2);
GetDlgItemText(hWnd, IDDN_BT4EDIT, boat4pos, 2);
GetDlgItemText(hWnd, IDDN_BT5EDIT, boat5pos, 2);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------> NEED HELP HERE -------------------------- NEED HELP HERE <---
----------------------------------------------------------------------------------------------------------------------------------------------------------------------- EnableMenuItem(hMenu, IDM_CLOSE, MF_ENABLED);
EnableMenuItem(hMenu, IDM_SAVE, MF_ENABLED);
EnableMenuItem(hMenu, IDM_SAVEAS, MF_ENABLED);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------> NEED HELP HERE ---------------------------- NEED HELP HERE <---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EndDialog(hWnd, FALSE);
break;
}
case IDDN_BT1L:
{
boats[0] = 1;
break;
}
case IDDN_BT1R:
{
boats[0] = 2;
break;
}
case IDDN_BT1U:
{
boats[0] = 3;
break;
}
case IDDN_BT1D:
{
boats[0] = 4;
break;
}
case IDDN_BT2L:
{
boats[1] = 1;
break;
}
case IDDN_BT2R:
{
boats[1] = 2;
break;
}
case IDDN_BT2U:
{
boats[1] = 3;
break;
}
case IDDN_BT2D:
{
boats[1] = 4;
break;
}
case IDDN_BT3L:
{
boats[3] = 1;
break;
}
case IDDN_BT3R:
{
boats[3] = 2;
break;
}
case IDDN_BT3U:
{
boats[3] = 3;
break;
}
case IDDN_BT3D:
{
boats[3] = 4;
break;
}
}
break;
}
case WM_CLOSE:
case WM_DESTROY:
case WM_QUIT:
{
EndDialog(hWnd, FALSE);
break;
}
break;
}
return DefWindowProc (hWnd, iMsg, wParam, lParam);
}
I am afriad right off the bat I do not see anything, maybe somebody else might? Have you tried moving your initialization of hmenu into the specific case, aka :
Otherwise if you attach your entire project I will try to help you troubleshoot it to see what is happening.Code:
case IDDN_OK:
hMenu = GetMenu(hWnd)
//.....
You're getting the menu for the dialog box rather than the main application window.Code:LRESULT CALLBACK DlgProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu;
hMenu = GetMenu(hWnd);
Typically, you would use the main application window as the parent window when you create dialog boxes:
and then you can retrieve a handle to the main application window from a dialog procedure using GetParent:Code:DialogBox(hInstance, MAKEINTRESOURCE(IDD_HELP), hWnd, DlgProc);
Of course, you could also use a global variable or a variety of other methods.Code:hMenu = GetMenu(GetParent(hWnd));
--
Donate to quake disaster relief effort
See I knew someone else would see it. Good catch anonytmouse.
Thank you.