Thread: Win32 menus and resources help

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    35

    Win32 menus and resources help

    hey,
    I am reading through the tutorails from forgers on Win32. I can't seem to grasp the whole concept of resources and menus. When I try to compile certain bits of code that he provides in the menus section, it won't work. When I use resources, do I have to put #include <resource.h> at the top? Is there anything that I will have to change to get it to work on Dev C++? Any help would be great...thx l8er

    -fire

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Post the source code that is giving you problems, and the error messages you recieve. I don't recall having to alter the examples to get them to work in Dev-C++ - so use resource.h as directed in Forger's.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    First off, I found a serious.....well not really a problem or error, more like something they didn't or couldn't predict.....with the forgers tutorial. I don't know if this is true with all compilers, but with mine, I had to #include windows.h in the resource and in my main code. Before that, it simply would not compile, no matter what I did. And yes, you MUST #include your resource.h file in BOTH the rescource and the main code, same as I have to with windows.h. See if that gets rid of your erros.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    here is the code and I am not sure where and how to add in all the resource code.
    i added the include resouce.h and the defines. Don't know if it is in the right spot or written correctly.
    Code:
    #include <windows.h>
    #include "resource.h"
    const char g_szClassName[] = "myWindowClass";
    #define IDR_MYMENU 101
    #define IDI_MYICON 201
    
    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002
    #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 "G&o somewhere else", 0, GRAYED
        END
    END
    
    IDI_MYICON ICON "menu_one.ico"
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_LBUTTONDOWN:    // <-
        // BEGIN NEW CODE
            {
                char szFileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);
    
                GetModuleFileName(hInstance, szFileName, MAX_PATH);
                MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
            }
            LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_CREATE:
            {
                HMENU hMenu, hSubMenu;
    
                hMenu = CreateMenu();
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
    
                SetMenu(hwnd, hMenu);
    
    
                hIcon = LoadImage(NULL, "menu_two.ico", 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, "menu_two.ico", 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:
    
                    break;
                    case ID_STUFF_GO:
    
                    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;
    
        //Step 1: Registering the Window Class
        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);
    
        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;
    }
    not sure if the bottom of the code is needed for this problem since I am sort of confused ..thanks for the help...

    -fire

  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    You shouldn't have to include windows.h in your resource header. The resource header should just define a bucnh of IDs, and nothing more. the resource file should just define the structure and type of your resources, and the code file that uses the resources should include resource.h. windows.h should be used when windows API functions and #defines are used.

    Cheers!

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    thx colonial, so i should have:
    #include "resource.h"
    defines...
    I am still not quite getting this. When do I use the windows and its defines. From what I have read, a resource is like a seperate section and is implimented into the windows.h. So in a program I would have includes for windows and its defines then resource and its defines then bring them up in the code when needed? Will try to work this out when I get on my home comp(at school now). Thanks again

    -fire

  7. #7
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Hello,

    Your files should look something like this:

    resource.h
    Code:
    #define IDR_MYMENU 101
    #define IDI_MYICON 201
    
    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002
    resourcescript.rc
    Code:
    #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 "G&o somewhere else", 0, GRAYED
        END
    END
    
    IDI_MYICON ICON "menu_one.ico"
    yoursourcecode.cpp
    Code:
    #include <windows.h>
    #include "resource.h"
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_LBUTTONDOWN:    // <-
        // BEGIN NEW CODE
            {
                char szFileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);
    
                GetModuleFileName(hInstance, szFileName, MAX_PATH);
                MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
            }
            LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_CREATE:
            {
                HMENU hMenu, hSubMenu;
    
                hMenu = CreateMenu();
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    
                hSubMenu = CreatePopupMenu();
                AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
    
                SetMenu(hwnd, hMenu);
    
    
                hIcon = LoadImage(NULL, "menu_two.ico", 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, "menu_two.ico", 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:
    
                    break;
                    case ID_STUFF_GO:
    
                    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;
    
        //Step 1: Registering the Window Class
        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);
    
        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;
    }
    Note, i haven't compiled this, but it should at least give you the idea as to what should be included and where

    Good luck!

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    thx for the example. This clears up resources a bunch and hopefully menus will be made by me lol ....thx again and I will try to get this to work tonight...

    =fire

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    I was trying to figure this out and am coming across a few more questions on the setup.
    As of now I have my source code file and my resourcescript.rc file. How do I add a resource.h file? I hope this is correct so far. I am using Dev C++ and I have 2 tabs at the top with the 2 files. Is the resouce.h file a 3rd tab and how do I go about creating it? thx l8er

    -fire

  10. #10
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    when i try to compile that it won't. It says cannot make createfile:
    I started out with nothing and I still have most of it left.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    I tryed going through the tutorial again and couldn't get the program to work. I compiled each section and still no luck. Here is a pic of the 3 files then the code 4 them...
    the pic is at the bottom of this post.

    here are the codes for the 3 files

    resource.h
    Code:
    #define IDR_MYMENU 101
    #define IDI_MYICON 201
    
    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002
    resourcescript.rc
    Code:
    #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 "G&o somewhere else", 0, GRAYED
        END
    END
    
    IDI_MYICON ICON "menu_one.ico"
    source.cpp
    Code:
    #include <windows.h>
    #include "resource.h"
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    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;
            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;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       =  (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
    
        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;
    }
    Is there a problem with the linking of these files or the setup? Any guidance in the right direction would be great...thx l8er

    -fire
    Last edited by firestorm; 04-09-2005 at 12:07 PM.

  12. #12
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, I see the name of your resource under your project tree (sub-window on the left) so that means its in your project.

    What errors are you getting? Or is it compiling and simply not working?

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    it compiles fine but when i hit compile and run it does run. Does this make sense?
    well I say it compiles fine, but normally when u just click compile it shows up done then u click ok i think(somethin like that). Now it just throws the window up there saying compile but never completes it.

    Also, when I open the folder with all these files in it there are a total of 6 files. Shouldn't I only have 3? or does each 1 have to be save under the project and the type of format? Thx l8er

    -fire

  14. #14
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, Dev C++, as with a lot of compilers, generates its own files on compilation. If you've got a resource, there should be two files called "yourprojectname"_Private, one a .h and one a .rc. The other is probably the .o (binary form) of your .cpp.

    Okay, ignoring the compiling thing for a sec, let's a assume it is compiling correctly. How does it run?

  15. #15
    Registered User
    Join Date
    Apr 2005
    Posts
    35
    when I click run it says it is not compiled. When I hit "compile and run", a brief flash of the screen pops up but nothing more than that. Anything I do to get it to compile? thx l8er

    -fire

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Resources in Dev C++
    By JJFMJR in forum Windows Programming
    Replies: 7
    Last Post: 08-27-2007, 05:14 AM
  3. Icons of pop-up menus
    By ExDigit in forum Windows Programming
    Replies: 1
    Last Post: 01-11-2002, 05:13 PM
  4. Edit Menus
    By ripper in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2001, 02:00 PM