Menu

Help with source code

Anonymous
2002-08-13
2012-09-26
  • Anonymous

    Anonymous - 2002-08-13

    Dear Bloodshed software forum,
    I am relatively new to windows programming.  I recently got a hold of some great source code for creating a window toolbar.  Unfortunately, the program reports that the value of the toolbar is NULL.  I tried modifying it because the main menu at the top didn't even work.  So here's the modified version:

    #include <windows.h>

    #define IDD_TOOLBAR                     101
    #define IDC_PRESS                       1000
    #define IDC_OTHER                       1001
    #define ID_FILE_EXIT                    40001
    #define ID_DIALOG_SHOW                  40002
    #define ID_DIALOG_HIDE                  40003

    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE        102
    #define _APS_NEXT_COMMAND_VALUE         40004
    #define _APS_NEXT_CONTROL_VALUE         1002
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif

    const char g_szClassName[] = "myWindowClass";

    HWND g_hToolbar = NULL;

    BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDC_PRESS:
                        MessageBox(hwnd, "Hi!", "This is a message",
                            MB_OK | MB_ICONEXCLAMATION);
                    break;
                    case IDC_OTHER:
                        MessageBox(hwnd, "Bye!", "This is also a message",
                            MB_OK | MB_ICONEXCLAMATION);
                    break;
                }
            break;
            default:
                return FALSE;
        }
        return TRUE;
    }

    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_DIALOG_SHOW, "&Show Toolbar");
                AppendMenu(hSubMenu, MF_STRING, ID_DIALOG_HIDE, "&Hide Toolbar");
                AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Dialog");

                SetMenu(hwnd, hMenu);

                g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR),
                    hwnd, ToolDlgProc);
                if(g_hToolbar != NULL)
                {
                    ShowWindow(g_hToolbar, SW_SHOW);
                }
                //Problem is right here: g_HToolbar = NULL!!!
                else
                {
                    MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",   
                        MB_OK | MB_ICONINFORMATION);
                }
            break;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case ID_FILE_EXIT:
                        PostMessage(hwnd, WM_CLOSE, 0, 0);
                    break;
                    case ID_DIALOG_SHOW:
                        ShowWindow(g_hToolbar, SW_SHOW);
                    break;
                    case ID_DIALOG_HIDE:
                        ShowWindow(g_hToolbar, SW_HIDE);
                    break;
                }
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                DestroyWindow(g_hToolbar);
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, Message, 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_3DSHADOW+1);
        wc.lpszMenuName  = 0;
        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;
        }

        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "Toolbar App",
            WS_OVERLAPPEDWINDOW,
            100, 100,CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, hInstance, NULL);

        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }

        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);

        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            if(!IsDialogMessage(g_hToolbar, &Msg))
            {
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
            }
        }
        return Msg.wParam;
    }

    Even if you don't know how to fix it, I would appreciate and comments and suggestions.  Thanks for you help!!!

     
    • Nobody/Anonymous

      It seems to me that CreateDialog must be failing and returning null in the following line.

      g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR),
      hwnd, ToolDlgProc);

      Have you made sure that you're passing the right values to it? That's all I can think of.

      Chris

       
    • Jack Black

      Jack Black - 2002-08-13

      Steven,

      Your call to CreateDialog is failing because you are referring to MAKEINTRESOURCE(IDD_TOOLBAR) but not creating a Dialog resource in a resource file for IDD_TOOLBAR.

      Try creating the following in a file called MyResource.RC    :-

      #include <windows.h>
      #include "resource.h"
      //---------------------------------------------------------------------
      IDD_TOOLBAR DIALOG DISCARDABLE  22, 17, 230, 75
      STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
      CAPTION "Test"
      FONT 8, "System"
      BEGIN
          LTEXT           "FIRST DIALOG",IDC_STATIC,49,10,119,8, SS_NOPREFIX
          DEFPUSHBUTTON   "PRESS",IDC_PRESS,155,6,30,11,WS_GROUP
          DEFPUSHBUTTON   "OTHER",IDC_OTHER,195,6,30,11,WS_GROUP
      END
      //---------------------------------------------------------------------

      You should move the #defines in your main program to a resource.h file. This header can then be included into your main file and the resource file. You will need to add one IDC_STATIC if you want this to work. Here is my code for the Resource.h I created to make your code work:

      //---------------------------------------------------------------------
      #ifndef RESOURCE_H
      #define RESOURCE_H

      #define IDD_TOOLBAR                     101
      #define IDC_PRESS                       1000
      #define IDC_OTHER                       1001
      #define IDC_STATIC                      102
      #define ID_FILE_EXIT                    40001
      #define ID_DIALOG_SHOW                  40002
      #define ID_DIALOG_HIDE                  40003

      #endif
      //---------------------------------------------------------------------

      I tested this and it works with no other changes to your code. The dialog I created is not very elegant (it's too late at night here!!!) but it demonstrates what you want to do.

      Enjoy,  

      BlakJak ;]

       

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.