Advertisement

DialogBox Troubles

Started by July 14, 2002 10:02 AM
5 comments, last by MainForze 22 years, 2 months ago
Hi there! I''m trying to make a program that doesn''t use a regular window as it''s main window, but a dialogbox. It''s easier to place controls using a resource editor that doing at runtime. So I thought I''d simply do a call to DialogBox() in WinMain(). Normally, you need to pass a handle to it''s parent window when calling DialogBox(), but since there isn''t one, I just set it to NULL. In the MSDN docs it said that this was possible. However, the function always returns with an error in my prog. Could somebody give me an example of how to do this? Or at least give me a URL to some useful docs or anything? I can post the source later if you want... Greetz, MainForze
"It's strange, isn't it? You stand in the middle of a library and go 'AAAAAAAGGHHHH' and everyone just stares at you. But you do the same thing on an airplane, and everyone joins in!"
Hi,

I assume what you''re really trying to do is open a window that you''ve created using the Resource editor. If this is what you''re trying to do, then the following code will do that:

--- Cut Here ---

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

BOOL CALLBACK DialogFunc(HWND hdwnd, UINT Msg, WPARAM wParam, LPARAM lParam);


int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{


// Create the window from IDD_WINDOW resource
DialogBox(hInstance, MAKEINTRESOURCE(IDD_WINDOW), 0, DialogFunc);

return 0;
}


// Handles any messages that may come in from windows, or from the dialog
// box being manipulated...

BOOL CALLBACK DialogFunc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
{
//set dialog icon
SetClassLong(hwnd, GCL_HICON, (LONG)LoadIcon(NULL, IDI_APPLICATION));

return TRUE;
}

case WM_CLOSE:
DestroyWindow(hwnd);
break;

case WM_DESTROY:
EndDialog(hwnd, 0);
return TRUE;
}
return FALSE;
}

-- Cut Here ---

Create yourself a Dialog resource, set it to visible and popup and name it IDD_WINDOW

Use SendDlgItemMessage to manipulate the components on the window

Stuart Miller
s-miller@minx.co.uk
Advertisement
Create a normal window but don''t set it as visible. Then just pop up your dialog when your program starts but after the window has been created (such as in your main window''s message pump when you catch WM_CREATE).

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
WOW! Thanks for the quick replies, guys!

Did you type that from the top of your head Stuart (wow)? Or did you copy/paste it from one of your sources ? I noticed that you entered 0 as the hOwnerWindow in your call to DialogBox. Doesn''t that make Windows look for a window with a hWnd of 0? I thought that if you don''t want to specify a hWnd, you should use NULL (as in MessageBox(NULL, "Text", "Caption", MB_OK))?

I haven''t tested this yet, but will do so later this evening or tomorrow.

Oh, and sorry for my cheezy English

Thanks again, both of you!

Greetz, MainForze
"It's strange, isn't it? You stand in the middle of a library and go 'AAAAAAAGGHHHH' and everyone just stares at you. But you do the same thing on an airplane, and everyone joins in!"
NULL is just a #define macro for zero
i.e. its defined like this
#define NULL 0

so using 0 instead of NULL is the same thing

Ian Halliday yo
PWEENED
Ian Halliday yoPWEENED
Hi...

Hehe, no I''ve got an archive of code snippets I use often, and I just ripped it from there...


Stuart Miller
s-miller@minx.co.uk
Advertisement
Having 0 for NULL is not the same thing. However, it has the same effect . Strictly speaking, 0 is just the integer constant 0. NULL is usually defined as 0 in C++, and this is mostly because Bjarne thinks it makes the meaning more clear. In C, NULL is usually defined as ((void*)0) , which is an untyped pointer to address 0 (and this gets implicitly cast to the destination pointer type). Again, the effect is the same, but the meaning is different.

Of course, I can understand Bjarne's thinking. For consistency, it makes sense that you should use 0 just like you can assign the numeric value 12 to a pointer. But such a practice is intrinsically dangerous—it's much safer to take the addresses of variables than hard-coding addresses. Worse, what if you're on a system where 0 is a valid address? Then the compiler will have to know to translate that, which will still leave 0 unusuable unless you do a trick like a - 1 where a is an integer of value 1. It's a pedantic argument since there isn't a single relevant machine that makes this a problem, but it's a legitimate point for the good of abstractions, and that's what the NULL macro is.

So, if you ask me, the traditional C definition makes more sense particularly because, at a glance, when I see NULL I know instantly that I'm dealing with pointers—the meaning is obvious and explicit. But when I see 0, I could be dealing with integers, floats, or pointers, and you must differentiate by context. And that forces me to look at the code a little closer, which I think that I should have to do.

Incidentally, I do follow the C++ convention in C++ and the C convention in C.

Does this dialog of yours happen to have any fancy controls like up-down, extended combo box controls, etc.? If so, then your problem may simply be not having called InitCommonControls or InitCommonControlsEx before creating your dialog (normally, it's put at the beginning of your app somewhere). Even if this isn't the problem and you can't get it working, try removing the controls from the dialog. If it suddenly works, then you know that something about the controls isn't working right; just enable them one-by-one by type until you find the culprit. Then MSDN will be a good place to look.


[edited by - merlin9x9 on July 14, 2002 8:20:34 PM]

This topic is closed to new replies.

Advertisement