Thread: Window menu Probem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Window menu Probem

    i m trying to make a base of a program that i click on something in menu it does it but i can also like do another thing >> lets say i m trying to pressing open it function and also do another function it wont do the other function when i press in the menu unless it does the first function first i dunno why but is it possible in gui programs to do two programs because i m just learning abt windows api now so i hope its possible here i made program
    that do stuff but it doesnt work if i pressed EXIT sometimes when i go to exit menu program freezes so any ideas ?
    Code:
    #include "resource.h"
    #include <windows.h> 
    
    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002
    #define ID_STUFF_DO 9003
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    int do_stuff();
    
    
    int cr8_window(int nCmdShow)
    {
    	const char g_szClassName[] = "myWindowClass";
    	WNDCLASSEX wc;// Data structure for the windowclass 
        HWND hwnd;//window handle
        MSG Msg;//used for translate msg
    	HINSTANCE hInstance = GetModuleHandle(NULL);
    
        //Step 1: Registering the WindowClass
        wc.cbSize        = sizeof(WNDCLASSEX);// The size of the structure
        wc.style         = 0;// Class Styles (CS_*), not to be confused with Window Styles (WS_*) This can usually be set to 0. 
        wc.lpfnWndProc   = WndProc;// Pointer to the window procedure for this window class. 
        wc.cbClsExtra    = 0;// Amount of extra data allocated for this class in memory. Usually 0. 
        wc.cbWndExtra    = 0;// Amount of extra data allocated in memory per window of this type. Usually 0. 
        wc.hInstance     = hInstance;// Handle to application instance (that we got in the first parameter of WinMain()). 
        wc.hIcon         = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_MSKIN));// Large (usually 32x32) icon shown when the user presses Alt+Tab. 
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);// Cursor that will be displayed over our window. 
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);// Background Brush to set the color of our window.
        wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);// Name of a menu resource to use for the windows with this class. 
        wc.lpszClassName = g_szClassName;// Name to identify the class with. 
        wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_MSKIN), IMAGE_ICON, 16, 16, 0);// icon to show in the taskbar and in the top left corner of the window. 
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
    {
    	cr8_window(nCmdShow);
    }
    
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_CREATE:
            {
                HMENU hMenu, hSubMenu;
    			HICON hIcon, hIconSm;
    
                hMenu = CreateMenu();
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "EXIT");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Main");
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&dostuff");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
    
                SetMenu(hwnd, hMenu);
    
    
                hIcon = LoadImage(NULL, "Skin.bmp", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
                if(hIcon)
                    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
                else
                    MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
    
                hIconSm = LoadImage(NULL, "Skin.bmp", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
                if(hIconSm)
                    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
                else
                    MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
            }
            break;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case ID_FILE_EXIT:
    					DestroyWindow(hwnd);
                    break;
                    case ID_STUFF_GO:
    							do_stuff();
                    break;
                }
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }
    
    
    
    
    int do_stuff()
    {
    	for(;;) {
    		MessageBox(NULL,"COOL","COOL",MB_OK);
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I'm not sure I completely understand what you're trying to do. But anyway, it looks like you're trying to load a menu from a resource (IDR_MYMENU) and also create a menu on the fly. Are you using a resource file?. If so, please post it.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Code:
    /*resource.rc*/
    #include "resource.h"
    IDR_MYMENU MENU
    BEGIN
        POPUP "&File"
        BEGIN
            MENUITEM "E&xit", ID_FILE_EXIT
        END
    
        POPUP "&Stuff"
        BEGIN
            MENUITEM "&Go", ID_STUFF_GO
    		MENUITEM "&Do", ID_STUFF_DO
            MENUITEM "G&o somewhere else", 0, GRAYED
        END
    END
    
    IDB_MSKIN BITMAP DISCARDABLE "Skin.bmp"
    Code:
    /*resource.h*/
    #define IDR_MYMENU 101
    #define IDB_MSKIN 201
    
    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002
    #define ID_STUFF_DO 9003

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    whenever i click like do stuff it only does that i want also to do other function with that also because i wanna do a program for networking a mini chat program but i just guess i test this out first coz before i do it so i be good in gui

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Do you mean that you want it to do something asynchronously? In the background? I notice that you have a for(; in your do_stuff function. In that case, multithreading would be the easiest way to do it, in my opinion.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by tjb View Post
    Do you mean that you want it to do something asynchronously? In the background? I notice that you have a for(; in your do_stuff function. In that case, multithreading would be the easiest way to do it, in my opinion.
    yah i made for (; because i will also have a for in my connect i think i should code it and if its not work i will show it as a problem but i m sure it wont work since connect is blocking so
    i tried this code before i code the program to see how will i do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM