From 7d52e8a022a57261fec32db631a83384bda752f7 Mon Sep 17 00:00:00 2001 From: Zhong Yang Date: Sat, 30 Jul 2022 18:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=BC=95=E5=85=A5GetDpiForMo?= =?UTF-8?q?nitor=E5=87=BD=E6=95=B0=E5=90=8E(#1097)=E5=9C=A8Win7=E4=B8=8B?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E6=89=BE=E4=B8=8D=E5=88=B0dll=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TrafficMonitor/DllFunctions.cpp | 28 +++++++++++++++++++ TrafficMonitor/DllFunctions.h | 21 ++++++++++++++ TrafficMonitor/TaskBarDlg.cpp | 5 +--- TrafficMonitor/TrafficMonitor.h | 3 +- TrafficMonitor/TrafficMonitor.vcxproj | 2 ++ TrafficMonitor/TrafficMonitor.vcxproj.filters | 6 ++++ 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 TrafficMonitor/DllFunctions.cpp create mode 100644 TrafficMonitor/DllFunctions.h diff --git a/TrafficMonitor/DllFunctions.cpp b/TrafficMonitor/DllFunctions.cpp new file mode 100644 index 000000000..78e72da05 --- /dev/null +++ b/TrafficMonitor/DllFunctions.cpp @@ -0,0 +1,28 @@ +#include "stdafx.h" +#include "DllFunctions.h" + +CDllFunctions::CDllFunctions() +{ + //shellscalingapi + m_shcore_module = ::LoadLibrary(_T("Shcore.dll")); + if (m_shcore_module != NULL) + { + m_getDpiForMonitor = (_GetDpiForMonitor)::GetProcAddress(m_shcore_module, "GetDpiForMonitor"); + } +} + +CDllFunctions::~CDllFunctions() +{ + if (m_shcore_module != NULL) + { + FreeLibrary(m_shcore_module); + m_shcore_module = NULL; + } +} + +HRESULT CDllFunctions::GetDpiForMonitor(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT* dpiY) +{ + if (m_getDpiForMonitor != nullptr) + return m_getDpiForMonitor(hmonitor, dpiType, dpiX, dpiY); + return 0; +} diff --git a/TrafficMonitor/DllFunctions.h b/TrafficMonitor/DllFunctions.h new file mode 100644 index 000000000..9c422a131 --- /dev/null +++ b/TrafficMonitor/DllFunctions.h @@ -0,0 +1,21 @@ +#pragma once +#include // ::GetDpiForMonitor + +typedef HRESULT(WINAPI* _GetDpiForMonitor)(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT* dpiY); + +class CDllFunctions +{ +public: + CDllFunctions(); + ~CDllFunctions(); + +public: + HRESULT GetDpiForMonitor(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT* dpiY); + +private: + _GetDpiForMonitor m_getDpiForMonitor{}; + +private: + HMODULE m_shcore_module{}; +}; + diff --git a/TrafficMonitor/TaskBarDlg.cpp b/TrafficMonitor/TaskBarDlg.cpp index e2e00affc..d9b7b8da4 100644 --- a/TrafficMonitor/TaskBarDlg.cpp +++ b/TrafficMonitor/TaskBarDlg.cpp @@ -2,15 +2,12 @@ // #include "stdafx.h" -#include // 包含::GetDpiForMonitor #include "TrafficMonitor.h" #include "TaskBarDlg.h" #include "afxdialogex.h" #include "TrafficMonitorDlg.h" #include "WindowsSettingHelper.h" -#pragma comment(lib, "Shcore.lib") // 函数::GetDpiForMonitor依赖此lib - // CTaskBarDlg 对话框 IMPLEMENT_DYNAMIC(CTaskBarDlg, CDialogEx) @@ -612,7 +609,7 @@ void CTaskBarDlg::DPI(CRect& rect) const void CTaskBarDlg::DPIFromRect(const RECT& rect, UINT* out_dpi_x, UINT* out_dpi_y) { HMONITOR h_current_monitor = ::MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST); - ::GetDpiForMonitor(h_current_monitor, MDT_EFFECTIVE_DPI, out_dpi_x, out_dpi_y); + theApp.m_dll_functions.GetDpiForMonitor(h_current_monitor, MDT_EFFECTIVE_DPI, out_dpi_x, out_dpi_y); } CTaskBarDlg::ClassCheckWindowMonitorDPIAndHandle CTaskBarDlg::CheckWindowMonitorDPIAndHandle{}; diff --git a/TrafficMonitor/TrafficMonitor.h b/TrafficMonitor/TrafficMonitor.h index 62c40eafd..c7147591f 100644 --- a/TrafficMonitor/TrafficMonitor.h +++ b/TrafficMonitor/TrafficMonitor.h @@ -17,6 +17,7 @@ #include #include "OpenHardwareMonitor/OpenHardwareMonitorApi.h" #include "PluginManager.h" +#include "DllFunctions.h" // CTrafficMonitorApp: // 有关此类的实现,请参阅 TrafficMonitor.cpp @@ -82,8 +83,8 @@ class CTrafficMonitorApp : public CWinApp HICON m_notify_icons[MAX_NOTIFY_ICON]; CTaskbarDefaultStyle m_taskbar_default_style; - CPluginManager m_plugins; + CDllFunctions m_dll_functions; CMenu m_main_menu; CMenu m_taskbar_menu; diff --git a/TrafficMonitor/TrafficMonitor.vcxproj b/TrafficMonitor/TrafficMonitor.vcxproj index 33a57d168..121c580f5 100644 --- a/TrafficMonitor/TrafficMonitor.vcxproj +++ b/TrafficMonitor/TrafficMonitor.vcxproj @@ -442,6 +442,7 @@ + @@ -513,6 +514,7 @@ + diff --git a/TrafficMonitor/TrafficMonitor.vcxproj.filters b/TrafficMonitor/TrafficMonitor.vcxproj.filters index 0b695f325..c7e227adb 100644 --- a/TrafficMonitor/TrafficMonitor.vcxproj.filters +++ b/TrafficMonitor/TrafficMonitor.vcxproj.filters @@ -410,6 +410,9 @@ 源文件和头文件\公共的类\WindowsSettingHelper + + 源文件和头文件\头文件 + @@ -607,6 +610,9 @@ 源文件和头文件\公共的类\WindowsSettingHelper + + 源文件和头文件 +