/*----------------------------------------------------------------------
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 );
}