Archived

This topic is now archived and is closed to further replies.

gamesmaster2

This window is stuck

Recommended Posts

gamesmaster2    122
Well I wanted to see how a windows program was put together.So I got windows programming for dummies.I have DEV C++ because for some reason the win32 programs weren''t working on the learners edition of VisualC++.When anyone I placed the code in below but it didn''t work and I''ve been looking over the code in the book to see what I did wrong but I can''t find anything yet.Maybe a second opinion is in order.I know it''s a bit long but please if anyone can find where I went wrong please tell me. //This code makes a 320 x 200 window //places it on the screen and allows //you to move it around //Includes #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <stdio.h> #include <math.h> //Defines for Windows #define WINDOW_CLASS_NAME "WINCLASS1" //GLOBALS HWND main_window_handle = NULL; //Save the Window Handle //FUNCTIONS LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg, WPARAM wparam,LPARAM lparam) { //this is the main message handler of the system PAINTSTRUCT ps; //used in WM_PAINT HDC hdc; //handle to device context //find out what the message is switch(msg) { case WM_CREATE; //called when a window is created { //do initialization stuff here return(0); }break; case WM_PAINT; //called when a window needs painting { //simply validate the window hdc = BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps); return(0); }break; case WM_DESTROY//called when a window is killed { //kill the application PostQuitMessage(0); return(0); }break; default:break; } //end switch //process any messages that you didn''t take care of return(DefWindowProc(hwnd,msg,wparam,lparam)); }//end winProc //WINMAIN int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASS winclass; //this will hold the class you create HWND hwnd; //generic window handle MSG msg; //generic message //first fill in the window class structure winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL,IDC+ARROW); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; //register the window class if(!RegisterClass(&winclass)) return(0); //Create the Window if(!hwnd = CreateWindow(WINDOW_CLASS_NAME, //class "Hello Dwayne", //title WS_OVERLAPPEDWINDOW | WS_VISIBLE, //flags 0,0, //x,y 320,200, //width and height of screen NULL, //handle to parent NULL, //handle to menu hinstance, //instance NULL))) //Creation Parameters return(0); //Save the window handle in a global main_window_handle = hwnd; //enter main event loop while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { //Test whether this is a quit if (msg,message == WM_QUIT)break; //translate any accelerator keys TranslateMessage(&msg); //send the message to the windowproc DispatchMessage(&msg); }//end if //Main Game Processing goes Here }//end while //return to Windows like this return (msg.wParam); }//end WinMain WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) { return 0; } The road to hell is paved in good intentions

Share this post


Link to post
Share on other sites
MonkeyChuff    158
First off it would be nice if you posted code that didn't contain loads of syntatic errors!

Your code is failing at the CreateWindow statement, due to you not seting the brush color in your WNDCLASS struct.

your code (without errors) should look like this.

    
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>


#define WINDOW_CLASS_NAME "WINCLASS1"
HWND main_window_handle = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(msg)
{
case WM_CREATE:
{
return(0);
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return(0);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
{
break;
}
}

return(DefWindowProc(hwnd,msg,wparam,lparam));
}

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
WNDCLASS winclass;
HWND hwnd;
MSG msg;

winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

if(!RegisterClass(&winclass))
return(0);

hwnd = CreateWindow(WINDOW_CLASS_NAME,"Hello Dwayne", WS_OVERLAPPEDWINDOW | WS_VISIBLE,0,0,320,200,NULL,NULL,hinstance,NULL);
if(hwnd == NULL)
{
return(0);
}

main_window_handle = hwnd;

while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (msg.wParam);
}


Edited by - MonkeyChuff on February 8, 2002 3:46:31 AM

Share this post


Link to post
Share on other sites
gamesmaster2    122
Thanks alot.But I''m still wondering what exactly I did wrong.
only thing I saw was that I forgot to put BLACK_BRUSH???

I understand being a little peeved by syntax errors but isn''t that what this forum is about.Learning things like that from others which are further along than you??

I don''t know everything yet.Hell I''m no where close.But I do have drive and I think that counts for something.

The road to hell is paved in good intentions

Share this post


Link to post
Share on other sites
MonkeyChuff    158
quote:
Original post by gamesmaster2
Thanks alot.But I''m still wondering what exactly I did wrong.
only thing I saw was that I forgot to put BLACK_BRUSH???

I understand being a little peeved by syntax errors but isn''t that what this forum is about.Learning things like that from others which are further along than you??

I don''t know everything yet.Hell I''m no where close.But I do have drive and I think that counts for something.

The road to hell is paved in good intentions


I agree that this is a great place for people to learn new things from, but surely it is easier for newbs to learn from code that will actually compile, rather than have to work through errors which they may not know how to fix.

But to add to my previous post ...

The WNDCLASS structure is created with the members all uninitialised (check it out in the debugger). The members such as hInstance, hIcon, hCursor and hbrBackground are set to values such as 0xcccccccc. You then assign valid values to your members, except hbrBackground, which remains pointing to an invalid value. When CreateWindow trys to read the hbrBackground member, to find out which colour to paint the background with, it fails due to hbrBackground not being set.

Hope that makes it a bit clearer.

you could also have set hbrBackground to NULL, and then handled the painting to the windows in the WM_ERASEBKGND message.

Share this post


Link to post
Share on other sites