对话框改变大小保持控件比例

//.h
private:
	typedef struct Rate//控件位置占比,ID,
	{
		CWnd *pWndItem;
		CRect  rc;
		UINT   uid;
		double d_l;
		double d_t;
		double d_r;
		double d_b;
		Rate()
		{
			pWndItem=NULL;
			rc.SetRect(0,0,0,0);
			uid=0;
			d_l=0.0;
			d_t=0.0;
			d_r=0.0;
			d_b=0.0;
		}
	}; 
	vector<Rate> itemRateVtr;
	double cW;
	double cH;
	CRect rcDlg;
	void  initItemRect();
public:
	virtual BOOL OnInitDialog();

/*******************************************************************************/

//.cpp
BOOL CXXDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    GetClientRect(rcDlg);
	cW=(double)rcDlg.Width();
	cH=(double)rcDlg.Height();	
	CWnd *pWndChild=GetWindow(GW_CHILD);
	while (pWndChild)
	{
		Rate mRate;
		mRate.pWndItem=pWndChild;
		mRate.uid=GetWindowLong(pWndChild->GetSafeHwnd(),GWL_ID);
		pWndChild->GetWindowRect(mRate.rc);
		ScreenToClient(mRate.rc);
		mRate.d_l=mRate.rc.left/ cW;
		mRate.d_t=mRate.rc.top/ cH;
		mRate.d_r=mRate.rc.right/ cW;
		mRate.d_b=mRate.rc.bottom/ cH;
		itemRateVtr.push_back(mRate);
		pWndChild=pWndChild->GetNextWindow();
	}
}




//改变控件位置,大小
void  CXXDlg::initItemRect()
{
	this->GetClientRect(rcDlg);
	cW=(double)rcDlg.Width();
	cH=(double)rcDlg.Height();
	for (int i = 0; i < (int)itemRateVtr.size(); i++)
	{
		int left  =(int)(itemRateVtr[i].d_l * cW);
		int top   =(int)(itemRateVtr[i].d_t * cH);
		int right =(int)(itemRateVtr[i].d_r * cW);
		int bottom=(int)(itemRateVtr[i].d_b * cH);
		itemRateVtr[i].rc.SetRect(left,top,right,bottom);
		itemRateVtr[i].pWndItem->MoveWindow(itemRateVtr[i].rc);
	}
}


void CXXDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	if(SIZE_MINIMIZED==nType||SIZE_MAXHIDE==nType) return;
	initItemRect();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值