#include “CtrlResize.h”
#if !defined(CTRLRESIZE_H__)
#define CTRLRESIZE_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CtrlResize.h : header file
//
#include <AfxTempl.h>
/////////////////////////////////////////////////////////////////////////////////////////////
// CCtrlResize,根据窗口大小重新排列组合控件的位置和大小
class CCtrlResize
{
public:
CCtrlResize();
enum EAlignMode//控件对齐方式
{
eFixed = 1,//保持原值不变
eKeepRatioWithDlgInflated = 2,//和对话框的宽(或高)的增量保持正比
};
struct CTRLRESIZEINFO
{
int nCtrlId;//控件ID
EAlignMode eAlignX;//左上角X坐标对齐方式
EAlignMode eAlignY;//左上角Y坐标对齐方式
EAlignMode eAlignWidth;//宽对齐方式
EAlignMode eAlignHeight;//高对齐方式
double dRatioX;//左上角X坐标和对话框宽的比例
double dRatioY;//左上角Y坐标和对话框高的比例
double dRatioWidth;//宽和对话框宽的比例
double dRatioHeight;//高和对话框高的比例
double dLTX;//左上角原始X坐标
double dLTY;//左上角原始Y坐标
double dWidth;//原始宽度
double dHeight;//原始高度
};
void InitDlg(CWnd *pWnd);
void AddCtrl(int nCtrlId,
double dRatioX,//左上角X坐标和对话框宽增量的比例
double dRatioY,//左上角Y坐标和对话框高增量的比例
double dRatioWidth,//宽和对话框宽增量的比例
double dRatioHeight//高和对话框高增量的比例
);
void ModiCtrl(int nCtrlId,
int nXOffset,int nYOffset,int nWidthOffset,int nHeightOffset);
void ResizeAllCtrl(int nCX, int nCY);
private:
CWnd *m_pWnd;
CRect m_DlgRect;
CMap <int,int,CTRLRESIZEINFO,CTRLRESIZEINFO> m_mapCtrlIdtoInfo;
};
#endif
CtrlResize.cpp
#include "stdafx.h"
#include "CtrlResize.h"
/////////////////////////////////////////////////////////////////////////////////////////////
// CCtrlResize,根据窗口大小重新排列组合控件的位置和大小
// gsk 2003-4-29
CCtrlResize::CCtrlResize()
{
}
void CCtrlResize::InitDlg(CWnd *pWnd)
{
m_pWnd = pWnd;
if(m_pWnd == NULL)
{
return;
}
m_pWnd->GetClientRect(m_DlgRect);
}
void CCtrlResize::AddCtrl(int nCtrlId,
double dRatioX,//左上角X坐标和对话框宽增量的比例
double dRatioY,//左上角Y坐标和对话框高增量的比例
double dRatioWidth,//宽和对话框宽增量的比例
double dRatioHeight//高和对话框高增量的比例
)
{
CTRLRESIZEINFO ctrl;
CRect rect;
if(m_pWnd == NULL)
{
return;
}
if(m_pWnd->GetDlgItem(nCtrlId) == NULL)
{
return;
}
m_pWnd->GetDlgItem(nCtrlId)->GetWindowRect(rect);
m_pWnd->ScreenToClient(&rect);
ctrl.nCtrlId = nCtrlId;
ctrl.dLTX = rect.left;
ctrl.dLTY = rect.top;
ctrl.dWidth = rect.Width();
ctrl.dHeight = rect.Height();
ctrl.dRatioX = dRatioX;
ctrl.dRatioY = dRatioY;
ctrl.dRatioWidth = dRatioWidth;
ctrl.dRatioHeight = dRatioHeight;
ctrl.eAlignX = (dRatioX==0? eFixed:eKeepRatioWithDlgInflated);
ctrl.eAlignY = (dRatioY==0? eFixed:eKeepRatioWithDlgInflated);
ctrl.eAlignWidth = (dRatioWidth==0? eFixed:eKeepRatioWithDlgInflated);
ctrl.eAlignHeight = (dRatioHeight==0? eFixed:eKeepRatioWithDlgInflated);
m_mapCtrlIdtoInfo.SetAt(nCtrlId,ctrl);
}
void CCtrlResize::ModiCtrl(int nCtrlId,
int nXOffset,int nYOffset,int nWidthOffset,int nHeightOffset)
{
CTRLRESIZEINFO ctrl;
if(m_mapCtrlIdtoInfo.Lookup(nCtrlId,ctrl) == FALSE)
{
return;
}
ctrl.dLTX += nXOffset;
ctrl.dLTY += nYOffset;
ctrl.dWidth += nWidthOffset;
ctrl.dHeight += nHeightOffset;
m_mapCtrlIdtoInfo.SetAt(nCtrlId,ctrl);
}
void CCtrlResize::ResizeAllCtrl(int nCX, int nCY)
{
int nInflatedX,nInflatedY,nCtrlId;
POSITION pos;
CTRLRESIZEINFO ctrl;
CRect rectNewPos;
nInflatedX = nCX - m_DlgRect.Width();
nInflatedY = nCY - m_DlgRect.Height();
pos = m_mapCtrlIdtoInfo.GetStartPosition();
while(pos)
{
m_mapCtrlIdtoInfo.GetNextAssoc(pos,nCtrlId,ctrl);
if(ctrl.eAlignX == eFixed)
{
rectNewPos.left = (long)ctrl.dLTX;
}
else
{
rectNewPos.left = (long)(ctrl.dLTX + nInflatedX*ctrl.dRatioX);
}
if(ctrl.eAlignWidth == eFixed)
{
rectNewPos.right = (long)(rectNewPos.left + ctrl.dWidth);
}
else
{
rectNewPos.right = (long)(rectNewPos.left + ctrl.dWidth + nInflatedX*ctrl.dRatioWidth);
}
if(ctrl.eAlignY == eFixed)
{
rectNewPos.top = (long)ctrl.dLTY;
}
else
{
rectNewPos.top = (long)(ctrl.dLTY + nInflatedY*ctrl.dRatioY);
}
if(ctrl.eAlignHeight == eFixed)
{
rectNewPos.bottom = (long)(rectNewPos.top + ctrl.dHeight);
}
else
{
rectNewPos.bottom = (long)(rectNewPos.top + ctrl.dHeight + nInflatedY*ctrl.dRatioHeight);
}
if(m_pWnd->GetDlgItem(ctrl.nCtrlId))
{
m_pWnd->GetDlgItem(ctrl.nCtrlId)->MoveWindow(rectNewPos);
}
}
}