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 a zip file containing the main CPP and as well as the resource file.

http://www.apcx.3rror.com/test.zip

(the program runs, but the test condition for CreateDialog()'s return value shows a NULL value is returned)

http://www.apcx.3rror.com/main.cpp


The resource file:

#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...

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...

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

Try putting all the definitions,

#include <windows.h>

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_ABOUT 9003
#define IDD_TOOLBAR 9004
#define IDC_PRESS 9005
#define IDC_OTHER 9006
#define ID_DIALOG_SHOW 9007
#define ID_DIALOG_HIDE 9008
#define ID_HELP_ABOUT 9009
#define IDD_ABOUT 9010

to a resource.h file and including the resource.h file to both your .rc file and main.cpp file. I dont know if the compiler allows the present rc file name. It hasn't got a file name. Only an extention. Correct that also.

What is the compiler you are using. Is it a IDE or command line compiler?

Hi Wolfpack

I'm using the Dev-Cpp IDE (I think it's the MinGW compiler that comes along with dev-cpp).

I tried the above, CreateDialogBox() still returns a NULL...
Is there an error in the contents of my .rc file?

There was nothing wrong with your rc file. I am using the Visual Studio IDE, and here is what I did to make the project compile.

1. Rename .rc to main.rc 2. Create a new file resource.h 3. Put the

#include <windows.h>

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_ABOUT 9003
#define IDD_TOOLBAR 9004
#define IDC_PRESS 9005
#define IDC_OTHER 9006
#define ID_DIALOG_SHOW 9007
#define ID_DIALOG_HIDE 9008
#define ID_HELP_ABOUT 9009
#define IDD_ABOUT 9010

of your main.cpp file to the resource.h file.
4. Added

#include "resource.h"

to your main.cpp file and main.rc file.
5. Added the above 3 files to a Visual Studio Solution and built it..

After that the program ran okay. I dont know the procedure for dev-Cpp. But I think you will have to do the above 4 steps for any compiler. Try it and tell the result. Only the way you compile; step 5, will change for Dev-Cpp. Here are the modified files.

Attachments

ah ... it runs fine in Dev-cpp. Thank you SO much, Wolfpack.

Just one more question. I'm creating this program to do some bitmap displaying/manipulation ... so I'd like to have another window (a blank one with no windows style/border and NO menu) pop up over this window.


So right after this bit of code ...

WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */

   
    
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    
    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

... I tried the following:
1.
wincl.lpfnWndProc = ImageWindowProc();
registered wincl
created a new HWND imageHwnd using CreateWindowEx()
then used the ShowWindow method to display this new window as well.


2.
created a new WNDCLASSEX -- wincl2.
After the creation of the main window:
initialized wincl2
registered wincl2
created a new HWND imageHwnd using CreateWindowEx()
used ShowWindow method to display this new window as well.

The problem is that in both of the above cases, the new window (whether it is the child is created as the child window of the main window or not) ... appears with the same menu that was added to the main window. The new window appears to share the main window's WindowProcedure method, even though I have tried to set it otherwise.


(My ImageWindowProc does NOT yet respond to any messages, e.g. display menu, dialogbox etc.)

I dont know what you are trying to do. But try replacing your main file with the one I have attached. It is a skeleton window procedure. It worked for me.No problems with menus or whatever.

Attachments
/* This source has been formatted by an unregistered SourceFormatX */
/* If you want to remove this info, please register this shareware */
/* Please visit http://www.textrush.com to get more information    */

