• Advertisement

Archived

This topic is now archived and is closed to further replies.

CreateWindowEx

This topic is 5513 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 can''t seem to solve this problem with the application I''m developing. When testing the return value of CreateWindowEx, it returns FALSE (well, !CreateWindowEx(...) returns TRUE, that is). Anyway, I test the error using GetLastError(), and the error message is "The operation completed successfully." Why is an error occurring here if it seems that there is no error? I think I had a similar (if not the same) problem before, and I think it had to do with the return values from the WinProc function, but I can''t remember what the problem was. The following is my code. Thanks for the help!
HINSTANCE hInst;
HWND hWnd;



HRESULT MakeWindow(HINSTANCE, UINT, UINT);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);



HRESULT MakeWindow(HINSTANCE hInstance, UINT nWidth, UINT nHeight)
{
	WNDCLASSEX window;
	
 	// Create the window''s class.
	window.cbSize           = sizeof(WNDCLASSEX);
	window.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	// CS_DBLCLKS
	window.lpfnWndProc	    = WinProc;
	window.cbClsExtra		= 0;
	window.cbWndExtra		= 0;
	window.hInstance		= hInstance;
	window.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	window.hCursor			= LoadCursor(NULL, IDC_ARROW);
	window.hbrBackground	= (HBRUSH) GetStockObject(BLACK_BRUSH);
	window.lpszMenuName		= NULL;
	window.lpszClassName	= WNDCLS_NAME;
	window.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	
	// Register the window''s class.
	if (!RegisterClassEx(&window))
		return (E_FAIL);
	
	// Create a window based upon the window class settings.
	if (!(hWnd = CreateWindowEx(WS_EX_TOPMOST,
								WNDCLS_NAME,
								"The Game of all Games",
								WS_POPUP | WS_VISIBLE,
								0, 0, nWidth, nHeight,
								NULL,
								NULL,
								hInstance,
								NULL)))
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
					  FORMAT_MESSAGE_IGNORE_INSERTS,
					  NULL,
					  GetLastError(),
					  0, // Default language
					  (LPTSTR) &lpMsgBuf,
					  0,
					  NULL);
		// Display the string.
		MessageBox(NULL, (LPCTSTR) lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION);
		// Free the buffer.
		LocalFree(lpMsgBuf);
		return (E_FAIL);
	}
	
	// Inform parent function that this function completed properly.
	return(S_OK);
}



int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;
	BOOL bRet;
	
//	DialogBox(hInstance, (LPCTSTR)IDD_MAIN, NULL, (DLGPROC)WndMain);
//	DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)WndMain);
	
	if (FAILED(MakeWindow(hInstance, 640, 480)))
		return 0;
	
/*	// Initialize Window Class.
	if (!RegWndClass(hInstance))
	{
		MessageBox(NULL, "Error registering Window Class.", "Error", NULL);
		return FALSE;
	}
	
	// Perform application initialization.
	if (!InitInstance(hInstance, nCmdShow))
	{
		MessageBox(NULL, "Error creating application window.", "Error", NULL);
		return FALSE;
	}*/
	
	MessageBox(NULL, "IT WORKS (#1)", "IT WORKS", NULL);
	
	// Main message loop:
	while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) 
	{
		if (bRet == -1)
		{
			// Handle Error and exit.
			MessageBox(NULL, "Unexpected error.", "Error", NULL);
			PostQuitMessage(FALSE);
			return FALSE;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	
	MessageBox(NULL, "IT WORKS (#2)", "IT WORKS", NULL);
	
	// Unregister the class from memory.
	UnregisterClass(WNDCLS_NAME, hInstance);
	
	return msg.wParam;
}



LRESULT CALLBACK WinProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	
	switch (msg) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
your WinProc function uses hDlg as the HWND param, but you use hWnd in your DefWindowProc calls. since it''s a global that has yet to be initialized during the all important WM_NCCREATE and WM_CREATE calls, your effectively sending a bad window handle to DefWindowProc. try changing hDlg to hWnd in the param list.

Share this post


Link to post
Share on other sites

  • Advertisement