I am new to windows programming so I though i would start with the basics, I just wanted to create a 800 x 600 window. The code compiles and runs with now problems but I never get to see the window as it closes straight away. Can anyone see what I have done wrong?
[code]
standard.h
/* * include file for standard system include files * */
//#pragma once // Specifies that the file will be included only once by the compiler in a build.
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows Headers
// Windows Header Files
#include // C RunTime Header Files
#include #include #include #include #include [/code]
The WinMain part
[code]
// Main Program Entry point
#include "standard.h"
#include "setupWin.h"
bool setupWindow( HINSTANCE hInstance);
INT WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
if (!setupWindow(hInstance))
return false;
// Main message loop:
MSG mssg;
PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE);
// run till completed
while (mssg.message!=WM_QUIT)
{
// is there a message to process?
if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE))
{
// dispatch the message
TranslateMessage(&mssg);
DispatchMessage(&mssg);
}
}
}
[/code]
The window set up
[code]
/* Setup the window */
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HWND wndHandle;
bool setupWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = (HBRUSH) ( COLOR_WINDOW+1 );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "Dx9 Framework";
wcex.hIcon = 0;
RegisterClassEx(&wcex);
wndHandle = CreateWindow(
"Dx9 Framework",
"Dx9 Framework",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
return false;
ShowWindow(wndHandle, SW_SHOWDEFAULT);
UpdateWindow(wndHandle);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
[/code]
Thanks for looking
S
Comments
: I am new to windows programming so I though i would start with the basics, I just wanted to create a 800 x 600 window. The code compiles and runs with now problems but I never get to see the window as it closes straight away. Can anyone see what I have done wrong?
[blue]
I didnt look through all the code, but did notice this..[/blue]
: [code]
: // Main message loop:
: MSG mssg;
:
[red]
[b]// just look for a single message??[/b]
PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE);
[/red]
: // run till completed
: while (mssg.message!=WM_QUIT)
: {
[red][b]// Infinity Loop![/b][/red]
: }
: }
:
: [/code]
[blue]
Im surprised it didnt crash, actually. Here you first
looked (and removed) a single message. Then it goes into
an [b]infinity loop[/b] because you never check any new
messages in the [green]while()[/green] (That is, mssg
will [b]never[/b] change, hence wont be WM_QUIT.)
This should be..[/blue][code]
// init code..
while (true)
{
MSG mssg;
if (PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE))
{
if (mssg.message==WM_QUIT)
break; // break out of loop and quit!
// handle message
}
}[/code][blue]
Good Luck!
~mt2002[/blue]
[hr][italic]"There are ten kinds of people in the world--Those who speak binary, and those who dont!"[/italic]