Help with Window Creation Please
This is my first try with program a game and i''m trying to get a window''s window to come up. I''m using MSVC++ 6.0, here is my code
#include "windows.h"
#include "windowsx.h"
#include "stdio.h"
#include "conio.h"
#define WIN32_LEAN_AND_MEAN
HWND hWnd=NULL;
HINSTANCE hInst;
LRESULT CALLBACK MsgHandler(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK MsgHandler(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
return(DefWindowProc(hWnd, msg, wparam, lparam));
}
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
WNDCLASSEX sampleClass; // declare structure variable
sampleClass.cbSize = sizeof(WNDCLASSEX); // always use this!
sampleClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // standard settings
sampleClass.lpfnWndProc = MsgHandler; // we need to write this!
sampleClass.cbClsExtra = NULL; // extra class info, not used
sampleClass.cbWndExtra = NULL; // extra window info, not used
sampleClass.hInstance = hInst; // parameter passed to WinMain()
sampleClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Windows logo
sampleClass.hCursor = LoadCursor(NULL, IDC_ARROW); // standard cursor
sampleClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // a simple black brush
sampleClass.lpszMenuName = NULL; // no menu
sampleClass.lpszClassName = "Sample Class"; // class name
sampleClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // Windows logo again
RegisterClassEx(&sampleClass);
if((hWnd = CreateWindowEx(NULL,"Sample Class","Kenan''s Program WOOT!!",WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,0,0,640,480,NULL,NULL,hInst,NULL)))
{
MessageBox(NULL,"Window Creation.","Success",MB_OK|MB_ICONEXCLAMATION);
}
return (0);
}
No window comes up but the message box does, can someone please tell me whats wrong with this? thank you
You''re never showing the window. Try:
ShowWindow(hWnd,SW_MAXIMIZE);
after you create the window.
You might also want to add WS_VISIBLE to the styles.
Didn''t read everything over, but those look like likely solutions to me.
-Arek the Absolute
ShowWindow(hWnd,SW_MAXIMIZE);
after you create the window.
You might also want to add WS_VISIBLE to the styles.
Didn''t read everything over, but those look like likely solutions to me.
-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Try adding WS_VISIBLE to your CreateWindowEx function
showing it would help also, it should look something like this:
HWND handleToTheWindow = CreateWindow( WndClassName, WndName, WS_POPUP | WS_VISIBLE,WndPosX, WndPosY, WndWidth, WndHeight,NULL, NULL, hInstance, NULL );
if( handleToTheWindow )
{
ShowWindow( handleToTheWindow, nCmdShow );
UpdateWindow( handleToTheWindow );
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
[edited by - zyroth on August 6, 2002 10:30:45 PM]
[edited by - zyroth on August 6, 2002 10:31:56 PM]
showing it would help also, it should look something like this:
HWND handleToTheWindow = CreateWindow( WndClassName, WndName, WS_POPUP | WS_VISIBLE,WndPosX, WndPosY, WndWidth, WndHeight,NULL, NULL, hInstance, NULL );
if( handleToTheWindow )
{
ShowWindow( handleToTheWindow, nCmdShow );
UpdateWindow( handleToTheWindow );
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
[edited by - zyroth on August 6, 2002 10:30:45 PM]
[edited by - zyroth on August 6, 2002 10:31:56 PM]
Hmm... from what I can tell by looking at the code, it''s all correct... except for one thing: you''re missing the message loop.
Basically what happens without it is the window is created then destroyed when the execution hits the return statement immediately following the message box.
There are about fifty bajillion ways of doing this, so try the Internet for some ways. However, if you ask nicely, I might provide the way I prefer to do it
And yes, make sure you have that call to ShowWindow() or the WS_VISIBLE flag set. Me, I like to be redundant and do both--I like to make sure that window is guaranteed to show up.
By the way, I’m probably being an idiot again, however, doesn’t that #define WIN32_LEAN_AND_MEAN need to be before the #include? Someone yell at me if I’m wrong.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To all of those who think I''m just being contradictory:
No I''m not!
Basically what happens without it is the window is created then destroyed when the execution hits the return statement immediately following the message box.
There are about fifty bajillion ways of doing this, so try the Internet for some ways. However, if you ask nicely, I might provide the way I prefer to do it
And yes, make sure you have that call to ShowWindow() or the WS_VISIBLE flag set. Me, I like to be redundant and do both--I like to make sure that window is guaranteed to show up.
By the way, I’m probably being an idiot again, however, doesn’t that #define WIN32_LEAN_AND_MEAN need to be before the #include? Someone yell at me if I’m wrong.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To all of those who think I''m just being contradictory:
No I''m not!
To all of those who think I'm just being contradictory:No I'm not!My First Game!!!"
Yes, #define WIN32_LEAN_AND_MEAN should come before #include <windows.h>.
The problems, to summarize what other have said, are:
-You are not showing the window using ShowWindow(SW_SHOW);
-You have no message loop. It will look like this:
(pseudocode)
while (GetMessage)
{
TranslateMessage
DispatchMessage
}
Look up those 3 functions (GetMessage, TranslateMessage, DispatchMessage) to get the parameters, and add that code directly before returning from WinMain.
The problems, to summarize what other have said, are:
-You are not showing the window using ShowWindow(SW_SHOW);
-You have no message loop. It will look like this:
(pseudocode)
while (GetMessage)
{
TranslateMessage
DispatchMessage
}
Look up those 3 functions (GetMessage, TranslateMessage, DispatchMessage) to get the parameters, and add that code directly before returning from WinMain.
Don''t forget PeekMessage()--That''s the one you''ll ultimately want to use if you''re making games.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To all of those who think I''m just being contradictory:
No I''m not!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To all of those who think I''m just being contradictory:
No I''m not!
To all of those who think I'm just being contradictory:No I'm not!My First Game!!!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement