Archived

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

Why does the window destroy?

This topic is 6580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It's the return 0; after your CreateWindow() call. This kicks you out ow WinMain(). You probably want

if(hwnd==NULL){return 0;} //with possible error message

Also, in your WinProc() procedure, you don't need to use break; after those return statements, since that'll do the same thing and kick you out of WinProc()

Its generally considered bad programming practice to return from a procedure before the end, but in windows that leads to convoluted logic so that's one area where its better to break the rules.

Share this post


Link to post
Share on other sites
Hi, below is source code that simply creates and displays a window. The problem is that right after it displys it destroys its self. Tat is wrong, wrong, wrong. I don't want it destroy it self, but I cannot figure out why it is doing so. Suggestions?

--------------------------------------------
//includes and defines
#define WIN32_LEAN_AND_MEAN
#include
#include

HWND main_window_handle = NULL;


//windproc function
LRESULT CALLBACK WindowProc(HWND hwnd, //the window
UINT msg, //the message itself
WPARAM wParam,
LPARAM lParam)
{
//this function handles all the messages sent to window

HDC hdc; //device context used for graphics
PAINTSTRUCT ps; //also used for graphics

//find out what message was sent
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);
return 0;
}break;

default:break;
}

return (DefWindowProc(hwnd, msg, wParam, lParam));

}

//winmain entry point
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpCmdLine,
int nShowCmd)
{

//declaration of window class
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hinstance; //from winmain
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "WINCLASS1";

//register wndclass
if (RegisterClass(&wndclass)==0)
{
//error
}


//create windows

hwnd = CreateWindow("WINCLASS1",
"Blind Try",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
320, 200,
NULL,
NULL,
hinstance,
NULL);
return 0;

//holdsthe message

main_window_handle = hwnd;

//loop unitl there is a WM_QUIT
while(1)
{
//is there a message
if (PeekMessage(&msg, NULL, 0, 0 , PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

//translate message
TranslateMessage(&msg);

//sends message to Window proc
DispatchMessage(&msg);
}
}

return (msg.wParam);

}

-------------------------------

Share this post


Link to post
Share on other sites