Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VC10.0

CenterWindow in WIN32

4.00/5 (2 votes)
23 Sep 2011CPOL 12.3K  
void CenterWnd(HWND wnd){ RECT r,r1; GetWindowRect(wnd,&r); GetWindowRect(GetDesktopWindow(),&r1); MoveWindow(wnd,((r1.right-r1.left)-(r.right-r.left))/2, ((r1.bottom-r1.top)-(r.bottom-r.top))/2, (r.right-r.left), (r.bottom-r.top),0);}
C++
void CenterWnd(HWND wnd)
{
   RECT r,r1;
   GetWindowRect(wnd,&r);
   GetWindowRect(GetDesktopWindow(),&r1);
   MoveWindow(wnd,((r1.right-r1.left)-(r.right-r.left))/2,
      ((r1.bottom-r1.top)-(r.bottom-r.top))/2,
      (r.right-r.left), (r.bottom-r.top),0);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)