[DX] Win32 Help

Started by
7 comments, last by zee_ola05 13 years ago


#include <windows.h>


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);

/*
hInstance - identifier of the "instance"/application
prevInstance - obsolete, not important
lpCmdLine - string pointer, like argv in C (getting cmdline parameters)
showCmd - not important in games
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc; //fill up this struct and then Register

ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc; //WindowProc() is a function that handles messages (e.g. mouse and keyboard inputs)
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "Window Class 1";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"Window Class 1 - CreateWindowEx",
"Window Name 1",
WS_OVERLAPPEDWINDOW,
300,
300,
500,
400,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);

MSG msg;

while(true)
{
//Check if there is a message in the message queue. If none, continue with the game code, else process
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

/*
Run Game Code Here
*/
}

return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
//close application
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

//Hnadle any statement the switch-case didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}



I have this program written using MS Visual C++ 2010. When I debug it (F5), there is no error and it runs. BUT it does not show anything. I'm expecting a blank window. Can anyone help me identifying what is wrong with this code? Thanks!

I'm following a DirectX tutorial, BTW.
Advertisement
When I run I get this [media]http://img19.imageshack.us/img19/8185/erroryx.png[/media]
You should check function return values for errors. Most Windows functions will indicate success or failure, and if they fail you can call GetLastError() to find out what the exact error is. In particular your CreateWindowEx() call is failing.
Thank you. Can you tell me what in particular is wrong with CreateWindowEx()?

And if I use the GetLastError(), How will I display the error?
According to the documentation for GetLastError(), you can use FormatMessage() to get a string for the error code. Alternately you can simply examine the numerical value and compare it to the list of system error codes. Doing so will tell you which parameter CreateWindowEx() is having trouble with in your function call.
I tried the GetLastError() and used the function in the example here http://msdn.microsoft.com/en-us/library/ms680582(v=vs.85).aspx

I added these:

if(!hWnd)
ErrorExit(TEXT("CreateWindowEx"));

And the messageBox said: CreateWindowEx failed with error 1407: Cannot find window class


Now how do I solve this error? T.T
It didn't tell what parameter was invalid.
Take a look at the CreateWindowEx() call. Which parameter is the window class parameter? Now compare that value to the name of the window class you registered.
I'm sorry. Thank you. :)

This topic is closed to new replies.

Advertisement