Snapping Windows

Environment: VC6 SP4, W2K SP2

In the process of creating my own shell for Windows I found that having all of the individual elements snap to the edge of the screen very useful. To make this process much easier I put together a small class that provides drop in window snapping. It uses a MFC like class so modifying it to use MFC should be easy. NOTE: For dialog windows use the CSnapDlg instead.

The Features

  • Snaps to the edge of the desktop
  • Adjusts to screen resolution changes

How it Works

When the mouse is pressed in the non-client area of the window, the position is saved as an offset from the top left corner of the window. Then when the WM_MOVING message is sent it modifies the windows position based on the precalculated offset.

It is quite simple to use. Here is how to use it.

// register class
CTestWnd twWnd;
twWnd.RegisterClass("CSNAPWNDTEST",IDI_CSNAPWNDTEST,IDC_CSNAPWNDTEST);

// Perform application initialization:
twWnd.Init(NULL,
           "CSnapWnd Test",
           NULL,
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           0,
           CW_USEDEFAULT,
           0,
           NULL,
           nCmdShow);

Downloads

Download demo project and source - 30.6 KB