Skip to content

Commit 23a58eb

Browse files
committed
修复错误的SubclassWndProc逻辑
1 parent 82bbbba commit 23a58eb

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/Snap.Hutao.Remastered.Native/Error.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
#include <intrin.h>
55

66
#define ThrowForHR(hr, message) \
7-
ThrowForHRCore(_ReturnAddress(), __LINE__, __FUNCTION__, __FILE__, hr, L##message)
7+
if (FAILED(hr)) \
8+
ThrowForHRCore(_ReturnAddress(), __LINE__, __FUNCTION__, __FILE__, hr, L##message)
9+
#define ThrowForHRAndReturn(hr, message) \
10+
if (FAILED(hr)) \
11+
{ \
12+
ThrowForHRCore(_ReturnAddress(), __LINE__, __FUNCTION__, __FILE__, hr, L##message); \
13+
return hr; \
14+
}
815
#define LogForHR(hr) \
916
LogForHRCore(_ReturnAddress(), __LINE__, __FUNCTION__, __FILE__, hr)
1017
#define LogMessageForHR(hr, message) \

src/Snap.Hutao.Remastered.Native/HutaoNativeWindowSubclass.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
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

911
static 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

3643
HutaoNativeWindowSubclass::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 实现
113125
HutaoNativeWindowSubclass2::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;

src/Snap.Hutao.Remastered.Native/HutaoNativeWindowSubclass.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <CommCtrl.h>
88
#include <shobjidl.h>
99

10+
#define WINDOW_SUBCLASS_ID 0x65
11+
1012
LRESULT CALLBACK SubclassWndProc(
1113
HWND hWnd,
1214
UINT uMsg,
@@ -28,7 +30,7 @@ class HutaoNativeWindowSubclass :
2830
virtual HRESULT __stdcall Detach() override;
2931

3032
private:
31-
HWND mWnd;
33+
HWND m_hWnd;
3234
HutaoNativeWindowSubclassCallback m_callback;
3335
GCHandle m_userData;
3436
WNDPROC m_originalWndProc;

0 commit comments

Comments
 (0)