From e67b77ba5d489f46d5f12ac7ef93092dee8144ef Mon Sep 17 00:00:00 2001 From: HarrievG Date: Sun, 9 May 2021 02:45:54 +0200 Subject: [PATCH] - Wrapped GetDpiForWindow in GetWindowScalingFactor - Use GetDeviceCaps for dpi on anything else than win10. --- neo/CMakeLists.txt | 1 + neo/tools/common/PropTree/PropTree.cpp | 6 ++--- neo/tools/common/PropTree/PropTreeItem.cpp | 6 ++--- .../common/PropTree/PropTreeItemButton.cpp | 3 +-- .../common/PropTree/PropTreeItemCheck.cpp | 3 +-- .../common/PropTree/PropTreeItemColor.cpp | 6 ++--- neo/tools/common/PropTree/PropTreeList.cpp | 10 ++++----- neo/tools/common/PropTree/PropTreeView.cpp | 3 +-- neo/tools/common/PropertyGrid.cpp | 3 +-- neo/tools/decl/DialogDeclBrowser.cpp | 3 +-- neo/tools/decl/DialogDeclEditor.cpp | 6 ++--- neo/tools/edit_gui_common.cpp | 22 +++++++++++++++++++ neo/tools/edit_gui_common.h | 1 + neo/tools/materialeditor/MEMainFrame.cpp | 3 +-- neo/tools/materialeditor/MaterialEditView.cpp | 3 +-- neo/tools/materialeditor/ToggleListView.cpp | 3 +-- neo/tools/radiant/DialogTextures.cpp | 3 +-- neo/tools/radiant/EditViewDlg.cpp | 4 +--- neo/tools/radiant/EntityDlg.cpp | 3 +-- neo/tools/radiant/InspectorDialog.cpp | 3 +-- neo/tools/radiant/MainFrm.cpp | 3 +-- neo/tools/radiant/MediaPreviewDlg.cpp | 3 +-- neo/tools/radiant/PropertyList.cpp | 12 ++++------ 23 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 neo/tools/edit_gui_common.cpp diff --git a/neo/CMakeLists.txt b/neo/CMakeLists.txt index 6031822d..3d4ce4c4 100644 --- a/neo/CMakeLists.txt +++ b/neo/CMakeLists.txt @@ -863,6 +863,7 @@ if (TOOLS AND MFC_FOUND AND MSVC) ${src_sound_editor} "tools/edit_public.h" "tools/edit_gui_common.h" + "tools/edit_gui_common.cpp" ) SET(CMAKE_MFC_FLAG 2) set(TOOLS_DEFINES "ID_ALLOW_TOOLS;_AFXDLL") diff --git a/neo/tools/common/PropTree/PropTree.cpp b/neo/tools/common/PropTree/PropTree.cpp index 11e9d2dc..d3c0e2d0 100644 --- a/neo/tools/common/PropTree/PropTree.cpp +++ b/neo/tools/common/PropTree/PropTree.cpp @@ -191,8 +191,7 @@ void CPropTree::OnSize(UINT nType, int cx, int cy) void CPropTree::ResizeChildWindows(int cx, int cy) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int sh = int(m_nInfoHeight * scaling_factor); if (m_bShowInfo) @@ -213,8 +212,7 @@ void CPropTree::ResizeChildWindows(int cx, int cy) void CPropTree::InitGlobalResources() { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); NONCLIENTMETRICS info; diff --git a/neo/tools/common/PropTree/PropTreeItem.cpp b/neo/tools/common/PropTree/PropTreeItem.cpp index d966a1ca..fd93279d 100644 --- a/neo/tools/common/PropTree/PropTreeItem.cpp +++ b/neo/tools/common/PropTree/PropTreeItem.cpp @@ -262,8 +262,7 @@ LONG CPropTreeItem::GetHeight() { if (m_pProp) { - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); return PROPTREEITEM_DEFHEIGHT * scaling_factor; } return PROPTREEITEM_DEFHEIGHT; @@ -402,8 +401,7 @@ LONG CPropTreeItem::DrawItem(CDC* pDC, const RECT& rc, LONG x, LONG y) ASSERT(m_pProp!=NULL); - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); int PNINDENT_s = int(PNINDENT * scaling_factor); int PROPTREEITEM_SPACE_s = int(PROPTREEITEM_SPACE * scaling_factor); int PROPTREEITEM_CHECKBOX_s = int(PROPTREEITEM_CHECKBOX * scaling_factor); diff --git a/neo/tools/common/PropTree/PropTreeItemButton.cpp b/neo/tools/common/PropTree/PropTreeItemButton.cpp index c6cfc35a..1414f072 100644 --- a/neo/tools/common/PropTree/PropTreeItemButton.cpp +++ b/neo/tools/common/PropTree/PropTreeItemButton.cpp @@ -54,8 +54,7 @@ LONG CPropTreeItemButton::DrawItem( CDC* pDC, const RECT& rc, LONG x, LONG y ) nTotal = CPropTreeItem::DrawItem( pDC, rc, x, y ); textSize = pDC->GetOutputTextExtent( buttonText ); - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); int s2 = int(2 * scaling_factor); int s4 = int(4 * scaling_factor); int s12 = int(12 * scaling_factor); diff --git a/neo/tools/common/PropTree/PropTreeItemCheck.cpp b/neo/tools/common/PropTree/PropTreeItemCheck.cpp index 4d71cee5..9a7eff1f 100644 --- a/neo/tools/common/PropTree/PropTreeItemCheck.cpp +++ b/neo/tools/common/PropTree/PropTreeItemCheck.cpp @@ -65,8 +65,7 @@ void CPropTreeItemCheck::DrawAttribute(CDC* pDC, const RECT& rc) return; } - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); int CHECK_BOX_SIZE_s = int(CHECK_BOX_SIZE * scaling_factor); checkRect.left = m_rc.left; diff --git a/neo/tools/common/PropTree/PropTreeItemColor.cpp b/neo/tools/common/PropTree/PropTreeItemColor.cpp index a20f14d4..51de9caf 100644 --- a/neo/tools/common/PropTree/PropTreeItemColor.cpp +++ b/neo/tools/common/PropTree/PropTreeItemColor.cpp @@ -221,8 +221,7 @@ void CPropTreeItemColor::OnActivate(int activateType, CPoint point) m_cPrevColor = m_cColor; - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); r = m_rc; r.right = r.left + 150 * scaling_factor; @@ -261,8 +260,7 @@ void CPropTreeItemColor::OnPaint() { CPaintDC dc(this); CPoint pt; - UINT dpi = GetDpiForWindow(m_pProp->GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(m_pProp->GetSafeHwnd()); int s3 = int(3 * scaling_factor); int s7 = int(7 * scaling_factor); int s13 = int(13 * scaling_factor); diff --git a/neo/tools/common/PropTree/PropTreeList.cpp b/neo/tools/common/PropTree/PropTreeList.cpp index a2abbf09..88744cb4 100644 --- a/neo/tools/common/PropTree/PropTreeList.cpp +++ b/neo/tools/common/PropTree/PropTreeList.cpp @@ -128,8 +128,8 @@ void CPropTreeList::UpdateResize() SCROLLINFO si; LONG nHeight; CRect rc; - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); ASSERT(m_pProp!=NULL); @@ -168,8 +168,7 @@ void CPropTreeList::OnPaint() CRect rc; GetClientRect(rc); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); rc.InflateRect(scaling_factor, scaling_factor); // draw control background @@ -572,8 +571,7 @@ void CPropTreeList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*) LONG nHeight; SetFocus(); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); GetClientRect(rc); nHeight = rc.Height(); diff --git a/neo/tools/common/PropTree/PropTreeView.cpp b/neo/tools/common/PropTree/PropTreeView.cpp index 8288e7b7..6d330947 100644 --- a/neo/tools/common/PropTree/PropTreeView.cpp +++ b/neo/tools/common/PropTree/PropTreeView.cpp @@ -103,8 +103,7 @@ void CPropTreeView::OnPaint() } void CPropTreeView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s20 = int(20 * scaling_factor); // #HvGNote : This should be the right way to do it, but hardcoded is fine too. diff --git a/neo/tools/common/PropertyGrid.cpp b/neo/tools/common/PropertyGrid.cpp index 7b5bbad9..487414b4 100644 --- a/neo/tools/common/PropertyGrid.cpp +++ b/neo/tools/common/PropertyGrid.cpp @@ -587,8 +587,7 @@ bool rvPropertyGrid::ReflectMessage ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM case WM_MEASUREITEM: { - UINT dpi = GetDpiForWindow(hWnd); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(hWnd); MEASUREITEMSTRUCT* mis = (MEASUREITEMSTRUCT*) lParam; mis->itemHeight = 18 * scaling_factor; diff --git a/neo/tools/decl/DialogDeclBrowser.cpp b/neo/tools/decl/DialogDeclBrowser.cpp index 8c5ef3b3..5c3e75c1 100644 --- a/neo/tools/decl/DialogDeclBrowser.cpp +++ b/neo/tools/decl/DialogDeclBrowser.cpp @@ -659,8 +659,7 @@ void DialogDeclBrowser::OnSize( UINT nType, int cx, int cy ) { GetClientRect( clientRect ); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); float scaled_toolbar_height = (TOOLBAR_HEIGHT * scaling_factor); float scaled_button_space = (BUTTON_SPACE * scaling_factor); float scaled_border_size = (BORDER_SIZE * scaling_factor); diff --git a/neo/tools/decl/DialogDeclEditor.cpp b/neo/tools/decl/DialogDeclEditor.cpp index f2b637fb..0a98f59e 100644 --- a/neo/tools/decl/DialogDeclEditor.cpp +++ b/neo/tools/decl/DialogDeclEditor.cpp @@ -235,8 +235,7 @@ void DialogDeclEditor::LoadDecl( idDecl *decl ) { SetWindowText( va( "Declaration Editor (%s, line %d)", decl->GetFileName(), decl->GetLineNum() ) ); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); rect.left = initialRect.left; rect.right = rect.left + (maxCharsPerLine * FONT_WIDTH + 32) *scaling_factor; @@ -386,8 +385,7 @@ void DialogDeclEditor::OnSize( UINT nType, int cx, int cy ) { CDialog::OnSize( nType, cx, cy ); GetClientRect( clientRect ); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); float scaled_toolbar_height = (TOOLBAR_HEIGHT * scaling_factor); float scaled_button_space = (BUTTON_SPACE * scaling_factor); diff --git a/neo/tools/edit_gui_common.cpp b/neo/tools/edit_gui_common.cpp new file mode 100644 index 00000000..38a99fc7 --- /dev/null +++ b/neo/tools/edit_gui_common.cpp @@ -0,0 +1,22 @@ +#include "edit_gui_common.h" + +//https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt + +float GetWindowScalingFactor(HWND window) +{ + float scaling_factor = 1.0f; + +#if (WINVER == 0x0A00) // Windows 10 + UINT dpi = GetDpiForWindow(window); + scaling_factor = static_cast(dpi) / 96.0f; +#else + HDC hdc = GetDC(window); + int LogicalScreenHeight = GetDeviceCaps(hdc, VERTRES); + int PhysicalScreenHeight = GetDeviceCaps(hdc, DESKTOPVERTRES); + scaling_factor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; +#endif + + return scaling_factor;// 1.25 = 125% +} + + diff --git a/neo/tools/edit_gui_common.h b/neo/tools/edit_gui_common.h index 48b43c1b..d2a50d7b 100644 --- a/neo/tools/edit_gui_common.h +++ b/neo/tools/edit_gui_common.h @@ -179,4 +179,5 @@ // Compilers for map, model, video etc. processing. #include "tools/compilers/compiler_public.h" +float GetWindowScalingFactor(HWND window); #endif // TOOLS_EDIT_GUI_COMMON_H diff --git a/neo/tools/materialeditor/MEMainFrame.cpp b/neo/tools/materialeditor/MEMainFrame.cpp index e633f8ee..d47c8c91 100644 --- a/neo/tools/materialeditor/MEMainFrame.cpp +++ b/neo/tools/materialeditor/MEMainFrame.cpp @@ -334,8 +334,7 @@ void MEMainFrame::OnSize(UINT nType, int cx, int cy) CFrameWnd::OnSize(nType, cx, cy); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s5 = int(5 * scaling_factor); CRect statusRect; diff --git a/neo/tools/materialeditor/MaterialEditView.cpp b/neo/tools/materialeditor/MaterialEditView.cpp index 6153b2cd..25646bea 100644 --- a/neo/tools/materialeditor/MaterialEditView.cpp +++ b/neo/tools/materialeditor/MaterialEditView.cpp @@ -241,8 +241,7 @@ void MaterialEditView::OnSize(UINT nType, int cx, int cy) { m_tabs.GetItemRect(0, tabRect); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s2 = int(2 * scaling_factor); int s8 = int(8 * scaling_factor); int s4 = int(4 * scaling_factor); diff --git a/neo/tools/materialeditor/ToggleListView.cpp b/neo/tools/materialeditor/ToggleListView.cpp index 075469c3..148d56f9 100644 --- a/neo/tools/materialeditor/ToggleListView.cpp +++ b/neo/tools/materialeditor/ToggleListView.cpp @@ -160,8 +160,7 @@ void ToggleListView::OnSize(UINT nType, int cx, int cy) { * Returns the size of each item in the toggle list. */ void ToggleListView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); lpMeasureItemStruct->itemHeight = TOGGLELIST_ITEMHEIGHT * scaling_factor; } diff --git a/neo/tools/radiant/DialogTextures.cpp b/neo/tools/radiant/DialogTextures.cpp index ef3259df..0b0d744b 100644 --- a/neo/tools/radiant/DialogTextures.cpp +++ b/neo/tools/radiant/DialogTextures.cpp @@ -938,8 +938,7 @@ void CDialogTextures::OnSize(UINT nType, int cx, int cy) return; } - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s8 = int(8 * scaling_factor); int s4 = int(4 * scaling_factor); int s12 = int(12 * scaling_factor); diff --git a/neo/tools/radiant/EditViewDlg.cpp b/neo/tools/radiant/EditViewDlg.cpp index f969f78c..d4ae5825 100644 --- a/neo/tools/radiant/EditViewDlg.cpp +++ b/neo/tools/radiant/EditViewDlg.cpp @@ -74,9 +74,7 @@ void CEditViewDlg::OnSize(UINT nType, int cx, int cy) { return; } - - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s2 = int(2 * scaling_factor); int s8 = int(8 * scaling_factor); int s4 = int(4 * scaling_factor); diff --git a/neo/tools/radiant/EntityDlg.cpp b/neo/tools/radiant/EntityDlg.cpp index efe311a7..c08d7061 100644 --- a/neo/tools/radiant/EntityDlg.cpp +++ b/neo/tools/radiant/EntityDlg.cpp @@ -164,9 +164,8 @@ void CEntityDlg::OnSize(UINT nType, int cx, int cy) CDialog::OnSize(nType, cx, cy); CRect rect, crect, crect2; GetClientRect(rect); + float scaling_factor = GetWindowScalingFactor(staticTitle.GetSafeHwnd()); - UINT dpi = GetDpiForWindow(staticTitle.GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; int s2 = int( 2 * scaling_factor); int s8 = int( 8 * scaling_factor); int s4 = int( 4 * scaling_factor); diff --git a/neo/tools/radiant/InspectorDialog.cpp b/neo/tools/radiant/InspectorDialog.cpp index 22348f87..647e6ee2 100644 --- a/neo/tools/radiant/InspectorDialog.cpp +++ b/neo/tools/radiant/InspectorDialog.cpp @@ -131,8 +131,7 @@ void CInspectorDialog::OnSize(UINT nType, int cx, int cy) POSITION pos; WORD wID; - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s5 = int(5 * scaling_factor); int s4 = int(4 * scaling_factor); diff --git a/neo/tools/radiant/MainFrm.cpp b/neo/tools/radiant/MainFrm.cpp index 8d5a01c8..e4520001 100644 --- a/neo/tools/radiant/MainFrm.cpp +++ b/neo/tools/radiant/MainFrm.cpp @@ -1797,8 +1797,7 @@ void CMainFrame::OnSize(UINT nType, int cx, int cy) { CRect rctParent; GetClientRect(rctParent); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); UINT nID; UINT nStyle; diff --git a/neo/tools/radiant/MediaPreviewDlg.cpp b/neo/tools/radiant/MediaPreviewDlg.cpp index e9db67ca..49ab5e07 100644 --- a/neo/tools/radiant/MediaPreviewDlg.cpp +++ b/neo/tools/radiant/MediaPreviewDlg.cpp @@ -108,8 +108,7 @@ BOOL CMediaPreviewDlg::OnInitDialog() void CMediaPreviewDlg::OnSize(UINT nType, int cx, int cy) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s8 = int(8 * scaling_factor); int s4 = int(4 * scaling_factor); diff --git a/neo/tools/radiant/PropertyList.cpp b/neo/tools/radiant/PropertyList.cpp index e780e9a7..60858358 100644 --- a/neo/tools/radiant/PropertyList.cpp +++ b/neo/tools/radiant/PropertyList.cpp @@ -88,8 +88,7 @@ BOOL CPropertyList::PreCreateWindow(CREATESTRUCT& cs) { } void CPropertyList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s20 = int(20 * scaling_factor); if (measureItem && !measureItem->m_curValue.IsEmpty()) { @@ -111,8 +110,7 @@ void CPropertyList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { void CPropertyList::DrawItem(LPDRAWITEMSTRUCT lpDIS) { - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s3 = 3;// int(3 * scaling_factor); CDC dc; @@ -196,8 +194,7 @@ void CPropertyList::OnSelchange() { static int recurse = 0; //m_curSel = GetCurSel(); - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s3 = int(3 * scaling_factor); GetItemRect(m_curSel,rect); @@ -280,8 +277,7 @@ void CPropertyList::DisplayButton(CRect region) { //displays a button if the property is a file/color/font chooser m_nLastBox = 2; m_prevSel = m_curSel; - UINT dpi = GetDpiForWindow(GetSafeHwnd()); - float scaling_factor = static_cast(dpi) / 96.0f; + float scaling_factor = GetWindowScalingFactor(GetSafeHwnd()); int s3 = int(3 * scaling_factor); if (region.Width() > 25) {