Advertisement

Resizing a window

Started by September 11, 2000 05:48 AM
2 comments, last by Cygon 24 years, 3 months ago
I'm trying to resize a normal window I created using CreateWindowEx() so the client area gets a special size (like 640x480). I know that I have to make the entire window a bit bigger than my desired size because the width of the title bar and window borders take away some place, making the client area smaller. How can I calculate the size needed for the entire window ? I've tried this code:
    
   RECT  l_r;
//

   l_r.left   = 0;
   l_r.right  = m_nWidth;
   l_r.top    = 0;
   l_r.bottom = m_nHeight;
   AdjustWindowRectEx(&l_r,
                      GetWindowLong(m_hWnd, GWL_STYLE),
                      GetWindowLong(m_hWnd, GWL_EXSTYLE),
                      GetMenu(m_hWnd) != 0);
//

   SetWindowPos(m_hWnd,
                HWND_TOP,
                l_r.left,
                l_r.top,
                l_r.right,
                l_r.bottom,
                SWP_DRAWFRAME);
//

   GetClientRect(m_hWnd, &l_r);
   m_nWidth  = l_r.right;
   m_nHeight = l_r.bottom;
    
AdjustWindowRect() only adds 3 pixels to the width and height. Not only is this wrong (it should add 6 pixels), it also doesn't take the window's title bar into account. What's the correct way to obtain the required window size so the client area gets 640x480 (or any other value I'd like) ? The function should work with and without menu bars if possible. -Markus- Edited by - Cygon on 9/11/00 5:50:06 AM
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
The method I use does the following:

* Width and Height are the new dimensions you want.

      RECT  WndRect, ClientRect;  HMENU hMenu;  // Remove any menu first - m_hWnd is app hWnd  hMenu = GetMenu(m_hWnd);  SetMenu(m_hWnd, NULL);  // Get the Window and Client sizes to work with  GetWindowRect(m_hWnd, &WndRect);  GetClientRect(m_hWnd, &ClientRect);  // Calculate the new size  WndRect.right  += (Width - ClientRect.right);  WndRect.bottom += (Height - ClientRect.bottom);  // Offset if there is a menu  if(hMenu != NULL)    WndRect.bottom += GetSystemMetrics(SM_CYMENU);  // Resize the window  MoveWindow(m_hWnd,WndRect.left,WndRect.top,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top,TRUE);  // Reset the menu  SetMenu(m_hWnd, hMenu);    



The reason you turn off the menu is that is the original window is small enough, it may have two lines of menu instead of just the one. If you resize to a window that is small enough to have two lines, then you''d have to adjust the above.


Jim Adams

Advertisement
Thanks, after playing a bit with your code I found what I''ve done wrong
I''m working on adding the menu size correction just now :D

If anyone is interested, here''s my code (resizes a window so its client area has the size p_nWidth x p_nHeight pixels).
     void CD3DApp::Resize(int p_nWidth, int p_nHeight) {   RECT  l_rt;   //   // Obtain current size   if(!GetWindowRect(m_hWnd, &l_rt))     throw "CD3DApp::Resize() - Error: GetWindowRect() failed\n";   //   l_rt.right  = l_rt.left + p_nWidth;   l_rt.bottom = l_rt.top + p_nHeight;   //   // Calculate the required window size needed for a client area sized width x height   if(!AdjustWindowRectEx(&l_rt, GetWindowLong(m_hWnd, GWL_STYLE),                          GetMenu(m_hWnd) != 0,                          GetWindowLong(m_hWnd, GWL_EXSTYLE)))     throw "CD3DApp::Resize() - Error: AdjustWindowRectEct() failed\n";   //   if(l_rt.left < 0) {     l_rt.right -= l_rt.left;     l_rt.left = 0;   }   if(l_rt.top < 0) {     l_rt.bottom -= l_rt.top;     l_rt.top = 0;   }   //   // Set new window position and size   if(!SetWindowPos(m_hWnd, HWND_TOP,                    l_rt.left,                    l_rt.top,                    l_rt.right - l_rt.left,                    l_rt.bottom - l_rt.top,                    SWP_DRAWFRAME))     throw "CD3DApp::Resize() - Error: SetWindowPos() failed\n";   //   GetClientRect(m_hWnd, &l_rt);                         // Obtain the width and height   m_nWidth  = l_rt.right - l_rt.left;                    // of the window were using   m_nHeight = l_rt.bottom - l_rt.top;   //   if(!UpdateWindow(m_hWnd))     throw "CD3DApp::Resize() - Error: UpdateWindow() failed\n";   //   return; }    


-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Hi there,

In C++ Builder you could just go

    ClientHeight = 480;ClientWidth = 640;    


It does all the rest for you,much simpler :-)

This topic is closed to new replies.

Advertisement