#include<windows.h>
#define WINDOW_TITLE "Creating a Window"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
ValidateRect(hWnd, 0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadIcon(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the class
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(WINDOW_TITLE, WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, hInstance, 0);
// adjust to acctual window size
RECT rect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), false);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER|SWP_NOMOVE);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Render here
}
}
return 0;
}
Window Not Displaying
Hello I am doing the tutorials at C-Unit for DirectX 9 until my book I am ordering arrives. Problems is that the Win32 tutorial window is not displaying. Not shure why to me the Win32 code looks fine. Maybe I am missing something.
Hello,
A few list of things that will help you:
1) some functions returns a value (for example, CreateWindow() returns a HWND); this value can be set to a particular value (in this case: NULL) to tell you 'something bad happened'. Therefore, it is good to have a look to these return values, because it really help when you have to spot an error.
2) the first argument of CreateWindow() is a window class name, not a window title. In your case, it should be "MyWindowClass".
3) wcex.hCursor should be set to the result of LoadCursor(), not LoadIcon(). In your case, the result is rather fun but is probably not intended :)
HTH,
A few list of things that will help you:
1) some functions returns a value (for example, CreateWindow() returns a HWND); this value can be set to a particular value (in this case: NULL) to tell you 'something bad happened'. Therefore, it is good to have a look to these return values, because it really help when you have to spot an error.
2) the first argument of CreateWindow() is a window class name, not a window title. In your case, it should be "MyWindowClass".
3) wcex.hCursor should be set to the result of LoadCursor(), not LoadIcon(). In your case, the result is rather fun but is probably not intended :)
HTH,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement