Menu

help with win32 tutorial

robasc
2008-01-02
2012-09-26
  • robasc

    robasc - 2008-01-02

    I am working with this tutorial on win32 programming: http://www.winprog.org/tutorial/menus.html

    after building everything It does not work. I do not believe I am understanding it correctly. This is what i put together from the tutorial. It says to build a .rc file not sure if this is correct.
    [code]

    include <windows.h>

    include "resource.h"

    //left off at page menus on : http://www.winprog.org/tutorial/menus.html
    // added menu.rc
    const char g_szClassName[] = "myWindowClass";

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_LBUTTONDOWN:
    {
    char szFileName[MAX_PATH];
    HINSTANCE hInstance = GetModuleHandle(NULL);

            GetModuleFileName(hInstance, szFileName, MAX_PATH);
            MessageBox(hwnd, szFileName, &quot;This program is:&quot;, MB_OK | MB_ICONINFORMATION);
        }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    

    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    //just added this...................................................//
      wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    
    wc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
    

    //end of adding........................................................//

    if(!RegisterClassEx(&amp;wc))
    {
        MessageBox(NULL, &quot;Window Registration Failed!&quot;, &quot;Error!&quot;,
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        &quot;The title of my window&quot;,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);
    
    if(hwnd == NULL)
    {
        MessageBox(NULL, &quot;Window Creation Failed!&quot;, &quot;Error!&quot;,
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)
    {
        TranslateMessage(&amp;Msg);
        DispatchMessage(&amp;Msg);
    }
    return Msg.wParam;
    

    }

    //resource.h file

    define IDR_MYMENU 101

    define IDI_MYICON 201

    define ID_FILE_EXIT 9001

    define ID_STUFF_GO 9002

    //menu.rc file

    include "resource.h"

    IDR_MYMENU MENU
    BEGIN
    POPUP "&File"
    BEGIN
    MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP &quot;&amp;Stuff&quot;
    BEGIN
        MENUITEM &quot;&amp;Go&quot;, ID_STUFF_GO
        MENUITEM &quot;G&amp;o somewhere else&quot;, 0, GRAYED
    END
    

    END

    IDI_MYICON ICON "menu_one.ico"
    [/code]

     
    • cpns

      cpns - 2008-01-03

      "It does not work" is still unacceptable since there are so many ways in which anything may "not work".

       
    • cpns

      cpns - 2008-01-02

      So what does not work?

      There are some example Win32 GUI projects in c:\devcpp\examples, look how they are put together. You also need the set the build options for a Win32GUI app. You might also start with the basic Win32 GUI template (File->New->Project). Either way you will need to start with a 'project'.

      Next time post the Compile Log text so we can have at least some idea of what you are doing and how it fails!

      Clifford

       
    • BiT

      BiT - 2008-01-02

      Actually they need to go back and read the tutorial again as they didnt follow it fully. Which is obvious by looking at the following line in the code they supplied.

      wc.lpszMenuName = NULL;

      So my advice to the poster is go back and read and follow the tutorial again.

       
    • robasc

      robasc - 2008-01-02

      Thanks guys,

      Well thank you for your advice, I went back and read the tutorial and realized what I done wrong. For one I did not have the correct resource file menu_one.ico and secondly, I did not remove the wc.lpszMenuName = NULL;

      thanks again for the tip!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.