Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / MFC

MFC Snapping Windows

Rate me:
Please Sign up or sign in to vote.
4.68/5 (17 votes)
27 May 2014GPL34 min read 44.5K   2.7K   56  
Easily create windows that snap to each other.
// SampleSnapperDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SampleSnapper.h"
#include "SampleSnapperDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSampleSnapperDlg dialog




CSampleSnapperDlg::CSampleSnapperDlg(CWnd* pParent /*=NULL*/)
	: CExtPS < CDialogEx >(CSampleSnapperDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSampleSnapperDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CSampleSnapperDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CSampleSnapperDlg::OnBnClickedButton1)
	ON_MESSAGE(WM_DESTROY_CHILD, CSampleSnapperDlg::OnDestroyChild)
END_MESSAGE_MAP()


// CSampleSnapperDlg message handlers

BOOL CSampleSnapperDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSampleSnapperDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSampleSnapperDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CSampleSnapperDlg::OnBnClickedButton1()
{
	POSITION pos = m_Snappers.AddTail(new CChildSnapper());
	m_Snappers.GetAt(pos)->Create(CChildSnapper::IDD, this);
	m_Snappers.GetAt(pos)->ShowWindow(SW_SHOW);
	AddSnapper(m_Snappers.GetAt(pos)->GetSafeHwnd());
	//m_Snappers.GetAt(pos)->SetParentHwnd(GetSafeHwnd());

	// This is so we can delete the object and not leak memory
//	m_Snapper->SetParentHwnd(GetSafeHwnd());
	// TODO: Add your control notification handler code here
}

LRESULT CSampleSnapperDlg::OnDestroyChild(WPARAM wParam, LPARAM lParam)
{
	TRACE(_T("CSampleSnapperDlg::OnDestroyChild\n"));
	/*delete m_Snapper;*/
	CChildSnapper * ptrChild = NULL;
	HWND hWndChild = (HWND)wParam;

	POSITION pos = m_Snappers.GetHeadPosition();
	while (pos)
	{
		POSITION workingPOS = pos;
		ptrChild = m_Snappers.GetNext(pos);

		if ((ptrChild->GetSafeHwnd() == NULL) || 
			(ptrChild->GetSafeHwnd() == hWndChild))
		{
			m_Snappers.RemoveAt(workingPOS);
			delete ptrChild;
			return 0L;
		}
	}

	return 0L;
}

BOOL CSampleSnapperDlg::DestroyWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	//while (!m_Snappers.IsEmpty())
	//	delete m_Snappers.RemoveHead();

	return CExtPS::DestroyWindow();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions