Magpie/tools/WindowCase/WindowBase.h
Xu fb2270ec89
优化 Graphics Capture 对 Kirikiri 窗口的支持 (#1238)
* feat: 尝试模拟 kirikiri 窗口行为

* feat: 完善模拟 kirikiri 窗口

* feat: 优化 WGC 对 kirikiri 窗口的处理

* chore: 优化注释

* fix: 优化错误处理

* chore: 添加注释
2025-08-09 21:20:34 +08:00

57 lines
1.2 KiB
C++

#pragma once
template <typename T>
class WindowBaseT {
public:
WindowBaseT() noexcept = default;
WindowBaseT(const WindowBaseT&) = delete;
WindowBaseT(WindowBaseT&&) noexcept = default;
HWND Handle() const noexcept {
return _hWnd;
}
operator bool() const noexcept {
return _hWnd;
}
void Destroy() const noexcept {
if (_hWnd) {
DestroyWindow(_hWnd);
}
}
protected:
// 确保无法通过基类指针删除这个对象
~WindowBaseT() noexcept {
Destroy();
}
static LRESULT CALLBACK _WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
if (msg == WM_NCCREATE) {
WindowBaseT* that = (WindowBaseT*)(((CREATESTRUCT*)lParam)->lpCreateParams);
assert(that && !that->_hWnd);
that->_hWnd = hWnd;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)that);
} else if (T* that = (T*)GetWindowLongPtr(hWnd, GWLP_USERDATA)) {
return that->_MessageHandler(msg, wParam, lParam);
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
LRESULT _MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
switch (msg) {
case WM_DESTROY:
{
_hWnd = NULL;
return 0;
}
}
return DefWindowProc(_hWnd, msg, wParam, lParam);
}
private:
HWND _hWnd = NULL;
};