Jump to content
  • Advertisement

Archived

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

Kaalot

WNDCLASSEX declaration

This topic is 5419 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

Hello! I''ve been spending some time getting familiar with Win32 programming, and much of that involves creating windows over and over until it''s second nature. Last night I began to throw together another window program. For this exercise, I simply created one source file, and put everything in there. Normally, I use classes, inheritance, constructors, etc. (in an attempt to be fancy), but for this last one, just a WinMain() function and a message handler. All window creation stuff was written directly into WinMain. I was having trouble with this, until I moved the WNDCLASSEX declaration outside of WinMain. Declaration outside, window appears. Declaration inside, no window. I was just wondering if anybody might be able to help me with the logic of this. Why does the WNDCLASS(EX) structure need to be declared outside of WinMain? Again, this is assuming one source file, and no special classes. TIA! -K

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You probably didn''t memset your WNDCLASSEX struct to zero. Global variables are autoinitialized to zero while local ones can contain any crap until you initialize them.

Share this post


Link to post
Share on other sites
What are you talking about? Global variables are not initialized to zero. Unless you mean he left out a memset() during a copy-paste operation, which I think is what happened.

Share this post


Link to post
Share on other sites
You do *not* need to zeromemory your WNDCLASSEX struct inside of WinMain. I know this because I''m looking at an app that doesn''t do it right now. Normally you should, but if you are filling everything in, it will mostly take care of itself (although you may get some packing issues, I never have).

So, if you post your code, we can help you more (know you''re at work now).


P.S. - For some reason, a lot of people seem to forget that you need to tell WNDCLASSEX how big it is (cbSize).

Share this post


Link to post
Share on other sites
Thanks everyone for your thoughts, my inet is completely down at home, so I brought my box to work today

Yes, memset() did the trick. I also assumed that if I was specifically filling in all of the properties of my window class that I wouldn''t have to worry about initialization, but that is apparently not the case. Code follows:



int APIENTRY WinMain(HINSTANCE inst1, HINSTANCE, LPSTR, INT nShowCmd)
{

MSG msg1;
HWND handle1;
WNDCLASSEX wndcls1;


wndcls1.cbSize = sizeof(WNDCLASSEX);
wndcls1.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls1.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcls1.hIcon = LoadIcon(NULL, IDI_HAND);
wndcls1.hInstance = inst1;
wndcls1.lpszClassName = "menu2";
wndcls1.lpszMenuName = NULL;
wndcls1.lpfnWndProc = proc1;
wndcls1.style = CS_HREDRAW|CS_VREDRAW;

RegisterClassEx(&wndcls1);

handle1 = CreateWindowEx(WS_EX_APPWINDOW, "menu2", "menu2", WS_OVERLAPPEDWINDOW, 50, 50, 200, 100, NULL, NULL, inst1, NULL);

ShowWindow(handle1, SW_SHOW);






while(1)
{
if(PeekMessage(&msg1, handle1, NULL, NULL, PM_REMOVE))
{
if(msg1.message == WM_QUIT)
break;

else
{
TranslateMessage(&msg1);
DispatchMessage(&msg1);
}

}

}

return 1;

}



The preceding code compiles and runs, but never displays any window. Moving the WNDCLASSEX declaration outside WinMain does the trick, as does adding

memset(wndcls1,0,sizeof(WNDCLASSEX));



For clarification''s sake, here is my wndproc, which is the only other piece of this program...

LRESULT CALLBACK proc1(HWND han1, UINT msg, WPARAM wParam, LPARAM lParam)
{

switch(msg)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(han1, msg, wParam, lParam);
}



Share this post


Link to post
Share on other sites
quote:
Original post by Kaalot
Yes, memset() did the trick. I also assumed that if I was specifically filling in all of the properties of my window class that I wouldn''t have to worry about initialization, but that is apparently not the case. Code follows:


Well, if you had actually filled in all the members of the struct you wouldn''t have had to worry about it, but you seem to have missed intializing hIconSm, cbClsExtra and cbWndExtra.

Share this post


Link to post
Share on other sites
Aargh! In my own defense, I had set the two "Extra" properties to 0 in an attempt to make it work. And, as is the way with code, it worked its way in and out of my source and happened to be out when I posted it this morning.

But you nailed me on the hIconSm. For some reason, I was under the impression that the hIconSm would default to whatever hIcon was set to. Looks like I was wrong, thank you for pointing that out. As soon as I went back and loaded up an Icon resource specifically for hIconSm (and replaced cbClsExtra and cbWndExtra), the program runs and displays a window.

Again, thank you! Does WNDCLASSEX have a constructor that takes all the members of the structure as parameters? So I don''t forget again? :D

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!