MFC控制元件隨視窗大小變化,多次縮放不改變位置
MFC控制元件隨視窗大小變化,多次縮放不影響位置
void CMyCefTestDlg::OnSize(UINT nType, int cx, int cy) {
CDialogEx::OnSize(nType, cx, cy); // TODO: 在此處新增訊息處理程式程式碼 //最小化判斷 if (nType == 1) { return; } else { CRect rect;//獲取當前視窗的大小 for (std::list<control*>::iterator it = m_con_list.begin(); it != m_con_list.end();it++) { CWnd* pWnd = GetDlgItem((*it)->Id);//獲取ID為woc的空間的控制代碼 pWnd->GetWindowRect(&rect); ScreenToClient(&rect);//將控制元件大小轉換為在對話方塊中的區域座標 rect.left = (*it)->scale[0] * cx; rect.right = (*it)->scale[1] * cx; rect.top = (*it)->scale[2] * cy; rect.bottom = (*it)->scale[3] * cy; pWnd->MoveWindow(rect);//設定控制元件大小 } } GetClientRect(&m_rect);//將變化後的對話方塊大小設為舊大小 return;
}
//在初始化裡面呼叫下面這個函式 void CMyCefTestDlg::get_control_original_proportion() { HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); while (hwndChild) { CRect rect;//獲取當前視窗的大小 control* tempcon = new control; CWnd* pWnd = GetDlgItem(::GetDlgCtrlID(hwndChild));//獲取ID為woc的空間的控制代碼 pWnd->GetWindowRect(&rect); ScreenToClient(&rect);//將控制元件大小轉換為在對話方塊中的區域座標 tempcon->Id = ::GetDlgCtrlID(hwndChild);//獲得控制元件的ID; tempcon->scale[0] = (double)rect.left / m_rect.Width();//注意型別轉換,不然儲存成long型就直接為0了 tempcon->scale[1] = (double)rect.right / m_rect.Width(); tempcon->scale[2] = (double)rect.top / m_rect.Height(); tempcon->scale[3] = (double)rect.bottom / m_rect.Height(); m_con_list.push_back(tempcon); hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT); } }
新增的兩個成員 CRect m_rect; std::list<control*> m_con_list;
結構體: typedef struct Rect { public: int Id; double scale[4];
Rect()
{
Id = -2;
scale[0] = 0;
scale[1] = 0;
scale[2] = 0;
scale[3] = 0;
}
Rect(const Rect& c)
{
*this = c;
}
}control;