Win32 GUI application - problem getting dialog box to pop up...
Hi all
I have been following a tutorial on creating win32 GUI applications. I'm having a problem getting a dialog box to pop up on top of my main window ...the CreateDialog() method returns a NULL for some reason.
Would really appreciate some help with this.
Here is the source code -- http://www.apcx.3rror.com/main.cpp
(the program runs, but the test condition for CreateDialog()'s return value shows a NULL value is returned)
The resource file:
Quote:
#include "windows.h"
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "&Press This Button",IDC_PRESS,7,7,84,14
PUSHBUTTON "&Or This One",IDC_OTHER,7,31,84,14
END
Window Procedure of the main window...
Quote:
Code:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
hDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
if(hDialog != NULL)
{
ShowWindow(hDialog, SW_SHOW);
}
else
{
MessageBox(hwnd, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
}
}
break;
//handle other messages...
}
...where the (HWND) hDialog is declared as global variable.
Message Handling function of the dialog box...
Quote:
Code:
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
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;
}
Re: Win32 GUI application - problem getting dialog box to pop up...
Make sure that you message loop looks like this:
Code:
while(GetMessage(&msg, hWnd, 0, 0) > 0)
{
if(!IsDialogMessage(hWnd, &msg)) // This is important!
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
EDIT: Maybe the WS_POPUP can't be a child window. Try to set the third parameter of CreateDialog() to NULL.
Re: Win32 GUI application - problem getting dialog box to pop up...
Hi philkr
I've now added the IsDialogMessage() check to the message loop ... CreateDialog() still returns NULL.
EDIT: ok, now I've tried that too ... still NULL...
Re: Win32 GUI application - problem getting dialog box to pop up...
add WS_VISIBLE to your dialog box...or check the 'Visible' property at your recource file for your dialog box...
Re: Win32 GUI application - problem getting dialog box to pop up...
The problem is your createdialog line itself cannot find the resource
change
hDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
to
hDialog = CreateDialog(GetModuleHandle(NULL),"IDD_TOOLBAR",hwnd, ToolDlgProc);
because of how you've named this object in the resource file you don't need to use the MAKEINTRESOURCE() macro here
also, in the future you should add handling to get more specific information when a function fails. GetLastError() helps track down exactly this kind of problem very quickly and when combined with some output functions you can get a nice tidy message....
Error 1814: The specified resource name cannot be found in the image file.
Re: Win32 GUI application - problem getting dialog box to pop up...
Quote:
Originally Posted by filthy_mcnasty
The problem is your createdialog line itself cannot find the resource
change
hDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
to
hDialog = CreateDialog(GetModuleHandle(NULL),"IDD_TOOLBAR",hwnd, ToolDlgProc);
because of how you've named this object in the resource file you don't need to use the MAKEINTRESOURCE() macro here
also, in the future you should add handling to get more specific information when a function fails. GetLastError() helps track down exactly this kind of problem very quickly and when combined with some output functions you can get a nice tidy message....
Error 1814: The specified resource name cannot be found in the image file.
An okay for the error handling but a no-no to the function call. Resources are always identified by numbers when accessed in the local image. These numbers ger macroed to have a better name e.g. IDD_TOOLBAR. So the OP's code is correct.
Re: Win32 GUI application - problem getting dialog box to pop up...
i know what you're saying and normally i'd say you're right but, did you try it?
the manual creation of the resource file turns the IDD_DIALOG1 into a string literal and no longer simply the number defined previously. Therefore it needs "IDD_DIALOG1" to find the resource for me.
ie, look at the dialog in the resource editor and it shows up as "IDD_DIALOG1" with the quotes and all
MAKEINTRESOURCE returns a string but we already had it
Re: Win32 GUI application - problem getting dialog box to pop up...
Quote:
Originally Posted by filthy_mcnasty
i know what you're saying and normally i'd say you're right but, did you try it?
the manual creation of the resource file turns the IDD_DIALOG1 into a string literal and no longer simply the number defined previously. Therefore it needs "IDD_DIALOG1" to find the resource for me.
ie, look at the dialog in the resource editor and it shows up as "IDD_DIALOG1" with the quotes and all
MAKEINTRESOURCE returns a string but we already had it
Code:
DialogBox(hInst, (LPWSTR)IDD_DIALOG1, NULL, (DLGPROC)DlgProc);
This code works out fine for me, although it is the same MAKEINTRESOURCE() does (casting to LPWSTR).
Quote:
Specifies the dialog box template. This parameter is either the pointer to a null-terminated character string that specifies the name of the dialog box template or an integer value that specifies the resource identifier of the dialog box template.
Both are equal ;)
Re: Win32 GUI application - problem getting dialog box to pop up...
Hello ...
Thanks for the replies.
I've tried changing the CreateDialogBox() method call as shown... but it still returns a NULL value ...
Re: Win32 GUI application - problem getting dialog box to pop up...
Quote:
Originally Posted by apcx
Hello ...
Thanks for the replies.
I've tried changing the CreateDialogBox() method call as shown... but it still returns a NULL value ...
Then please be a good citizen and let us know what GetLastError() returns.
Re: Win32 GUI application - problem getting dialog box to pop up...
Hey philkr
I used GetLastError() ... it printed the value 1813.
Re: Win32 GUI application - problem getting dialog box to pop up...
ah, ok ... nevermind. Something got reset in my project options, and it was no longer including my .rc file in the current project ...oops.
Thanks for the help everyone. Much appreciated.