Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Trouble creating dialog

This topic is 4566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble creating and showing a dialog window (identified by IDD_SLIDERDIALOG). So far, I have a simple skeleton Windows program with the addition of creating and displaying a modeless dialog. The function CreateDialog() returns a NULL and GetLastError() returns 1407, or "Cannot find window class." I've searched the entire program, but could not find why this problem might occur. The handle to the main application window is not NULL and neither is the handle to the application instance at the time of the CreateDialog() call. Here is the source code to the entire program:
#undef UNICODE

#include "resource.h"

#include <vector>
#include <windows.h>

LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
void RegisterWindowClass( );
void CreateAppWindow( );
WPARAM StartMessageLoop();
BOOL CALLBACK SliderDialogProc( HWND hDlgWnd, UINT message, WPARAM wParam, LPARAM lParam );

HWND g_hWnd;
HWND g_hDlgWnd;
HINSTANCE g_hInstance;

//////////////////////
// WinMain()
//////////////////////
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, INT )
{
	g_hInstance = hInstance;

	RegisterWindowClass(  );
	CreateAppWindow( );
	ShowWindow( g_hWnd, SW_SHOWDEFAULT );
	UpdateWindow( g_hWnd );
	INT result = StartMessageLoop();
	return result;
}

//////////////////////
// WndProc()
//////////////////////
LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
                // Create the dialog when any key is pressed
		case WM_CHAR:
			{
				if( !IsWindow( g_hDlgWnd ) )
				{
					g_hDlgWnd = ::CreateDialogA( g_hInstance, MAKEINTRESOURCE( IDD_SLIDERDIALOG ),
						hWnd, (DLGPROC)SliderDialogProc );
					DWORD err = GetLastError();
					::ShowWindow( g_hDlgWnd , SW_SHOW );
				}
			}
			return 0;
		case WM_DESTROY:
			PostQuitMessage( 0 );
			return 0;
		case WM_PAINT:
			ValidateRect( g_hWnd, NULL );
			return 0;
		case WM_KEYDOWN:
			switch( wParam )
			{
				case VK_ESCAPE:
					PostQuitMessage( 0 );
					break;
			}
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

//////////////////////
// RegisterWindowClass()
//////////////////////
void RegisterWindowClass( )
{
	WNDCLASSEX wc;
	wc.cbSize = sizeof( WNDCLASSEX );
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = g_hInstance;
	wc.hIcon =  LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = (HCURSOR)LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "App";
	wc.hIconSm = NULL;

	RegisterClassEx( &wc );
}

//////////////////////
// CreateAppWindow()
//////////////////////
void CreateAppWindow( )
{
	g_hWnd = CreateWindowEx(
		NULL,
		"App",
		"App",
		WS_OVERLAPPEDWINDOW,
		100,
		100,
		600,
		500,
		GetDesktopWindow(),
		NULL,
		g_hInstance,
		NULL );
}

//////////////////////
// StartMessageLoop()
//////////////////////
WPARAM StartMessageLoop()
{
	MSG msg;
	while(1)
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if( msg.message == WM_QUIT )
				break;

			if( !IsWindow( g_hDlgWnd ) || !IsDialogMessage( g_hDlgWnd, &msg ) )
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		}
	}
	return msg.wParam;
}

BOOL CALLBACK SliderDialogProc( HWND hDlgWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	switch( message )
	{
		case WM_INITDIALOG:
			{
				// initialize slider values
				//
			}
			return TRUE;
		
	}

	return FALSE;
}



Any ideas as to what's wrong?

Share this post


Link to post
Share on other sites
Advertisement
Are you using any common controls on the dialog? Try calling InitCommonControls before calling CreateDialog. This loads and registeres all of the extenden common controls (sliders for example). This might also be the case with an ActiceX control if it isn't properly registered.

Another nitpick: As soon as you've got a valid g_hDlgWnd your message loop will fail to deliver any windows message because of your IsWindow check.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!