Menu

Parameter to center window on desktop

2002-12-15
2012-09-26
  • Nobody/Anonymous

    Is there any parameter i can set at CreateWindow to make my MainWindow always show at the center of the Desktop?

     
    • Nobody/Anonymous

      The simple answer is no but it is quite easy to do. You can refer to the MSDN website to find info on functions.

      http://msdn.microsoft.com/

      I have a small routine which will centre a window on your screen:

         void CentreWnd()
         {
            RECT  rc2, rc1;
            RECT  FAR *lprc;
            int   nWidth, nHeight, cxCenter, cyCenter;
            HWND  hParentWnd;

            // Set the hParentWnd variable. If no window exists then the parent is the desktop.
            hParentWnd = GetParent();
            if (!hParentWnd)
               hParentWnd = ::GetDesktopWindow();
         
            ::GetWindowRect(hParentWnd, &rc2);
            lprc = (RECT*)&rc2;
       
            cxCenter = lprc->left+((lprc->right-lprc->left)/2);
            cyCenter = lprc->top+((lprc->bottom-lprc->top)/2);

            GetWindowRect(rc1);
            nWidth  = rc1.right-rc1.left; 
            nHeight = rc1.bottom-rc1.top;
         
            Move(cxCenter-(nWidth/2), cyCenter-(nHeight/2), nWidth, nHeight);
            return;   
         }

      I have this function built into a class which manages all Win32 API calls which manage actions on windows.

      BlakJak  :]

       
      • Nobody/Anonymous

        Sorry - as this is it won't work as you need to modify the Move line to:

        ::MoveWindow(_hwnd, x, y, width, height, TRUE);

        where _hwnd is the handle to the window you wish to centre.

        note that in my class _hwnd is the member variable.

        BlakJak :]

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.