Skip to content

Commit a36ba45

Browse files
committed
WIP: Add support for per-monitor DPI awareness (10)
1 parent ae46e46 commit a36ba45

File tree

9 files changed

+79
-17
lines changed

9 files changed

+79
-17
lines changed

Externals/crystaledit/Sample/MainFrm.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "MainFrm.h"
88
#include "ceditcmd.h"
9+
#include <commoncontrols.h>
910

1011
#ifdef _DEBUG
1112
#define new DEBUG_NEW
@@ -61,7 +62,7 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
6162
return -1;
6263

6364
if (!m_wndToolBar.Create(this) ||
64-
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
65+
!LoadToolBar())
6566
{
6667
TRACE0("Failed to create toolbar\n");
6768
return -1; // fail to create
@@ -107,6 +108,11 @@ LRESULT CMainFrame::OnDpiChanged(WPARAM wParam, LPARAM lParam)
107108
{
108109
__super::OnDpiChanged(wParam, lParam);
109110
DpiAware::UpdateAfxDataSysMetrics(GetDpi());
111+
LoadToolBar();
112+
const RECT* pRect = reinterpret_cast<RECT *>(lParam);
113+
SetWindowPos(nullptr, pRect->left, pRect->top,
114+
pRect->right - pRect->left,
115+
pRect->bottom - pRect->top, SWP_NOZORDER | SWP_NOACTIVATE);
110116
Default();
111117
return 0;
112118
}
@@ -119,6 +125,26 @@ BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
119125
return __super::PreCreateWindow(cs);
120126
}
121127

128+
BOOL CMainFrame::LoadToolBar()
129+
{
130+
m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
131+
CToolBarCtrl& toolbarCtrl = m_wndToolBar.GetToolBarCtrl();
132+
int cx = 16;
133+
int cy = 15;
134+
m_imgListToolBar.DeleteImageList();
135+
m_imgListToolBar.Create(IDR_MAINFRAME, cx, 0, RGB(192, 192, 192));
136+
CComQIPtr<IImageList2> pImageList2(reinterpret_cast<IImageList *>(m_imgListToolBar.m_hImageList));
137+
if (pImageList2)
138+
{
139+
cx = 16 * m_dpi / 96;
140+
cy = 15 * m_dpi / 96;
141+
HRESULT hr = pImageList2->Resize(cx, cy);
142+
}
143+
toolbarCtrl.SetImageList(&m_imgListToolBar);
144+
toolbarCtrl.SetButtonSize({ cx + 8, cy + 8 });
145+
return TRUE;
146+
}
147+
122148
/////////////////////////////////////////////////////////////////////////////
123149
// CMainFrame diagnostics
124150

Externals/crystaledit/Sample/MainFrm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ class CMainFrame : public DpiAware::CDpiAwareWnd<CMDIFrameWnd>
3131
virtual void AssertValid() const;
3232
virtual void Dump(CDumpContext& dc) const;
3333
#endif
34+
BOOL LoadToolBar();
3435

3536
protected: // control bar embedded members
3637
CStatusBar m_wndStatusBar;
3738
CToolBar m_wndToolBar;
39+
CImageList m_imgListToolBar;
3840

3941
// Generated message map functions
4042
protected:

Externals/crystaledit/editlib/utils/DpiAware.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-License-Identifier: BSL-1.0
2+
// Copyright (c) 2020 Takashi Sawanaka
3+
14
#include "StdAfx.h"
25
#include "DpiAware.h"
36
#include <commoncontrols.h>
@@ -16,7 +19,7 @@ namespace DpiAware
1619

