Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WTL

C++ Header Guard

Rate me:
Please Sign up or sign in to vote.
3.83/5 (9 votes)
17 Mar 2009CPOL2 min read 51.3K   238   12  
Create unique preprocessor macros to prevent multiple header inclusion.
#ifndef _0C46500C_084D_44ac_8C26_37E38BED2714_
#define _0C46500C_084D_44ac_8C26_37E38BED2714_
#pragma once

#ifdef __ATLBASE_H__
#include <atlmisc.h>
template <class T> class CSnapWindow
#else
class CSnapWindow
#endif
{
public:
       int snap_Margin, snap_ModifierKey;

#ifdef __ATLBASE_H__
	BEGIN_MSG_MAP(CSnapWindow<T>)
		MESSAGE_HANDLER(WM_MOVING, OnSnapMoving)
		MESSAGE_HANDLER(WM_ENTERSIZEMOVE, OnSnapEnterSizeMove)
	END_MSG_MAP()
#endif

#ifdef __ATLBASE_H__                                                                 
	virtual LRESULT OnSnapEnterSizeMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
#else
	virtual LRESULT OnSnapEnterSizeMove(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
#endif
					{
						snap_cur_pos.x=0;
						snap_cur_pos.y=0;

						snap_rcWindow.bottom=0;
						snap_rcWindow.left=0;
						snap_rcWindow.right=0;
						snap_rcWindow.top=0;

					#ifdef __ATLBASE_H__ 
						T* pT = static_cast<T*>(this);
						GetWindowRect(pT->m_hWnd, &snap_rcWindow );
					#else
						GetWindowRect(hWnd, &snap_rcWindow );
					#endif

						GetCursorPos( &snap_cur_pos );

						snap_x = snap_cur_pos.x - snap_rcWindow.left;
						snap_y = snap_cur_pos.y - snap_rcWindow.top;
					return 0;
					}

#ifdef __ATLBASE_H__ 
	virtual LRESULT OnSnapMoving(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
#else
	virtual LRESULT OnSnapMoving(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
#endif
					{
						//no snap if shift key pressed
						if (GetAsyncKeyState(snap_ModifierKey) < 0) return FALSE;

						snap_prc = (LPRECT)lParam;

						snap_cur_pos.x=0;
						snap_cur_pos.y=0;
						snap_rcWindow.bottom=0;
						snap_rcWindow.left=0;
						snap_rcWindow.right=0;
						snap_rcWindow.top=0;

						GetCursorPos( &snap_cur_pos );
						OffsetRect( snap_prc,
							snap_cur_pos.x - (snap_prc->left + snap_x) ,
							snap_cur_pos.y - (snap_prc->top  + snap_y) );

						//it may change during app lifetime
						SystemParametersInfo( SPI_GETWORKAREA, 0, &snap_wa, 0 );

						if (isSnapClose( snap_prc->left, snap_wa.left )) {OffsetRect( snap_prc, snap_wa.left - snap_prc->left, 0);}
						else 
							if (isSnapClose( snap_wa.right, snap_prc->right )) {OffsetRect( snap_prc, snap_wa.right - snap_prc->right, 0);}

						if (isSnapClose( snap_prc->top, snap_wa.top )) {OffsetRect( snap_prc, 0, snap_wa.top - snap_prc->top );}
						else 
							if (isSnapClose( snap_wa.bottom, snap_prc->bottom )) {OffsetRect( snap_prc, 0, snap_wa.bottom - snap_prc->bottom );}
					return TRUE;
					}

	virtual BOOL isSnapClose( int a, int b ) { return (abs( a - b ) < snap_Margin);}

	CSnapWindow()
		{
		snap_ModifierKey=VK_SHIFT;
		NONCLIENTMETRICS ncm = { 0 };
		ncm.cbSize = sizeof ncm;
		SystemParametersInfo( SPI_GETNONCLIENTMETRICS, 0, &ncm, 0 );
		snap_Margin=ncm.iCaptionHeight;
		}
private:
	POINT snap_cur_pos;
	RECT snap_rcWindow, snap_wa, *snap_prc;
	int snap_x, snap_y;
};
#endif//_0C46500C_084D_44ac_8C26_37E38BED2714_

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 Code Project Open License (CPOL)


Written By
Software Developer
Croatia Croatia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions