Thread: Dim Menus

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    73

    Dim Menus

    How do you dim a menu inside the program, not in the resource script.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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);
    
    //....
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ok so I got bored. Here is a complete working program that attaches a menu created by using the above code.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    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?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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");
    Woop?

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Let's see if you mean enabling and disabling menu items then you would use EnableMenuItem(). Here is a code excerpt:

    Code:
    EnableMenuItem(hMenu, IDM_FILE, MF_ENABLED); //enable item
    
    EnableMenuItem(hMenu, IDM_EDIT, MF_GRAYED); //disable item
    Hope that helps. Happy Coding!

    [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]
    Last edited by andyhunter; 12-28-2004 at 05:52 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    thanks

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    One more question, how do you make a seperator?

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
    Happy Coding!

    [edit]

    So for example in the sample program I provided if we did this:
    Code:
    AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_START, TEXT("Start"));
    AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
    AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_STOP, TEXT("Stop"));
    It would put a separator between Start and Stop

    [/edit]
    Last edited by andyhunter; 12-28-2004 at 08:42 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    Thanks again.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    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);
    }

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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 :

    Code:
    case IDDN_OK: 
                          hMenu = GetMenu(hWnd)
                          //.....
    Otherwise if you attach your entire project I will try to help you troubleshoot it to see what is happening.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    LRESULT CALLBACK DlgProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    	HMENU hMenu;
    	hMenu = GetMenu(hWnd);
    You're getting the menu for the dialog box rather than the main application window.

    Typically, you would use the main application window as the parent window when you create dialog boxes:
    Code:
    	DialogBox(hInstance, MAKEINTRESOURCE(IDD_HELP), hWnd, DlgProc);
    and then you can retrieve a handle to the main application window from a dialog procedure using GetParent:
    Code:
    	hMenu = GetMenu(GetParent(hWnd));
    Of course, you could also use a global variable or a variety of other methods.

    --
    Donate to quake disaster relief effort

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    See I knew someone else would see it. Good catch anonytmouse.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 menus and resources help
    By firestorm in forum Windows Programming
    Replies: 24
    Last Post: 04-12-2005, 01:23 PM
  2. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  3. Menu's
    By Benzakhar in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2004, 10:13 PM
  4. Bit a tough one
    By crag2804 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-10-2002, 07:40 PM
  5. adding menus at runtime
    By bennyandthejets in forum Windows Programming
    Replies: 3
    Last Post: 11-22-2002, 05:07 AM