#include "resource.h"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM );
LRESULT CALLBACK WindowProcedure2( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK ToolDlgProc( HWND hwnd, UINT Message, WPARAM wParam, LPARAM
  						lParam );


/*  Make the class name into a global variable  */
char szClassName[] = "WindowsApp";

HWND hDialog = NULL; //the handle for the dialog box

int WINAPI WinMain( HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR
   				lpszArgument, int nFunsterStil )

{
	HWND hwnd, hwnd2; /* This is the handle for our window */



	MSG messages; /* Here messages to the application are saved */
	WNDCLASSEX wincl; /* Data structure for the windowclass */

	/* The Window structure */



	wincl.hInstance = hThisInstance;
	wincl.lpszClassName = szClassName;
	wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
	wincl.style = CS_DBLCLKS; /* Catch double-clicks */
	wincl.cbSize = sizeof( WNDCLASSEX );

	/* Use default icon and mouse-pointer */
	wincl.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wincl.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
	wincl.hCursor = LoadCursor( NULL, IDC_ARROW );
	wincl.lpszMenuName = NULL; /* No menu */
	wincl.cbClsExtra = 0; /* No extra bytes after the window class */
	wincl.cbWndExtra = 0; /* structure or the window instance */
	/* Use Windows's default color as the background of the window */
	wincl.hbrBackground = ( HBRUSH )COLOR_BACKGROUND;


	/* Register the window class, and if it fails quit the program */
	if ( !RegisterClassEx( &wincl ))
	{
		return 0;
	}

	/* The class is registered, let's create the program*/
	hwnd = CreateWindowEx( 0,  /* Extended possibilites for variation */
	szClassName,  /* Classname */
	"Windows App",  /* Title Text */
	WS_OVERLAPPEDWINDOW,  /* default window */
	CW_USEDEFAULT,  /* Windows decides the position */
	CW_USEDEFAULT,  /* where the window ends up on the screen */
	544,  /* The programs width */
	375,  /* and height in pixels */
	HWND_DESKTOP,  /* The window is a child-window to desktop */
	NULL,  /* No menu */
	hThisInstance,  /* Program Instance handler */
	NULL /* No Window Creation data */
	 );

	wincl.lpszClassName = "wincl2";
	wincl.lpfnWndProc = WindowProcedure2; /* This function is called by windows */
	/* Register the window class, and if it fails quit the program */
	if ( !RegisterClassEx( &wincl ))
	{
		return 0;
	}
	/* The class is registered, let's create the program*/
	hwnd2 = CreateWindowEx( 0,  /* Extended possibilites for variation */
	"wincl2",  /* Classname */
	"Window No 2",  /* Title Text */
	WS_OVERLAPPEDWINDOW,  /* default window */
	CW_USEDEFAULT,  /* Windows decides the position */
	CW_USEDEFAULT,  /* where the window ends up on the screen */
	544,  /* The programs width */
	375,  /* and height in pixels */
	HWND_DESKTOP,  /* The window is a child-window to desktop */
	NULL,  /* No menu */
	hThisInstance,  /* Program Instance handler */
	NULL /* No Window Creation data */
	 );

	/* Make the window visible on the screen */
	ShowWindow( hwnd, nFunsterStil );
	ShowWindow( hwnd2, nFunsterStil );
	// MessageBox(hwnd, "hello world!", "Example - messagebox", MB_OK);

	/* Run the message loop. It will run until GetMessage() returns 0 */
	while ( GetMessage( &messages, NULL, 0, 0 ))
	{
		/* Translate virtual-key messages into character messages */
		TranslateMessage( &messages );
		/* Send message to WindowProcedure */
		DispatchMessage( &messages );
	}

	/* The program return-value is 0 - The value that PostQuitMessage() gave */
	return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure( HWND hwnd, UINT message, WPARAM wParam,
 								LPARAM lParam )
{
	switch ( message ) /* handle the messages */
	{

		case WM_LBUTTONDOWN:
			break;
		case WM_DESTROY:
			DestroyWindow( hDialog );
			PostQuitMessage( 0 ); /* send a WM_QUIT to the message queue */
			break;

		case WM_CREATE:
			{
				//creating the menu 
				HMENU hMenu, hSubMenu;
				hMenu = CreateMenu();
				hSubMenu = CreatePopupMenu();
				AppendMenu( hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit" );
				AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT )hSubMenu, "&File" );
				hSubMenu = CreatePopupMenu();
				AppendMenu( hSubMenu, MF_STRING, ID_STUFF_GO, "&Go" );

				AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT )hSubMenu, "&Stuff" );

				hSubMenu = CreatePopupMenu();
				AppendMenu( hSubMenu, MF_STRING, ID_STUFF_GO, "&About" );
				AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT )hSubMenu, "&Help" );
				SetMenu( hwnd, hMenu );



				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;



		case WM_COMMAND:

			switch ( LOWORD( wParam )) //contains the ID of the control that sent message
			{

			case ID_FILE_EXIT:
				PostMessage( hwnd, WM_CLOSE, 0, 0 );
				break;
			case ID_STUFF_GO:
				{
					LPCSTR s = "hello";
					MessageBox( GetActiveWindow(), s, s, MB_OK );
					break;
				}
			case ID_HELP_ABOUT:
				break;


			}

		default:
			 /* for messages that we don't deal with */
			return DefWindowProc( hwnd, message, wParam, lParam );
	}

	return 0;
}

/**************************************************************************
 *                                                                        *
 **************************************************************************/
LRESULT CALLBACK WindowProcedure2( HWND hwnd, UINT message, WPARAM wParam,
 								LPARAM lParam )
{
	switch ( message ) /* handle the messages */
	{

		case WM_DESTROY:
			DestroyWindow( hwnd );
			//PostQuitMessage( 0 ); /* send a WM_QUIT to the message queue */
			break;

		case WM_CREATE:
			{
				MessageBox( hwnd, "created windows no2", "Message", MB_OK);
			}
			break;
	default:
			 /* for messages that we don't deal with */
			return DefWindowProc( hwnd, message, wParam, lParam );
	}

	return 0;
}



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

/**************************************************************************
 *                                                                        *
 **************************************************************************/

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

Return Value

If the function succeeds, the return value is the handle to the dialog box.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Try using GetLastError() function.

Hey guys... thanks for the replies

Yesterday, I used the main.cpp that Wolfpack included in GUI.zip ... everything worked fine, and the dialog box popped up.

Today I had to reinstall dev-cpp ... I tried compiling and running the same main.cpp but the dialogbox won't show up (a NULL value is returned again).
Everything is the same, except that I had to create a new resource file, with the same information, but this resource file did compile successfully.

I used GetLastError() ... it printed the value 1813.

Code :1813
Error :The specified resource type cannot be found in the image file.
Const :ERROR_RESOURCE_TYPE_NOT_FOUND

ERROR_RESOURCE_TYPE_NOT_FOUND
1813 The specified resource type cannot be found in the image file.

One of the names you have used in the new resource file is different from the one you used yesterday.

ah, ok ... nevermind. Something in my project options got reset, and it was no longer including my .rc file in the current project. :o

Thanks again, guys. Much appreciated.

apcxpc, your 7 year old hint with the reset options helped my out in my 3 year old Dev-cpp project, which a tried to reactivate. Thanks a lot ! Jens

Edited 2 Years Ago by Jens_2

This article has been dead for over six months. Start a new discussion instead.