您的位置:首页 > 其它

VC 对话框 控件 随对话框大小改变

2011-04-18 20:55 357 查看
//该结构体保存控件相对于对话框的比例
typedef struct Perent
{
double percentX;
double percentY;
double lxP;
double lyP;
}PERCENT;
/////////////////////////////计算子控件相对于父窗口的比例填入结构体////////////////
void CBiJiBenDlg::InitCtrlPercent(UINT nID)
{
int index = nID - 1000;
CRect rcDialog,rc;//保存控件窗口的位置
GetWindowRect(&rcDialog);//得到的是相对于CBiJiBenDlg的位置
GetDlgItem(nID)->GetWindowRect(rc);

m_per[index].percentX =  (double)(rc.right-rc.left) / (rcDialog.right-rcDialog.left);
m_per[index].percentY =  (double)(rc.bottom-rc.top) / (rcDialog.bottom-rcDialog.top);
m_per[index].lxP = (double)rc.left / rcDialog.right;
m_per[index].lyP = (double)rc.top / rcDialog.bottom;//这4个比例应该是不变值
}
///////////////////////////根据子控件号重设子控件位置//////////////////////////
void CBiJiBenDlg::RePositionCtrl(UINT nID)
{
CRect rcDialog,rc;//保存控件窗口的位置
GetWindowRect(&rcDialog);
int index = nID - 1000;
#pragma warning(disable:4244)
int wid = (rcDialog.right-rcDialog.left) * m_per[index].percentX;
int height = (rcDialog.bottom-rcDialog.top) * m_per[index].percentY;
rc.left = /*rcDialog.left + */(rcDialog.right-rcDialog.left)*m_per[index].lxP;
rc.top = /*rcDialog.top + */(rcDialog.bottom-rcDialog.top)*m_per[index].lyP;
GetDlgItem(nID)->MoveWindow(rc.left, rc.top, wid, height);//这个函数的参数后两个是宽和高,而不是右下角坐标
}

InitCtrlPercent函数在BOOL CBiJiBenDlg::OnInitDialog()中添加,例如“InitCtrlPercent(IDC_TOPMOST);”。
RePositionCtrl函数在void CBiJiBenDlg::OnSize(UINT nType, int cx, int cy)中添加例如“RePositionCtrl(IDC_TOPMOST);”。

改变对话框大小前:



改变对话框大小后:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: