22#include " HutaoNativeWindowSubclass.h"
33#include < CommCtrl.h>
44#include < shobjidl.h>
5+ #include < dwmapi.h>
56
67#pragma comment(lib, "Comctl32.lib")
78#pragma comment(lib, "Ole32.lib")
9+ #pragma comment(lib, "dwmapi.lib")
810
911static LRESULT CALLBACK SubclassWndProc (
1012 HWND hWnd,
@@ -14,6 +16,11 @@ static LRESULT CALLBACK SubclassWndProc(
1416 UINT_PTR uIdSubclass,
1517 DWORD_PTR dwRefData)
1618{
19+ if (uMsg == WM_PAINT)
20+ {
21+ ThrowForHR (DwmFlush (), " Failed to perform DwmFlush in WM_PAINT" );
22+ }
23+
1724 HutaoNativeWindowSubclass* pThis = reinterpret_cast <HutaoNativeWindowSubclass*>(dwRefData);
1825 if (pThis == nullptr )
1926 {
@@ -23,8 +30,8 @@ static LRESULT CALLBACK SubclassWndProc(
2330 if (pThis->m_callback .has_value ())
2431 {
2532 LRESULT lResult = 0 ;
26- BOOL handled = pThis->m_callback .value ()(hWnd, uMsg, wParam, lParam, pThis->m_userData , &lResult);
27- if (handled )
33+ BOOL kontinue = pThis->m_callback .value ()(hWnd, uMsg, wParam, lParam, pThis->m_userData , &lResult);
34+ if (!kontinue )
2835 {
2936 return lResult;
3037 }
@@ -34,7 +41,7 @@ static LRESULT CALLBACK SubclassWndProc(
3441}
3542
3643HutaoNativeWindowSubclass::HutaoNativeWindowSubclass (HWND hWnd, HutaoNativeWindowSubclassCallback callback, GCHandle userData)
37- : mWnd (hWnd)
44+ : m_hWnd (hWnd)
3845 , m_callback(callback)
3946 , m_userData(userData)
4047 , m_originalWndProc(nullptr )
@@ -54,19 +61,26 @@ HRESULT __stdcall HutaoNativeWindowSubclass::Attach()
5461{
5562 if (m_attached)
5663 {
57- return S_FALSE; // 已经附加
64+ return S_FALSE;
5865 }
5966
60- if (mWnd == nullptr || !IsWindow (mWnd ))
67+ if (m_hWnd == nullptr || !IsWindow (m_hWnd ))
6168 {
6269 return E_INVALIDARG;
6370 }
6471
65- // 使用SetWindowSubclass进行子类化
72+ BOOL IsCompositionEnabled;
73+ ThrowForHRAndReturn (DwmIsCompositionEnabled (&IsCompositionEnabled), " Failed to determine whether desktop composition is enabled" );
74+
75+ if (!IsCompositionEnabled)
76+ {
77+ ThrowForHRAndReturn (E_UNEXPECTED, " Failed to determine whether desktop composition is enabled" );
78+ }
79+
6680 BOOL result = SetWindowSubclass (
67- mWnd ,
81+ m_hWnd ,
6882 SubclassWndProc,
69- 1 , // 子类ID
83+ WINDOW_SUBCLASS_ID,
7084 reinterpret_cast <DWORD_PTR>(this ));
7185
7286 if (result)
@@ -84,19 +98,18 @@ HRESULT __stdcall HutaoNativeWindowSubclass::Detach()
8498{
8599 if (!m_attached)
86100 {
87- return S_FALSE; // 已经分离
101+ return S_FALSE;
88102 }
89103
90- if (mWnd == nullptr || !IsWindow (mWnd ))
104+ if (m_hWnd == nullptr || !IsWindow (m_hWnd ))
91105 {
92106 return E_INVALIDARG;
93107 }
94108
95- // 移除子类化
96109 BOOL result = RemoveWindowSubclass (
97- mWnd ,
110+ m_hWnd ,
98111 SubclassWndProc,
99- 1 ); // 子类ID
112+ WINDOW_SUBCLASS_ID);
100113
101114 if (result)
102115 {
@@ -109,7 +122,6 @@ HRESULT __stdcall HutaoNativeWindowSubclass::Detach()
109122 }
110123}
111124
112- // HutaoNativeWindowSubclass2 实现
113125HutaoNativeWindowSubclass2::HutaoNativeWindowSubclass2 ()
114126 : m_initialized(false )
115127 , m_pTaskbarList(nullptr )
@@ -129,10 +141,9 @@ HRESULT __stdcall HutaoNativeWindowSubclass2::InitializeTaskbarProgress()
129141{
130142 if (m_initialized)
131143 {
132- return S_FALSE; // 已经初始化
144+ return S_FALSE;
133145 }
134146
135- // 初始化通用控件
136147 INITCOMMONCONTROLSEX icex;
137148 icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
138149 icex.dwICC = ICC_PROGRESS_CLASS;
0 commit comments