Thread: menu items check

  1. #16
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    then again... what the hell...

    i just compiled a very short version of the program... and guess what... it worked! the problem was that i called the CheckMenuItem from WM_CREATE, but once i called it from WM_COMMAND it worked fine
    why is that, could it be because the menu wasn't created yet

    here's the code:
    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    static char gszClassName[] = "test";
    static HINSTANCE ghInstance = NULL;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPSTR lpCmdLine, int nCmdShow)
     {
        WNDCLASSEX WndClass;
        HWND hwnd;
        MSG Msg;
        HACCEL hFkeys;
    
        ghInstance = hInstance;
    
        WndClass.cbSize = sizeof(WNDCLASSEX);
        WndClass.style = NULL;
        WndClass.lpfnWndProc = (WNDPROC)WndProc;
        WndClass.cbClsExtra = 0;
        WndClass.cbWndExtra = 0;
        WndClass.hInstance = ghInstance;
        WndClass.hIcon = NULL;
        WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        WndClass.lpszMenuName = NULL;
        WndClass.lpszClassName = gszClassName;
        WndClass.hIconSm = NULL;
    
        if (!RegisterClassEx(&WndClass)) {
           MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
           return(0);
        }
    
        hwnd = CreateWindowEx(WS_EX_STATICEDGE, gszClassName, "test",
                              WS_SYSMENU | WS_MINIMIZEBOX, 150, 50, 500, 479, NULL,
                              NULL, ghInstance, NULL);
    
        if (hwnd==NULL) {
           MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
           return(0);
        }
    
        SetMenu(hwnd, LoadMenu(ghInstance, MAKEINTRESOURCE(ID_MENU)));
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
        InitCommonControls();
    
        while (GetMessage(&Msg, NULL, 0, 0))
           if (!TranslateAccelerator(hwnd, hFkeys, &Msg)) {
              TranslateMessage(&Msg);
              DispatchMessage(&Msg);
           }
        return(Msg.wParam);
     }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
     {
        switch (Msg)
         {
            case WM_CLOSE:
                    DestroyWindow(hwnd);
                    break;
            case WM_DESTROY:
                    PostQuitMessage(0);
                    break;
            case WM_CREATE:
    
                    break;
            case WM_COMMAND:
                 {
                    switch(LOWORD(wParam))
                     {
                        /* check menu */
                        case IDM_SETTINGS_GROUP:
                                if (CheckMenuItem(GetSubMenu(GetMenu(hwnd), 3), IDM_SETTINGS_GROUP, MF_BYCOMMAND | MF_CHECKED)==-1)
                                   MessageBox(NULL, "error!!!!", "", 0);
                                break;
                     }
                 }
                    break;
            default: return(DefWindowProc(hwnd, Msg, wParam, lParam));
         }
        return(0);
     }
    here's the zip, i compiled it with borland 5.5:
    http://thedp.netfirms.com/test.zip --> copy & paste, don't click, it won't work.
    Last edited by Devil Panther; 12-09-2004 at 03:48 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #17
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>could it be because the menu wasn't created yet<<

    Yes. You are not attaching your menu until after the return from CreateWindowEx (see 'remarks' section) and WM_CREATE messages are issued during system processing of that api function.

    You can make it part of the registered window class by using
    Code:
    WndClass.lpszMenuName=MAKEINTRESOURCE(ID_MENU);
    and losing the 'SetMenu' line. Then every window you create of that registered class will have that menu. If, however, you don't want to do it that way then move your 'LoadMenu' to the hMenu parameter of your CreateWindowEx call when creating the main window then only that window will have that menu.
    Code:
    hwnd = CreateWindowEx(WS_EX_STATICEDGE, gszClassName, "test",
                          WS_SYSMENU | WS_MINIMIZEBOX, 150, 50, 500, 479, NULL,
                          LoadMenu(ghInstance, MAKEINTRESOURCE(ID_MENU)), ghInstance, NULL);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #18
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    weird, i never noticed that the menu was part of that class, better late than never


    thanks alot!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Sorry I am late....

    >>GetSubMenu(GetMenu(hwnd), 3),

    I have found that GetMenu() works without the Getsubmenu, if you use ID's rather than positions.

    (as long as you have set the menu in the class reg)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #20
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    you're 100% right!

    for checking using an identifier:
    Code:
    CheckMenuItem(GetMenu(hwnd), IDM_SETTINGS_GROUP, MF_BYCOMMAND | MF_CHECKED);
    and for checking using a position:
    Code:
    CheckMenuItem(GetSubMenu(GetMenu(hwnd), 3),  2, MF_BYPOSITION | MF_CHECKED);
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  3. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Disable menu items.
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2004, 09:48 AM