SAMPLE: Resize.exe Shows How to Resize a Window in Jumps

ID: Q123605

The information in this article applies to:

SUMMARY

Sometimes it is useful to have a window that can only be certain sizes. For example, Microsoft Word and Microsoft Visual C++ have toolbars that are resizable only to particular sizes that best fit the controls in the toolbar. When you do this, it's a good idea to give the user visual cues about the available window sizes. The RESIZE sample code shows by example how to modify the way Windows resizes a window so that when a user uses the mouse to resize the window the border jumps automatically to the next available size.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ Resize.exe (size: 33190 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from
               Online Services

When a user clicks the resizing border of a window, Windows enters a PeekMessage loop to capture all the mouse messages that occur until the left mouse button is released. While inside this loop, every time the mouse moves it moves the rectangle that shows the new window size to provide a visual cue to the user as to what the new window size will be.

The RESIZE sample code modifies the resizing operation by entering it's own message loop to capture the mouse messages until the left button is released. Instead of updating the rectangle every time a mouse move is received, the RESIZE code checks to see if the current mouse position would make the window size one of the possible window width and height sizes as defined by the application. By doing this, the RESIZE application provides more accurate visual cues about what the resizing operation will do.

The resizing operation is triggered by the WM_NCLBUTTONDOWN message both for Windows and the RESIZE application. When this message is received, a message loop is entered to filter out all the mouse messages except for two, WM_MOUSEMOVE and WM_LBUTTONUP. When the WM_MOUSEMOVE message is received, the RESIZE application checks to see if the current mouse position would make the window larger or smaller. If the window would be smaller, the resizing rectangle is moved to the next smaller dimension defined by the application. If the window would be larger, the program checks to see if the new size would be large enough for the next possible dimension and updates the rectangle accordingly. When the WM_LBUTTONUP message is received, the resizing operation is completed by updating the window size to the current position defined by the mouse and the rectangle is removed.

The RESIZE application also takes advantage of some of the flexibility provided by processing the WM_NCHITTEST message. Windows sends this message to an application with a mouse position and expects the application to describe which part of the window that mouse position covers. Frequently, applications pass this message on to DefWindowProc() and let the default calculations take care of telling the system what the mouse is on top of. The RESIZE application allows DefWindowProc() to process the message, but then checks to see if the mouse is over one of the resizing corners or in the client area. To simplify the resizing operation, RESIZE doesn't let the user resize from a window corner, so the application overrides the HTBOTTOMLEFT, HTBOTTOMRIGHT, HTTOPLEFT, and HTTOPRIGHT hit test codes and returns HTBOTTOM or HTTOP. By doing this, the mouse cursor accurately reflects the direction of the resize. When the HTCLIENT hit test code is returned, RESIZE changes this to HTCAPTION to allow the window to be moved even though it doesn't have a title bar.

Although this technique will work in Windows 95, it is not necessary. Windows 95 provides a new message WM_SIZING that will enable the program to do exactly the same thing without processing the WM_NCxxx messages or entering a PeekMessage() loop.

Additional query words: Keywords : kbcode kbfile kbsample kbNTOS kbSDKWin32 kbGrpUser kbWinOS kbWndw

Last Reviewed: December 26, 1998