1720
bool DpiAwareSupport = []()
1821
{
19-
DPIOnInit = MulDiv(96, ::GetSystemMetrics(SM_CXSMICON), 16);
22+
DPIOnInit = MulDiv(USER_DEFAULT_SCREEN_DPI, ::GetSystemMetrics(SM_CXSMICON), 16);
2023
HMODULE hLibraryUser32 = GetModuleHandleW(L"user32.dll");
2124
if (hLibraryUser32)
2225
{
@@ -121,7 +124,7 @@ namespace DpiAware
121124
else
122125
{
123126
int cxsmicon = ::GetSystemMetrics(SM_CXSMICON);
124-
int iconsize = MulDiv(16, dpi, 96);
127+
int iconsize = MulDiv(16, dpi, USER_DEFAULT_SCREEN_DPI);
125128
int size = SHIL_EXTRALARGE;
126129
if (iconsize < cxsmicon * 2)
127130
size = SHIL_SMALL;

Externals/crystaledit/editlib/utils/DpiAware.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
// SPDX-License-Identifier: BSL-1.0
4+
// Copyright (c) 2020 Takashi Sawanaka
5+
36
#include <afxwin.h>
47
#include <type_traits>
58
#include "mfc_templ_defines.h"
@@ -9,13 +12,13 @@
912
#define WM_DPICHANGED_BEFOREPARENT 0x02E2
1013
#define WM_DPICHANGED_AFTERPARENT 0x02E3
1114
#endif
15+
#ifndef USER_DEFAULT_SCREEN_DPI
16+
#define USER_DEFAULT_SCREEN_DPI 96
17+
#endif
1218

1319
namespace DpiAware
1420
{
15-
struct NONCLIENTMETRICS6 : public NONCLIENTMETRICS
16-
{
17-
int iPaddedBorderWidth;
18-
};
21+
struct NONCLIENTMETRICS6 : public NONCLIENTMETRICS { int iPaddedBorderWidth; };
1922

2023
using AdjustWindowRectExForDpiType = BOOL(__stdcall*)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
2124
using GetDpiForWindowType = UINT(__stdcall*)(HWND hwnd);

Src/Common/BCMenu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,11 @@ void BCMenu::MeasureItem( LPMEASUREITEMSTRUCT lpMIS )
779779

780780
const int BCMENU_PAD=4;
781781
if (m_hTheme == nullptr)
782-
lpMIS->itemWidth = MulDiv(m_iconX+BCMENU_PAD+8, dpi, 96) +t.cx;
782+
lpMIS->itemWidth = MulDiv(m_iconX+BCMENU_PAD+8, dpi, USER_DEFAULT_SCREEN_DPI) +t.cx;
783783
else
784784
lpMIS->itemWidth = m_gutterWidth+m_textBorder+t.cx+m_arrowWidth;
785785
int temp = DpiAware::GetSystemMetricsForDpi(SM_CYMENU, dpi);
786-
int temp2 = MulDiv(m_iconY + BCMENU_PAD, dpi, 96);
786+
int temp2 = MulDiv(m_iconY + BCMENU_PAD, dpi, USER_DEFAULT_SCREEN_DPI);
787787
lpMIS->itemHeight = temp>temp2 ? temp : temp2;
788788
}
789789
}

Src/Common/MessageBoxDialog.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ LRESULT CMessageBoxDialog::OnDpiChanged(WPARAM wParam, LPARAM lParam)
811811
GetDlgItem(IDCHECKBOX)->SetFont(&m_font);
812812
m_stcMessage.SetFont(&m_fontMainInstruction);
813813
Invalidate();
814+
m_sDialogUnit = {};
814815
return 0;
815816
}
816817

@@ -1178,38 +1179,45 @@ void CMessageBoxDialog::ParseStyle ( )
11781179
if ( ( m_nStyle & MB_ICONMASK ) && ( m_hIcon == nullptr ) )
11791180
{
11801181
// Switch the icon.
1182+
LPTSTR icon = nullptr;
11811183
switch ( m_nStyle & MB_ICONMASK )
11821184
{
11831185

11841186
case MB_ICONEXCLAMATION:
11851187

11861188
// Load the icon with the exclamation mark.
1187-
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION);
1189+
icon = IDI_EXCLAMATION;
11881190

11891191
break;
11901192

11911193
case MB_ICONHAND:
11921194

11931195
// Load the icon with the error symbol.
1194-
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_HAND);
1196+
icon = IDI_HAND;
11951197

11961198
break;
11971199

11981200
case MB_ICONQUESTION:
11991201

12001202
// Load the icon with the question mark.
1201-
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
1203+
icon = IDI_QUESTION;
12021204

12031205
break;
12041206

12051207
case MB_ICONASTERISK:
12061208

12071209
// Load the icon with the information symbol.
1208-
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_ASTERISK);
1210+
icon = IDI_ASTERISK;
12091211

