Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / MFC

A snap to screen border dialog class

Rate me:
Please Sign up or sign in to vote.
3.71/5 (4 votes)
8 Apr 2000 101.3K   2.1K   24  
Dialog class that implement a snap-to-screen-border feature like Winamp
#include "stdafx.h"
#include "SnapDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSnapDialog dialog


CSnapDialog::CSnapDialog(UINT IDD, CWnd* pParent /*=NULL*/)
	: CDialog(IDD, pParent)
{
	//{{AFX_DATA_INIT(CSnapDialog)
		// NOTE: the ClassWizard will add member initialization here
	m_nYOffset = 15;
	m_nXOffset = 15;
	//}}AFX_DATA_INIT
}

void CSnapDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSnapDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSnapDialog, CDialog)
	//{{AFX_MSG_MAP(CSnapDialog)
		// NOTE: the ClassWizard will add message map macros here
		ON_WM_WINDOWPOSCHANGING()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnapDialog message handlers

void CSnapDialog::OnWindowPosChanging( WINDOWPOS* lpwndpos )
{
	CRect	wndRect, trayRect;
	int		leftTaskbar = 0, rightTaskbar = 0, topTaskbar = 0, bottomTaskbar = 0;

	GetWindowRect(&wndRect);

	// Screen resolution
	int screenWidth =	GetSystemMetrics(SM_CXSCREEN); 
	int screenHeight =	GetSystemMetrics(SM_CYSCREEN);

	// Find the taskbar
	CWnd* pWnd = FindWindow(_T("Shell_TrayWnd"), _T(""));
	pWnd->GetWindowRect(&trayRect);

	int wndWidth = wndRect.right - wndRect.left;
	int wndHeight = wndRect.bottom - wndRect.top;

	if(trayRect.top <= 0 && trayRect.left <= 0 && trayRect.right >= screenWidth) {
		// top taskbar
		topTaskbar = trayRect.bottom - trayRect.top;
	}
	else if(trayRect.top > 0 && trayRect.left <= 0) {
		// bottom taskbar
		bottomTaskbar = trayRect.bottom - trayRect.top;
	}
	else if(trayRect.top <= 0 && trayRect.left > 0) {
		// right taskbar
		rightTaskbar = trayRect.right - trayRect.left;
	}
	else {
		// left taskbar
		leftTaskbar = trayRect.right - trayRect.left;
	}

	// Snap to screen border
	// Left border
	if(lpwndpos->x >= -m_nXOffset + leftTaskbar && lpwndpos->x <= leftTaskbar + m_nXOffset) {
		lpwndpos->x = leftTaskbar;
	}

	// Top border
	if(lpwndpos->y >= -m_nYOffset && lpwndpos->y <= topTaskbar + m_nYOffset) {
		lpwndpos->y = topTaskbar;
	}

	// Right border
	if(lpwndpos->x + wndWidth <= screenWidth - rightTaskbar + m_nXOffset && lpwndpos->x + wndWidth >= screenWidth - rightTaskbar - m_nXOffset) {
		lpwndpos->x = screenWidth - rightTaskbar - wndWidth;
	}

	// Bottom border
	if( lpwndpos->y + wndHeight <= screenHeight - bottomTaskbar + m_nYOffset && lpwndpos->y + wndHeight >= screenHeight - bottomTaskbar - m_nYOffset) {
		lpwndpos->y = screenHeight - bottomTaskbar - wndHeight;
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
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