Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / WTL

A fast and lightweight cell control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (31 votes)
11 Mar 2008CPOL1 min read 90.8K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
/*----------------------------------------------------------------------
Copyright (c)  Gipsysoft. All Rights Reserved.
Please see the file "licence.txt" for licencing details.
File:	MoveDialog.cpp
Owner:	russf@gipsysoft.com
Purpose:

	Move the dialog to a specific point, ensuring it stays on screen.

	Not tested on multiple monitors but in theory it *should* work.

----------------------------------------------------------------------*/
#include "stdafx.h"

#pragma warning( disable: 4706 )
#define COMPILE_MULTIMON_STUBS
#include <multimon.h>

static void GetDisplayWorkArea( POINT pt, RECT &rc )
{
	HMONITOR hMon = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
	if( hMon )
	{
		MONITORINFO m = { sizeof( m ) };
		if( GetMonitorInfo( hMon, &m ) )
		{
			rc = m.rcWork;
		}
	}
}


void MoveDialog( HWND hwnd, POINT pt )
{
	RECT rcWindow;
	GetWindowRect( hwnd, &rcWindow );

	const int nWidth = rcWindow.right - rcWindow.left;
	const int nHeight = rcWindow.bottom - rcWindow.top;
	
	RECT rcDisplay;
	GetDisplayWorkArea( pt, rcDisplay );

	if( pt.x < rcDisplay.left )
	{
		pt.x = rcDisplay.left;
	}

	if( pt.y < rcDisplay.top )
	{
		pt.y = rcDisplay.top;
	}

	RECT rcWindowNew;
	rcWindowNew.left = pt.x;
	rcWindowNew.top = pt.y;
	rcWindowNew.right = pt.x + nWidth;
	rcWindowNew.bottom = pt.y + nHeight;


	int nDeltaX = 0, nDeltaY = 0;
	if( rcWindowNew.right > rcDisplay.right )
	{
		nDeltaX = rcDisplay.right - rcWindowNew.right;
	}


	if( rcWindowNew.bottom > rcDisplay.bottom )
	{
		nDeltaY = rcDisplay.bottom - rcWindowNew.bottom;
	}


	if( nDeltaY || nDeltaX )
	{
		OffsetRect( &rcWindowNew, nDeltaX, nDeltaY );
	}
	SetWindowPos( hwnd, NULL, rcWindowNew.left, rcWindowNew.top, nWidth, nHeight, SWP_NOZORDER );
}

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
Web Developer
China China
My name is Yanxueming,i live in Chengdu China.Graduated from UESTC in 1999.

Comments and Discussions