SAMPLE: RESIZE App Shows How to Resize a Window in Jumps

Last reviewed: October 10, 1997
Article ID: Q123605
3.10 3.50 | 4.00 WINDOWS NT | WINDOWS kbui kbcode kbfile

The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.1 and 3.5
        - Microsoft Windows 95 version 4.0
    

SUMMARY

Sometimes it's 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.

To obtain the RESIZE sample code, download RESIZE.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download RESIZE.EXE (size: 33190 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get RESIZE.EXE (size: 33190 bytes) 
    

MORE INFORMATION

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 reference words: 3.10 3.50 4.00
KBCategory: kbui kbcode kbfile
KBSubcategory: UsrWndw
Keywords : UsrWndw kbcode kbfile kbui
Version : 3.10 3.50 | 4.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.