So basically I am new to API programming and teaching myself. I've done a bit of console C++ that I got off of.... an internet tutorial. The story is the same here too. I am somewhat aware of what the lines of code do, though I am not yet a dictionary so please if anyone wishes to help, keep that in mind.

Onward to the problem --> I am trying to make a dialog box show up when I click on one of my menu options.
Curently the menu option I chose is "Blurp"-->"Go"
When I compile and run the program, I get my window and menu but upon selecting "Go", I get the "Oh God..." window saying "Dialog failed!" so I guess it's not creating the window...
I have checked my dialog box code (made in Resource Builder) against (in) another program that runs the dialog box out of a .rc file and it worked.

#include <windows.h>
#include "resource.h"

HWND hwnd;
HINSTANCE hInstance;
const char g_szClassName[] = "myWindowClass";

BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

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(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;
    }

    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);

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

BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
            return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    EndDialog(hwnd, IDOK);
                    break;
                case IDCANCEL:
                    EndDialog(hwnd, IDCANCEL);
                    break;
            }
            break;
        default:
            return FALSE;
    }
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
        {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_EXIT:
                    DestroyWindow(hwnd);
                    break;
                case ID_BLURP_GO:
                    {
                        int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);

                        if(ret == IDC_BUTTON0)
                        {
                            MessageBox(hwnd, "OK... OK?", "Notice", MB_OK | MB_ICONINFORMATION);
                        }
                        else if(ret == IDC_BUTTON1)
                        {
                            MessageBox(hwnd, "CANCELLED!", "Notice", MB_OK | MB_ICONINFORMATION);
                        }
                        else if(ret == -1)
                        {
                            MessageBox(hwnd, "Dialog failed!", "Oh God...", MB_OK | MB_ICONINFORMATION);
                        }
                    }
                    break;
            // Other menu commands...
                }
                break;
        case WM_LBUTTONDOWN:
            {
                char szFileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);

                GetModuleFileName(hInstance, szFileName, MAX_PATH);
                MessageBox(hwnd, szFileName, "This program is:", 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;
}

Here is my .h file (There may be some extraneous definitions but I figure they can be weeded out later if they are there)

#define IDR_MYMENU 101
#define IDI_MYICON 201

#define ID_FILE_EXIT 9001
#define ID_BLURP_GO 9002

#define ID_HELP_ABOUT 9003
#define IDD_ABOUT 9004
#define DIALOG_0 9006

#define IDC_BUTTON0 0
#define IDC_BUTTON1 1
#define IDC_STATIC -1
#define IDC_TRACKBAR2 2

Here is my Menu - menu_one.ico is something I just had to pick and place myself... but it works for me.

#include "resource.h"
#include "windows.h"

IDR_MYMENU MENU
BEGIN
    POPUP "&Blamo"
    BEGIN
        MENUITEM "W&indow Smash!", ID_FILE_EXIT
    END

    POPUP "&Blurp"
    BEGIN
        MENUITEM "&Go", ID_BLURP_GO
        MENUITEM "O&ut of Bounds", 0, GRAYED
    END
END

IDI_MYICON ICON "menu_one.ico"

And even though it seems to be fine, here is the dialog code (has extraneous features just for fun):

#include "windows.h"
#include "resource.h"

IDD_ABOUT DIALOG MOVEABLE PURE LOADONCALL DISCARDABLE 0, 0, 250, 100
STYLE DS_FIXEDSYS |DS_SETFONT |WS_POPUP |WS_VISIBLE |WS_SYSMENU |WS_THICKFRAME |WS_MAXIMIZEBOX |WS_MINIMIZEBOX |WS_CAPTION
CAPTION "Yoyo This Is It"
FONT 8, "Ms Shell Dlg"
LANGUAGE LANG_NEUTRAL, 0
BEGIN
  CONTROL "Go",IDC_BUTTON0,"BUTTON",BS_DEFPUSHBUTTON |BS_VCENTER |BS_CENTER |WS_CHILD |WS_TABSTOP |WS_VISIBLE ,29,60,63,20
  CONTROL "Dude!",1,"BUTTON",BS_DEFPUSHBUTTON |BS_VCENTER |BS_CENTER |WS_CHILD |WS_TABSTOP |WS_VISIBLE ,157,60,63,20
  CONTROL "",2,"msctls_trackbar32",WS_CHILD |WS_VISIBLE |0x18,10,20,230,20
  CONTROL "Woah Slide Bar",IDC_STATIC,"STATIC",SS_LEFT |WS_CHILD |WS_GROUP |WS_VISIBLE ,15,9,51,8
END

So yeah, the code feels a bit patchwork to me when I think about it, but it runs just fine aside from this dialog box problem.

In short, I am just needing help getting the dialog box to ... appear when I click "Blurp"-->"Go". However I would be totally thrilled to hear (read) suggestions or input of any kind in adittion to that.

Recommended Answers

All 7 Replies

P.S. I am pretty much certain that I am just missing some function somewhere to actually create the box, I just can't figure it out >.< ... Rawawarrr.

Well... back again... if you can't tell I am still actively playing with this sucker and ... I intend to figure it out.

I got the box working on right click without the menu, but ... when I added the menu... BLAM there goes functionality.... I think I just don't know how to link it to the menu (or the other way around if that's the case)...

Hi,

To sum up, win32 api is a bitch. I'm not an expert on that but until somebody more knowledgeable appears i'd suggest checking http://www.winprog.org/tutorial/dialogs.html to see what you are missing.
Also, if you think an error maybe ocurring, try and see what GetLastError() has to say.

That is the verry tutorial I was using to make this thing. I'll go play around with that GetLastError thing and see what it does for me.

And ... yeah, it is a total pain, but thus far I like what it can do.

Thanks for the suggestions btw.

AHHHHHHH! It was so.... stupidly .... simple! I was using separate .rc files.

one for the menu, and one for the dialog box... and when I put them together into ONE file it worked just fine. So hurah hurah, I figured it out.

It was so.... stupidly .... simple!

As usual with these things! Glad you solved it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.