|
|
Client Area Size with MoveWindowHow to resize in a Win32 program eliminating title and menu bars
One of the most deceptive tricks in Windows programming is the relationship between the client and window rectangles. We explain all...
IntroductionSometimes, we want to resize a window such that the available client area is pegged to a known size. Luckily, the Win32 API provides us with the MoveWindow function, which is used to resize a window. Unfortunately, the resulting size is based on the entire window area, and not just the client area. This article explains the use of the following functions:
We will also be needing the Win32 data types:
Using these elements of Windows programming, we can calculate the correct window size for the desired client area size. We can then roll up all of the code in a neat ClientResize function. Some of the key uses for this technique include:
Please feel free to cut and paste the code presented here in your own applications. The Client RectangleThe client rectangle refers to the part of the window that the programmer can directly draw to using the standard BeginPaint, EndPaint function pair. It is the area that Windows refers to when it sends the WM_PAINT message. It does not include child controls (including scrollbars), the title bar or menu area (should there be one). We obtain the client rectangle through the GetClientRect function: GetClientRect(HWND hWnd, RECT * rcClient); We usually pass a RECT structure by reference to the function, rather than declaring a pointer to a RECT. The Window RectangleThe window rectangle contains everything that makes up the window - the borders, title bar, menu area, and so on - but does not include the child windows. It will usually bound the child windows, unless the programmer has caused them to appear outside of the window rectangle. We can obtain the window rectangle using a call to GetWindowRect: GetWindowRect(HWND hWnd, RECT * rcWindow); The usage for this function is very similar to the GetClientRect function. The reader should be aware that these areas will change during the applications lifespan if the windows was created as being resizable. A common place to pick up the Window and Client areas is in processing the WM_SIZE message. The MoveWindow FunctionIn order to move the window, we use the MoveWindow function, which takes the window handle, the co-ordinates for the top corner, as well as the desired width and height of the window, based on the screen co-ordinates. The MoveWindow function is defined as: MoveWindow(HWND hWnd, int nX, int nY, int nWidth, int nHeight, BOOL bRepaint); The bRepaint flag determines whether the client area should be invalidated, causing a WM_PAINT message to be sent, allowing the client area to be repainted. As an aside, the screen co-ordinates can be obtained using a call similar to GetClientRect(GetDesktopWindow(), &rcDesktop) with rcDesktop being a variable of type RECT, passed by reference. SummarySo, our ClientResize function needs to get the two sizes (client and window), calculate the difference, and add that back to the desired client area size. Then, we call MoveWindow to effect the resize. This is all easily accomplished: void ClientResize(HWND hWnd, int nWidth, int nHeight) { RECT rcClient, rcWindow; POINT ptDiff; GetClientRect(hWnd, &rcClient); GetWindowRect(hWnd, &rcWindow); ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right; ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom; MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE); } Of course, we could have condensed the code into fewer function calls and calculations, but the watchword when programming for Windows is clarity above all else.
The copyright of the article Client Area Size with MoveWindow in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Client Area Size with MoveWindow in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|