Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,537 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,526 people online right now. Registration is fast and FREE... Join Now!




Centering the Window position?

 
Reply to this topicStart new topic

Centering the Window position?

skyHigh
29 Oct, 2007 - 04:03 PM
Post #1

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
I'm using WinAPI and trying to center the new create window position.

CODE

RECT rc;
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
GetWindowRect(hwnd, &rc);
SetWindowPos(hwnd, 0, (screenWidth - rc.right)/2,
    (screenHeight - rc.bottom)/2, 0, 0, SWP_NOZORDER|SWP_NOSIZE);


from the code above, it works because it puts the window at the center of the screen, but not until I keep execute my code repeatedly over and over, the window position starts to shift little by little until it reaches the edge of the screen then it comes back to put the window at the center of the screen. If I excute my code again, it will shift again... how can I prevent it from shifting after executing the code for the second, third or fourth time?

This post has been edited by skyHigh: 29 Oct, 2007 - 04:03 PM
User is offlineProfile CardPM
+Quote Post

csmanoj
RE: Centering The Window Position?
30 Oct, 2007 - 02:00 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Aug, 2007
Posts: 142



Thanked: 3 times
Dream Kudos: 50
My Contributions
rc.right is the right edge of the rectangle counting from the left edge of the screen and not the left edge of the rectangle rc. So the width of the rectangle rc would be (rc.right - rc.left). Similarly the height would be (rc.bottom - rc.top). Substitute these values in the place of rc.right and rc.bottom respectively in your code.
User is offlineProfile CardPM
+Quote Post

skyHigh
RE: Centering The Window Position?
30 Oct, 2007 - 07:42 AM
Post #3

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
QUOTE(csmanoj @ 30 Oct, 2007 - 03:00 AM) *

rc.right is the right edge of the rectangle counting from the left edge of the screen and not the left edge of the rectangle rc. So the width of the rectangle rc would be (rc.right - rc.left). Similarly the height would be (rc.bottom - rc.top). Substitute these values in the place of rc.right and rc.bottom respectively in your code.



The formula above works because it puts the Window at the center position; the only thing I have problem with is when executing for the second time, the position starts to shift little. Then if I execute the code again, it will continue to shift.
User is offlineProfile CardPM
+Quote Post

csmanoj
RE: Centering The Window Position?
30 Oct, 2007 - 10:47 AM
Post #4

D.I.C Head
Group Icon

Joined: 6 Aug, 2007
Posts: 142



Thanked: 3 times
Dream Kudos: 50
My Contributions
did you even try what i suggested? it works perfect for me
User is offlineProfile CardPM
+Quote Post

skyHigh
RE: Centering The Window Position?
30 Oct, 2007 - 12:52 PM
Post #5

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
QUOTE(csmanoj @ 30 Oct, 2007 - 03:00 AM) *

rc.right is the right edge of the rectangle counting from the left edge of the screen and not the left edge of the rectangle rc. So the width of the rectangle rc would be (rc.right - rc.left). Similarly the height would be (rc.bottom - rc.top). Substitute these values in the place of rc.right and rc.bottom respectively in your code.


Yep I tried it, but seems don't work. What values do you use for your CreateWindow() of the x,y, nWidth and nHeight
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month