12101212
break;
12111213

12121214
}
1215+
if (icon)
1216+
{
1217+
const int cx = GetSystemMetrics(SM_CXICON);
1218+
const int cy = GetSystemMetrics(SM_CYICON);
1219+
DpiAware::LoadIconWithScaleDown(nullptr, icon, cx, cy, &m_hIcon);
1220+
}
12131221
}
12141222
}
12151223

Src/Common/sizecbar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ BEGIN_MESSAGE_MAP(CSizingControlBar, DpiAware::CDpiAwareWnd<baseCSizingControlBa
9696
ON_WM_SIZE()
9797
//}}AFX_MSG_MAP
9898
ON_MESSAGE(WM_SETTEXT, OnSetText)
99+
ON_MESSAGE(WM_DPICHANGED_BEFOREPARENT, OnDpiChangedBeforeParent)
99100
END_MESSAGE_MAP()
100101

101102
BOOL CSizingControlBar::Create(LPCTSTR lpszWindowName,
@@ -1176,6 +1177,23 @@ void CSizingControlBar::GlobalSaveState(CFrameWnd* pFrame,
11761177
}
11771178
}
11781179

1180+
LRESULT CSizingControlBar::OnDpiChangedBeforeParent(WPARAM wParam, LPARAM lParam)
1181+
{
1182+
int olddpi = m_dpi;
1183+
1184+
__super::OnDpiChangedBeforeParent(wParam, lParam);
1185+
1186+
for (auto& size : { &m_szHorz, &m_szVert, &m_szFloat })
1187+
{
1188+
size->cx = MulDiv(size->cx, m_dpi, olddpi);
1189+
size->cy = MulDiv(size->cy, m_dpi, olddpi);
1190+
}
1191+
1192+
m_pDockSite->DelayRecalcLayout();
1193+
1194+
return 0;
1195+
}
1196+
11791197
#ifdef _SCB_REPLACE_MINIFRAME
11801198
#ifndef _SCB_MINIFRAME_CAPTION
11811199
/////////////////////////////////////////////////////////////////////////////

Src/Common/sizecbar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class CSizingControlBar : public DpiAware::CDpiAwareWnd<baseCSizingControlBar>
179179
afx_msg void OnSize(UINT nType, int cx, int cy);
180180
//}}AFX_MSG
181181
afx_msg LRESULT OnSetText(WPARAM wParam, LPARAM lParam);
182+
afx_msg LRESULT OnDpiChangedBeforeParent(WPARAM wParam, LPARAM lParam);
182183

183184
DECLARE_MESSAGE_MAP()
184185

Src/MainFrm.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,12 +2577,8 @@ LRESULT CMainFrame::OnDpiChanged(WPARAM wParam, LPARAM lParam)
25772577
{
25782578
int olddpi = m_dpi;
25792579

2580-
Default();
2581-
25822580
__super::OnDpiChanged(wParam, lParam);
25832581

2584-
RECT* const prcNew = (RECT*)lParam;
2585-
25862582
DpiAware::UpdateAfxDataSysMetrics(m_dpi);
25872583
BCMenu::ReopenTheme(m_dpi);
25882584

@@ -2592,6 +2588,11 @@ LRESULT CMainFrame::OnDpiChanged(WPARAM wParam, LPARAM lParam)
25922588
UpdateFont(FRAME_FILE);
25932589
UpdateFont(FRAME_FOLDER);
25942590

2591+
const RECT* pRect = reinterpret_cast<RECT*>(lParam);
2592+
SetWindowPos(nullptr, pRect->left, pRect->top,
2593+
pRect->right - pRect->left,
2594+
pRect->bottom - pRect->top, SWP_NOZORDER | SWP_NOACTIVATE);
2595+
25952596
return 0;
25962597
}
25972598

0 commit comments

Comments
 (0)