win32_window.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "win32_window.h"
  2. #include <dwmapi.h>
  3. #include <flutter_windows.h>
  4. #include "resource.h"
  5. namespace {
  6. /// Window attribute that enables dark mode window decorations.
  7. ///
  8. /// Redefined in case the developer's machine has a Windows SDK older than
  9. /// version 10.0.22000.0.
  10. /// See: https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
  11. #ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
  12. #define DWMWA_USE_IMMERSIVE_DARK_MODE 20
  13. #endif
  14. #define APP_MUTEX_NAME L"nomo_win.mutex"
  15. #define APP_WINDOW_NAME L"NOMO"
  16. constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";
  17. /// Registry key for app theme preference.
  18. ///
  19. /// A value of 0 indicates apps should use dark mode. A non-zero or missing
  20. /// value indicates apps should use light mode.
  21. constexpr const wchar_t kGetPreferredBrightnessRegKey[] =
  22. L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  23. constexpr const wchar_t kGetPreferredBrightnessRegValue[] = L"AppsUseLightTheme";
  24. // The number of Win32Window objects that currently exist.
  25. static int g_active_window_count = 0;
  26. using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd);
  27. // Scale helper to convert logical scaler values to physical using passed in
  28. // scale factor
  29. int Scale(int source, double scale_factor) {
  30. return static_cast<int>(source * scale_factor);
  31. }
  32. // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module.
  33. // This API is only needed for PerMonitor V1 awareness mode.
  34. void EnableFullDpiSupportIfAvailable(HWND hwnd) {
  35. HMODULE user32_module = LoadLibraryA("User32.dll");
  36. if (!user32_module) {
  37. return;
  38. }
  39. auto enable_non_client_dpi_scaling =
  40. reinterpret_cast<EnableNonClientDpiScaling*>(
  41. GetProcAddress(user32_module, "EnableNonClientDpiScaling"));
  42. if (enable_non_client_dpi_scaling != nullptr) {
  43. enable_non_client_dpi_scaling(hwnd);
  44. }
  45. FreeLibrary(user32_module);
  46. }
  47. } // namespace
  48. // Manages the Win32Window's window class registration.
  49. class WindowClassRegistrar {
  50. public:
  51. ~WindowClassRegistrar() = default;
  52. // Returns the singleton registrar instance.
  53. static WindowClassRegistrar* GetInstance() {
  54. if (!instance_) {
  55. instance_ = new WindowClassRegistrar();
  56. }
  57. return instance_;
  58. }
  59. // Returns the name of the window class, registering the class if it hasn't
  60. // previously been registered.
  61. const wchar_t* GetWindowClass();
  62. // Unregisters the window class. Should only be called if there are no
  63. // instances of the window.
  64. void UnregisterWindowClass();
  65. private:
  66. WindowClassRegistrar() = default;
  67. static WindowClassRegistrar* instance_;
  68. bool class_registered_ = false;
  69. };
  70. WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr;
  71. const wchar_t* WindowClassRegistrar::GetWindowClass() {
  72. if (!class_registered_) {
  73. WNDCLASS window_class{};
  74. window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
  75. window_class.lpszClassName = kWindowClassName;
  76. window_class.style = CS_HREDRAW | CS_VREDRAW;
  77. window_class.cbClsExtra = 0;
  78. window_class.cbWndExtra = 0;
  79. window_class.hInstance = GetModuleHandle(nullptr);
  80. window_class.hIcon =
  81. LoadIcon(window_class.hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
  82. window_class.hbrBackground = 0;
  83. window_class.lpszMenuName = nullptr;
  84. window_class.lpfnWndProc = Win32Window::WndProc;
  85. RegisterClass(&window_class);
  86. class_registered_ = true;
  87. }
  88. return kWindowClassName;
  89. }
  90. void WindowClassRegistrar::UnregisterWindowClass() {
  91. UnregisterClass(kWindowClassName, nullptr);
  92. class_registered_ = false;
  93. }
  94. Win32Window::Win32Window() {
  95. ++g_active_window_count;
  96. }
  97. Win32Window::~Win32Window() {
  98. --g_active_window_count;
  99. Destroy();
  100. }
  101. bool Win32Window::Create(const std::wstring& title,
  102. const Point& origin,
  103. const Size& size) {
  104. // 增加应用程序互斥
  105. HANDLE hMutexHandle = CreateMutex(NULL, TRUE, APP_MUTEX_NAME);
  106. HWND handle = FindWindow(NULL, APP_WINDOW_NAME);
  107. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  108. WINDOWPLACEMENT place = { sizeof(WINDOWPLACEMENT) };
  109. GetWindowPlacement(handle, &place);
  110. switch(place.showCmd) {
  111. case SW_SHOWMAXIMIZED:
  112. ShowWindow(handle, SW_SHOWMAXIMIZED);
  113. break;
  114. case SW_SHOWMINIMIZED:
  115. ShowWindow(handle, SW_RESTORE);
  116. break;
  117. default:
  118. ShowWindow(handle, SW_NORMAL);
  119. break;
  120. }
  121. SetWindowPos(0, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
  122. SetForegroundWindow(handle);
  123. ReleaseMutex(hMutexHandle);
  124. return 0;
  125. }
  126. Destroy();
  127. const wchar_t* window_class =
  128. WindowClassRegistrar::GetInstance()->GetWindowClass();
  129. const POINT target_point = {static_cast<LONG>(origin.x),
  130. static_cast<LONG>(origin.y)};
  131. HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST);
  132. UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
  133. double scale_factor = dpi / 96.0;
  134. HWND window = CreateWindow(
  135. window_class, title.c_str(), WS_OVERLAPPEDWINDOW,
  136. Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
  137. Scale(size.width, scale_factor), Scale(size.height, scale_factor),
  138. nullptr, nullptr, GetModuleHandle(nullptr), this);
  139. if (!window) {
  140. return false;
  141. }
  142. UpdateTheme(window);
  143. return OnCreate();
  144. }
  145. bool Win32Window::Show() {
  146. return ShowWindow(window_handle_, SW_SHOWNORMAL);
  147. }
  148. // static
  149. LRESULT CALLBACK Win32Window::WndProc(HWND const window,
  150. UINT const message,
  151. WPARAM const wparam,
  152. LPARAM const lparam) noexcept {
  153. if (message == WM_NCCREATE) {
  154. auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
  155. SetWindowLongPtr(window, GWLP_USERDATA,
  156. reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
  157. auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
  158. EnableFullDpiSupportIfAvailable(window);
  159. that->window_handle_ = window;
  160. } else if (Win32Window* that = GetThisFromHandle(window)) {
  161. return that->MessageHandler(window, message, wparam, lparam);
  162. }
  163. return DefWindowProc(window, message, wparam, lparam);
  164. }
  165. LRESULT
  166. Win32Window::MessageHandler(HWND hwnd,
  167. UINT const message,
  168. WPARAM const wparam,
  169. LPARAM const lparam) noexcept {
  170. switch (message) {
  171. case WM_DESTROY:
  172. window_handle_ = nullptr;
  173. Destroy();
  174. if (quit_on_close_) {
  175. PostQuitMessage(0);
  176. }
  177. return 0;
  178. case WM_DPICHANGED: {
  179. auto newRectSize = reinterpret_cast<RECT*>(lparam);
  180. LONG newWidth = newRectSize->right - newRectSize->left;
  181. LONG newHeight = newRectSize->bottom - newRectSize->top;
  182. SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
  183. newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
  184. return 0;
  185. }
  186. case WM_SIZE: {
  187. RECT rect = GetClientArea();
  188. if (child_content_ != nullptr) {
  189. // Size and position the child window.
  190. MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
  191. rect.bottom - rect.top, TRUE);
  192. }
  193. return 0;
  194. }
  195. case WM_ACTIVATE:
  196. if (child_content_ != nullptr) {
  197. SetFocus(child_content_);
  198. }
  199. return 0;
  200. case WM_DWMCOLORIZATIONCOLORCHANGED:
  201. UpdateTheme(hwnd);
  202. return 0;
  203. }
  204. return DefWindowProc(window_handle_, message, wparam, lparam);
  205. }
  206. void Win32Window::Destroy() {
  207. OnDestroy();
  208. if (window_handle_) {
  209. DestroyWindow(window_handle_);
  210. window_handle_ = nullptr;
  211. }
  212. if (g_active_window_count == 0) {
  213. WindowClassRegistrar::GetInstance()->UnregisterWindowClass();
  214. }
  215. }
  216. Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
  217. return reinterpret_cast<Win32Window*>(
  218. GetWindowLongPtr(window, GWLP_USERDATA));
  219. }
  220. void Win32Window::SetChildContent(HWND content) {
  221. child_content_ = content;
  222. SetParent(content, window_handle_);
  223. RECT frame = GetClientArea();
  224. MoveWindow(content, frame.left, frame.top, frame.right - frame.left,
  225. frame.bottom - frame.top, true);
  226. SetFocus(child_content_);
  227. }
  228. RECT Win32Window::GetClientArea() {
  229. RECT frame;
  230. GetClientRect(window_handle_, &frame);
  231. return frame;
  232. }
  233. HWND Win32Window::GetHandle() {
  234. return window_handle_;
  235. }
  236. void Win32Window::SetQuitOnClose(bool quit_on_close) {
  237. quit_on_close_ = quit_on_close;
  238. }
  239. bool Win32Window::OnCreate() {
  240. // No-op; provided for subclasses.
  241. return true;
  242. }
  243. void Win32Window::OnDestroy() {
  244. // No-op; provided for subclasses.
  245. }
  246. void Win32Window::UpdateTheme(HWND const window) {
  247. DWORD light_mode;
  248. DWORD light_mode_size = sizeof(light_mode);
  249. LSTATUS result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,
  250. kGetPreferredBrightnessRegValue,
  251. RRF_RT_REG_DWORD, nullptr, &light_mode,
  252. &light_mode_size);
  253. if (result == ERROR_SUCCESS) {
  254. BOOL enable_dark_mode = light_mode == 0;
  255. DwmSetWindowAttribute(window, DWMWA_USE_IMMERSIVE_DARK_MODE,
  256. &enable_dark_mode, sizeof(enable_dark_mode));
  257. }
  258. }