Sign in to follow this  
zee_ola05

[DX] Win32 Help

Recommended Posts

zee_ola05    334
[code]

#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);
}
[/code]


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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
According to the [url=http://msdn.microsoft.com/en-us/library/ms679360(v=vs.85).aspx]documentation for GetLastError()[/url], you can use FormatMessage() to get a string for the error code. Alternately you can simply examine the numerical value and compare it to [url=http://msdn.microsoft.com/en-us/library/ms681381(v=vs.85).aspx] the list of system error codes[/url]. Doing so will tell you which parameter CreateWindowEx() is having trouble with in your function call.

Share this post


Link to post
Share on other sites
zee_ola05    334
I tried the GetLastError() and used the function in the example here [url="http://msdn.microsoft.com/en-us/library/ms680582(v=vs.85).aspx"]http://msdn.microsoft.com/en-us/library/ms680582(v=vs.85).aspx[/url]

I added these:

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

And the messageBox said: [b]CreateWindowEx failed with error 1407: Cannot find window class[/b]
[b]
[/b]
Now how do I solve this error? T.T

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this