|
////////////////////////////////////////////////////////////////////////
// DlgItemResizer.cpp : implementation file
//
// Written by Magnus Egelberg (magnus.egelberg@lundalogik.se)
// Much of this code comes from Eli Vingot (elivingt@internet-zahav.net)
//
//
#include "stdafx.h"
#include "DlgItemResizer.h"
CDlgItemResizer::CDlgItemResizer()
{
m_szInitial = CSize(0,0);
}
CDlgItemResizer::~CDlgItemResizer()
{
// Make sure to delete all allocated items
for (INT i = 0; i < m_vecControls.size(); i++)
{
delete m_vecControls[i];
}
}
void CDlgItemResizer::Add(HWND hWnd, UINT uFlags)
{
HWND hParent = GetParent(hWnd);
if (uFlags == 0)
return;
CRect rc;
GetWindowRect(hWnd, &rc);
ScreenToClient(hParent, &rc);
DLGITEMINFO *item = new DLGITEMINFO;
item->m_uFlags = uFlags;
item->m_hWnd = hWnd;
item->m_rectPosition = rc;
// Set inital size if not set
if (m_szInitial.cx == 0 || m_szInitial.cy == 0)
{
CRect rc;
GetClientRect(hParent, &rc);
m_szInitial = rc.Size();
}
// Add it to the array
m_vecControls.push_back(item);
}
void CDlgItemResizer::Resize(HWND hWnd)
{
// Just return if no initial size yet
if (m_szInitial.cx == 0 || m_szInitial.cy == 0)
return;
// Don't bother to resize minimized windows
if (GetWindowLong(hWnd, GWL_STYLE) & WS_MINIMIZE)
return;
CRect client;
GetClientRect(hWnd, &client);
for (INT i = 0; i < m_vecControls.size(); i++)
{
DLGITEMINFO *item = (DLGITEMINFO *)m_vecControls[i];
// Invalidate the old position
CRect rect;
GetWindowRect(item->m_hWnd, &rect);
ScreenToClient(hWnd, &rect);
InvalidateRect(hWnd, &rect, TRUE);
// Get the current size of the control
CSize size = rect.Size();
// Set the new position according to the flags specified
if ((item->m_uFlags & (RESIZE_LOCKLEFT|RESIZE_LOCKRIGHT)) ==
(RESIZE_LOCKLEFT|RESIZE_LOCKRIGHT))
{
rect.left = item->m_rectPosition.left;
rect.right = client.right -
(m_szInitial.cx - item->m_rectPosition.right);
}
else if (item->m_uFlags & RESIZE_LOCKRIGHT)
{
rect.right = client.right -
(m_szInitial.cx - item->m_rectPosition.right);
rect.left = rect.right - size.cx;
}
else if (item->m_uFlags & RESIZE_LOCKLEFT)
{
rect.left = item->m_rectPosition.left;
rect.right = rect.left + size.cx;
}
if ((item->m_uFlags & (RESIZE_LOCKTOP|RESIZE_LOCKBOTTOM)) ==
(RESIZE_LOCKTOP|RESIZE_LOCKBOTTOM))
{
rect.top = item->m_rectPosition.top;
rect.bottom = client.bottom -
(m_szInitial.cy - item->m_rectPosition.bottom);
}
else if (item->m_uFlags & RESIZE_LOCKTOP)
{
rect.top = item->m_rectPosition.top;
rect.bottom = rect.top + size.cy;
}
else if (item->m_uFlags & RESIZE_LOCKBOTTOM)
{
rect.bottom = client.bottom -
(m_szInitial.cy - item->m_rectPosition.bottom);
rect.top = rect.bottom - size.cy;
}
// Check if control is completely inside client, hide if not
// Do this only when the RESIZE_SHOWHIDE flag is set.
if (item->m_uFlags & RESIZE_SHOWHIDE)
{
CRect unionrect;
unionrect.UnionRect(rect, client);
if (unionrect != client)
{
ShowWindow(item->m_hWnd, SW_HIDE);
}
else
{
// Make sure it is visible
if (!IsWindowVisible(item->m_hWnd))
{
ShowWindow(item->m_hWnd, SW_SHOWNORMAL);
}
MoveWindow(item->m_hWnd, rect.left, rect.top,
rect.Width(), rect.Height(), TRUE);
}
}
else
{
MoveWindow(item->m_hWnd, rect.left, rect.top,
rect.Width(), rect.Height(), TRUE);
}
}
}
void CDlgItemResizer::ScreenToClient(HWND hWnd, CRect* pRect)
{
CPoint point;
point.x = pRect->left;
point.y = pRect->top;
::ScreenToClient(hWnd, &point);
pRect->left = point.x;
pRect->top = point.y;
point.x = pRect->right;
point.y = pRect->bottom;
::ScreenToClient(hWnd, &point);
pRect->right = point.x;
pRect->bottom = point.y;
}
|
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.
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.