Snapping Windows | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 26, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.