Public Group

Archived

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

Win32 API: Centering the window?

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

Recommended Posts

Is there any trick on doing this?

Share on other sites
The way I'd imagine doing it is calling int x = GetDeviceCaps(hdc,HORZRES);

then I'd call GetWindowRect(hwnd,&rect);

Subtract rect.right from x and divide by two and that'll be your x coordinate in CreateWindow()

Follow the same procedure for vertical centering where instead of HORZRES for GetDeviceCaps() use VERTRES. GetWindowRect() will of course fill the rect struct with the width and height values, so it'll be called once only.

EDIT: The structure would be defined simply as RECT rect....also for vertical centering, it'll be rect.bottom that will give the height.

[edited by - nervo on May 31, 2004 7:18:19 PM]

Share on other sites
Hmm I''ll try that hehe. I''m usually a full source code learner... aka seeing the whole code for the window & the features that we''re discussing now. If you don''t want to try that, I''ll continue to get this working. Thanks

Share on other sites
I guess if you want to have it centered by the time you call CreateWindow() then right before that just call GetDeviceCaps() to take the dimensions of the screen and subtract the window sizes from those and use the results to the call in CreateWindow(). Basically do all those steps right before CreateWindow(). CreateWindow() is often done in WinMain() before the message loop is written (the part with GetMessage())...When you start getting all gutsy with win32 you'll find out that you can do CreateWindow() anywhere you want just about.

I'm not sure how much you've already learned though, but I imagine you've already been able to make a simple window on the screen with the API, right? If so, then centering the window like I outlined would simply be a cinch for you. Hope it helps.

[edited by - nervo on May 31, 2004 7:42:43 PM]

Share on other sites
I''m lost. I''ll see what I can do. Btw, I can make a blank window, menues, GDI, message boxes now, logfonts, lines & other shapes. Most of them were in full source code in tutorials like functionx.com/win32 that really helped me out.

Share on other sites
void CenterWindow(HWND hWnd){	RECT rect;	GetWindowRect(hWnd,&rect);	int xSize = rect.right - rect.left;	int ySize = rect.bottom - rect.top;	int x = (GetSystemMetrics(SM_CXSCREEN) - xSize) / 2;	int y = (GetSystemMetrics(SM_CYSCREEN) - ySize) / 2;	SetWindowPos(hWnd,0,x,y,0,0,SWP_NOZORDER|SWP_NOSIZE);}

My Site

Share on other sites
EDIT: I got it going I just put it under WM_CREATE and it worked. Thanks again.

[edited by - philvaira on May 31, 2004 8:08:40 PM]

1. 1
2. 2
3. 3
Rutin
24
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631766
• Total Posts
3002232
×