Teen Programmers Unite  
 

 

Return to forum top

I need help with dialog boxes (Win32 API)

Posted by CodeRed [send private reply] at October 27, 2001, 12:23:09 PM

I've got a Help->About menu and I'm trying to make it so that when you select about it brings up a dialog box. I've gotten the dialog box made okay, but I'm having problems in the message loop (LRESULT CALLBACK). I've got a switch structure to select the message, here's the WM_COMMAND case that I'm having problems with:

case WM_COMMAND:
switch( wParam )
{
case IDM_ABOUT:
DialogBox(ghInstance, "AboutDlg", hWnd, (DLGPROC)AboutDlgProc);
break;
}
break;

The DialogBox call gives me these errors:

Error: generic.cpp(129,80):Cannot convert 'void *' to 'HINSTANCE__ *'
Error: generic.cpp(129,80):Type mismatch in parameter 'hInstance' in call to '__stdcall DialogBoxParamA(HINSTANCE__ *,const char *,HWND__ *,int (__stdcall *)(HWND__ *,unsigned int,unsigned int,long),long)'

I know the rest of the code is okay because if I comment out that case (WM_COMMAND) it compiles and executes perfectly (except that you can't select the about menu item, obviously)

Heres the dialog code if you need it:

1 DLGINCLUDE "generic.h"

AboutDlg DIALOG FIXED 6, 21, 198, 99
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "About Generic"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "&OK", IDOK, 72, 74, 40, 14
LTEXT "Generic Application", 104, 45, 14,
128, 8
LTEXT "Written as a sample", 105, 45, 35, 59, 8
LTEXT "Microsoft Corporation", 106, 45, 45, 98, 8
LTEXT "Copyright (c) 1996", 107, 45,
54, 138, 8
END

As you can see, it's taken directly from the MSDN, which is why I don't understand why it doesn't work. Thanks in advance

Posted by CodeRed [send private reply] at October 27, 2001, 12:25:04 PM

Well the text formatting obviously doesn't work, I hope you can still understand it.

Posted by taubz [send private reply] at October 28, 2001, 05:57:24 PM

What is the declaration of ghInstance? (Because the error says the error is in the hInstance parameter.)

- taubz

Posted by CodeRed [send private reply] at October 28, 2001, 06:28:47 PM

It's a global handle to an instance. It's declared globally as:

HANDLE ghInstance;

After the window is created it is set to hInstance:

ghInstance = hInstance;

Do you think I should just pass hInstance instead of ghInstance, would that make a difference?

Posted by CodeRed [send private reply] at October 28, 2001, 06:32:27 PM

Here is the message loop for the dialog box:

LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch( wParam )
{
case IDOK:
EndDialog( hDlg, TRUE );
return TRUE;
}
break;
}
return FALSE;
}

Posted by taubz [send private reply] at October 29, 2001, 11:34:07 AM

Maybe pass &ghInstance instead of ghInstance. The parameter expects HINSTANCE__ * and your passing a HANDLE. I'm not sure how HANDLE and HINSTANCE__ compare, though. I think that function might even be deprecated in the first place, as iirc hInstances aren't used anymore since Win95.

- taubz

Posted by miken [send private reply] at October 29, 2001, 07:44:01 PM

Let's try a few things:

1) Change the definition of AboutDlgProc to
BOOL APIENTRY AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );

2) Inside your WinMain(HINSTANCE HInst, ...) function, call the DialogBox(...) function directly using HInst as the first variable. NOT IN A SUBROUTINE TO BEGIN WITH, but directly in WinMain(). When this works, change its location if you wish (with the proper global variables, of course).

3) Instead of specifying a HWnd value in the DialogBox() call, put NULL for that parameter. Really, you don't need the handle to the calling window at all unless you're doing some advanced applications work.

I hope this helps - tell us what happens!
- Danny

Posted by CodeRed [send private reply] at October 30, 2001, 10:05:34 AM

Thanks, but the problem was I was declaring ghInstance as a HANDLE for some reason when I should have been declaring it as an HINSTANCE. Works fine now. Actually that wasn't my problem, the code was copied directly from an example in the MSDN.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.