# Solved: Win32 API - Dialog box problem.



## burnthepc (Aug 12, 2007)

I'm having a bit of trouble getting a dialog box to work as it should. I've made the dialog box in the Visual Studio Dialog box editor and I'm trying to plug it into my main program.

Here's what I've done:

created the DialogBox and plugged in my own dialog box procedure into the 4th parameter.

The dialog box procedure traps the init dialog message, and the WM_COMMAND message (only processing the ok and cancel messages from the buttons).

The problem is that the message box fails to paint its background and the buttons hover over the background window. It's as if an ok and a cancel button suddenly poped into the main window.

If I remove my dialog box procedure from the call the box shows correctly, paints its window and looks fine. (Of course nothing works as there's no dialog procedure.)


What am I doing wrong? I can't be needed to process the WM_PAINT messages as I want the dialog box to paint its client area itself...but that's precisely what it's not doing...


----------



## mt2002 (Sep 14, 2005)

> If I remove my dialog box procedure from the call the box shows correctly, paints its window and looks fine. (Of course nothing works as there's no dialog procedure.)


From what call? I am thinking we need to see some code.


----------



## burnthepc (Aug 12, 2007)

Don't have my code with me... I'm at work 

But basically I'm doing this:

CreateDialog (hInst, MAKEINTRESOURCE (ID_DIALOG ), 0, DialogProc);

Paints just the buttons on the client area.


then I take the dialog proc out
CreateDialog (hInst, MAKEINTRESOURCE (ID_DIALOG ), 0, NULL);

Paints the whole thing fine, but there's no way to exit the dialog box.


And the dialog proc is doing not very much apart from trapping init dialog messages and WM_COMMAND.

All others return false.


----------



## mt2002 (Sep 14, 2005)

Is your dialog wnd proc returning false for all unhandled messages and true for handled messages?

Are you calling DefDlgProc inside of the dialog wnd proc?


----------



## burnthepc (Aug 12, 2007)

> Is your dialog wnd proc returning false for all unhandled messages and true for handled messages?


Yes. True if I handle it, false if I don't



> Are you calling DefDlgProc inside of the dialog wnd proc?


That could be the problem. Where do I call DefDlgProc? Is it part of the WinMain message pump? I've seen something there about processing dialog messages...wasn't sure if it was needed in this case.


----------



## mt2002 (Sep 14, 2005)

Er... Never mind. You aren't supposed to use DefDlgProc (I havnt done Win32 programing for a few months, sorry about that  ).

It may help out if you post how you create your dialog proc (CreateWindow? CreateDialog?) and your dialog's procedure. It kind of sounds like the WM_PAINT message is not being properly handled by Windows. (ie; your dialog proc returns false)


----------



## burnthepc (Aug 12, 2007)

Ok here's all the code refering to the Dialog box in my program:

The call to create:

DialogBox(hinstGLOBAL, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AddStringDlg)

The dialog proc:


```
BOOL CALLBACK AddStringDlg(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{

switch(message)
{
case WM_INITDIALOG:
	return TRUE;

case WM_COMMAND:

	switch(LOWORD(wParam))
	{
	case IDOK:
		addStringBuffer = L"State A";
		EndDialog(hdlg, TRUE);
		return TRUE;
	case IDCANCEL:
		EndDialog(hdlg, FALSE);
		return FALSE;
	}

	return FALSE;
}
}
```


----------



## mt2002 (Sep 14, 2005)

Try this:


```
BOOL CALLBACK AddStringDlg(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_INITDIALOG:
	    return TRUE;

        case WM_COMMAND:

	    switch(LOWORD(wParam))
	    {
	    case IDOK:
		addStringBuffer = L"State A";
		EndDialog(hdlg, TRUE);
		return TRUE;
	    case IDCANCEL:
		EndDialog(hdlg, FALSE);
		return FALSE;
	    }
     }

    return FALSE;
}
```


----------



## burnthepc (Aug 12, 2007)

Ok that's embarrassing...not matching my brackets.

Why oh why does everything cast to true !?!

Thanks for your help mt2002


----------

