// Game.cpp : Defines the entry point for the application.
//
#include "StdAfx.h"
#include "Resource.h"
//#include "Game.h"
#define CLASSNAME "WNDCLASS1"
HINSTANCE hInstance;
HWND hWndMain;
VOID MainGameLoop(VOID);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Defining WNDCLASS
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_GAMEAPP));
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = CLASSNAME;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
// Register WNDCLASS
if (!RegisterClass(&wndclass))
MessageBox(NULL,"Couldn't register WNDCLASS!!!","Error",MB_OK);
// Creating the main window
hWndMain = CreateWindow(CLASSNAME,"Klingis",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
// Showing the window
ShowWindow(hWndMain,SW_SHOW);
MainGameLoop();
return 0;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return 0;//DefWindowProc(hWnd,msg,wParam,lParam);
}
VOID MainGameLoop(VOID)
{
MSG msg;
BOOL running = TRUE;
while (running)
{
if (PeekMessage(&msg,hWndMain,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
running = FALSE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
Window doesn't show up
The window doesn't show up when I don't call
the default DefWindowProc at the end of my WindowProc.
Which is strange cos I've created it and showed it with:
hWndMain = CreateWindow(...); and
ShowWindow(hWndMain,SW_SHOW);
How can this come?
Well, you've answered your own question:
DefWindowProc does a lot of stuff. One of which is to correctly process WM_NCCREATE, the API documentation specifies the return value for the window procedure as:
So you're returning 0 (== FALSE) which stops window creation and causes CreateWindow to return NULL. So, no window, no way to see it.
Skizz
Quote:
The window doesn't show up when I don't call the default DefWindowProc
DefWindowProc does a lot of stuff. One of which is to correctly process WM_NCCREATE, the API documentation specifies the return value for the window procedure as:
Quote:
If an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle.
So you're returning 0 (== FALSE) which stops window creation and causes CreateWindow to return NULL. So, no window, no way to see it.
Skizz
Change:
To something like:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){ switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; default: break; } return 0;//DefWindowProc(hWnd,msg,wParam,lParam);}
To something like:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){ switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); } return 0;}
I removed the DefWindowProc(...) to see if it
handled the WM_PAINT cos the window seem to repainted it self
without me handling the WM_PAINT in my WindowProc(...);
handled the WM_PAINT cos the window seem to repainted it self
without me handling the WM_PAINT in my WindowProc(...);
Here's the API description for what DefWindowProc does with a WM_PAINT message:
and here's the description for WM_ERASEBKGND
So, if you've defined a class background brush in the WNDCLASSEX structure then that will be used to clear the window if you let DefWindowProc handle the WM_PAINT and WM_ERASEBKGND. Try it out by changing the brush value and see what happens.
Skizz
Quote:
The DefWindowProc function validates the update region. The function may also send the WM_NCPAINT message to the window procedure if the window frame must be painted and send the WM_ERASEBKGND message if the window background must be erased.
and here's the description for WM_ERASEBKGND
Quote:
The DefWindowProc function erases the background by using the class background brush specified by the hbrBackground member of the WNDCLASS structure.
So, if you've defined a class background brush in the WNDCLASSEX structure then that will be used to clear the window if you let DefWindowProc handle the WM_PAINT and WM_ERASEBKGND. Try it out by changing the brush value and see what happens.
Skizz
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement