개발은 하는건가..

[MFC] 배율에 따라 영향 받지 않도록 컨트롤 배치 본문

C, C++, MFC

[MFC] 배율에 따라 영향 받지 않도록 컨트롤 배치

수동애비 2022. 11. 9. 11:17
반응형

 

윈도우의 배율에 따라 컨트롤의 크기나 위치가 변경되는 것을 막으려고 DPI 인식을 높은 DIP 인식으로 변경해도  리소스 에디터에서 배치한 컨트롤의 위치가 배율에 영향을 받아 변경된다. 
찾고 찾아도 한방에 처리할 수 있는 방법은 못찾았고 그나마 내가 생각한 쉬운 방법을 정리해본다.

우선 윈도우 배율을 변경하지 말고 100% 상태로 리소스 에디터에서 컨트롤들의 크기와 위치를 맞춘 후 실행해서 맞게 배치되었는지 확인한다.

SetCtrlMove, PrintCtrlArrangePos 함수를 추가하고   OnInitDialog 에서 호출한다.

PrintCtrlArrangePos 함수를 통하여 현재 윈도우의 자식 컨트롤 윈도우들을 모두 찾아 해당 위치를 로그로 출력한다.

void CBaseDialog::SetCtrlMove(UINT nID, int nLeft, int nTop, int nWidth, int nHeight)
{
	CWnd *pWnd = GetDlgItem(nID);

	if (pWnd->GetSafeHwnd() != NULL) {
		pWnd->MoveWindow(nLeft, nTop, nWidth, nHeight);
	}
}


void CBaseDialog::PrintCtrlArrangePos(CHAR *pszWndName)
{
#ifdef _DEBUG		
	CStringA strLine;
	strLine.Format("========= %s::ArrangeControls Start ==========================\n", pszWndName);
	OutputDebugStringA(strLine);

	for (CWnd* pWnd = GetWindow(GW_CHILD); pWnd != NULL; pWnd = pWnd->GetWindow(GW_HWNDNEXT)) {	
		CRect rc = { 0, 0, 1, 1 };

		if (pWnd->GetSafeHwnd() != NULL) {
			pWnd->GetWindowRect(&rc);
			ScreenToClient(&rc);

			CString strArrange;		
			strArrange.Format(_T("SetCtrlMove(%d, %d, %d, %d, %d);\n"), pWnd->GetDlgCtrlID(), rc.left, rc.top, rc.Width(), rc.Height());
		
        	OutputDebugString(strArrange);
		}					
	}

	strLine.Format("========= %s::ArrangeControls End ==========================\n", pszWndName);
	OutputDebugStringA(strLine);

#endif
}



void CBaseDialog::OnInitDialog()
{
	PrintCtrlArrangePos("메인윈도우");
}

실행 시 아래와 같이 윈도우 위의 모든 컨트롤의 좌표가 출력되면 코드를 복사하여  onInitDialog() 안에 붙여 넣는다.

========= 메인윈도우::ArrangeControls Start ==========================
SetCtrlMove(1058, 300, 922, 62, 44);
SetCtrlMove(1057, 300, 867, 62, 44);
SetCtrlMove(1060, 25, 864, 102, 74);
SetCtrlMove(1039, 320, 13, 46, 36);
SetCtrlMove(1054, 200, 873, 80, 35);
SetCtrlMove(1056, 200, 927, 80, 35);
SetCtrlMove(1001, 70, 610, 246, 246);
SetCtrlMove(1052, 15, 63, 350, 147);
SetCtrlMove(0, 0, 225, 540, 365);
SetCtrlMove(0, 0, 225, 540, 365);
========= 메인윈도우::ArrangeControls End ==========================

적용 후 윈도우 디스플레이설정 배율을 변경 후 실행하여 배치된 상태를 확인한다.

Comments