win32_window.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "win32_window.h"
  2. #include <flutter_windows.h>
  3. #include "resource.h"
  4. namespace {
  5. constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";
  6. // The number of Win32Window objects that currently exist.
  7. static int g_active_window_count = 0;
  8. using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd);
  9. // Scale helper to convert logical scaler values to physical using passed in
  10. // scale factor
  11. int Scale(int source, double scale_factor) {
  12. return static_cast<int>(source * scale_factor);
  13. }
  14. // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module.
  15. // This API is only needed for PerMonitor V1 awareness mode.
  16. void EnableFullDpiSupportIfAvailable(HWND hwnd) {
  17. HMODULE user32_module = LoadLibraryA("User32.dll");
  18. if (!user32_module) {
  19. return;
  20. }
  21. auto enable_non_client_dpi_scaling =
  22. reinterpret_cast<EnableNonClientDpiScaling*>(
  23. GetProcAddress(user32_module, "EnableNonClientDpiScaling"));
  24. if (enable_non_client_dpi_scaling != nullptr) {
  25. enable_non_client_dpi_scaling(hwnd);
  26. FreeLibrary(user32_module);
  27. }
  28. }
  29. } // namespace
  30. // Manages the Win32Window's window class registration.
  31. class WindowClassRegistrar {
  32. public:
  33. ~WindowClassRegistrar() = default;
  34. // Returns the singleton registar instance.
  35. static WindowClassRegistrar* GetInstance() {
  36. if (!instance_) {
  37. instance_ = new WindowClassRegistrar();
  38. }
  39. return instance_;
  40. }
  41. // Returns the name of the window class, registering the class if it hasn't
  42. // previously been registered.
  43. const wchar_t* GetWindowClass();
  44. // Unregisters the window class. Should only be called if there are no
  45. // instances of the window.
  46. void UnregisterWindowClass();
  47. private:
  48. WindowClassRegistrar() = default;
  49. static WindowClassRegistrar* instance_;
  50. bool class_registered_ = false;
  51. };
  52. WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr;
  53. const wchar_t* WindowClassRegistrar::GetWindowClass() {
  54. if (!class_registered_) {
  55. WNDCLASS window_class{};
  56. window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
  57. window_class.lpszClassName = kWindowClassName;
  58. window_class.style = CS_HREDRAW | CS_VREDRAW;
  59. window_class.cbClsExtra = 0;
  60. window_class.cbWndExtra = 0;
  61. window_class.hInstance = GetModuleHandle(nullptr);
  62. window_class.hIcon =
  63. LoadIcon(window_class.hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
  64. window_class.hbrBackground = 0;
  65. window_class.lpszMenuName = nullptr;
  66. window_class.lpfnWndProc = Win32Window::WndProc;
  67. RegisterClass(&window_class);
  68. class_registered_ = true;
  69. }
  70. return kWindowClassName;
  71. }
  72. void WindowClassRegistrar::UnregisterWindowClass() {
  73. UnregisterClass(kWindowClassName, nullptr);
  74. class_registered_ = false;
  75. }
  76. Win32Window::Win32Window() {
  77. ++g_active_window_count;
  78. }
  79. Win32Window::~Win32Window() {
  80. --g_active_window_count;
  81. Destroy();
  82. }
  83. bool Win32Window::CreateAndShow(const std::wstring& title,
  84. const Point& origin,
  85. const Size& size) {
  86. Destroy();
  87. const wchar_t* window_class =
  88. WindowClassRegistrar::GetInstance()->GetWindowClass();
  89. const POINT target_point = {static_cast<LONG>(origin.x),
  90. static_cast<LONG>(origin.y)};
  91. HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST);
  92. UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
  93. double scale_factor = dpi / 96.0;
  94. HWND window = CreateWindow(
  95. window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  96. Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
  97. Scale(size.width, scale_factor), Scale(size.height, scale_factor),
  98. nullptr, nullptr, GetModuleHandle(nullptr), this);
  99. OnCreate();
  100. return window != nullptr;
  101. }
  102. // static
  103. LRESULT CALLBACK Win32Window::WndProc(HWND const window,
  104. UINT const message,
  105. WPARAM const wparam,
  106. LPARAM const lparam) noexcept {
  107. if (message == WM_NCCREATE) {
  108. auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
  109. SetWindowLongPtr(window, GWLP_USERDATA,
  110. reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
  111. auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
  112. EnableFullDpiSupportIfAvailable(window);
  113. that->window_handle_ = window;
  114. } else if (Win32Window* that = GetThisFromHandle(window)) {
  115. return that->MessageHandler(window, message, wparam, lparam);
  116. }
  117. return DefWindowProc(window, message, wparam, lparam);
  118. }
  119. LRESULT
  120. Win32Window::MessageHandler(HWND hwnd,
  121. UINT const message,
  122. WPARAM const wparam,
  123. LPARAM const lparam) noexcept {
  124. auto window =
  125. reinterpret_cast<Win32Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
  126. if (window == nullptr) {
  127. return 0;
  128. }
  129. switch (message) {
  130. case WM_DESTROY:
  131. window_handle_ = nullptr;
  132. Destroy();
  133. if (quit_on_close_) {
  134. PostQuitMessage(0);
  135. }
  136. return 0;
  137. case WM_DPICHANGED: {
  138. auto newRectSize = reinterpret_cast<RECT*>(lparam);
  139. LONG newWidth = newRectSize->right - newRectSize->left;
  140. LONG newHeight = newRectSize->bottom - newRectSize->top;
  141. SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
  142. newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
  143. return 0;
  144. }
  145. case WM_SIZE:
  146. RECT rect;
  147. GetClientRect(hwnd, &rect);
  148. if (child_content_ != nullptr) {
  149. // Size and position the child window.
  150. MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
  151. rect.bottom - rect.top, TRUE);
  152. }
  153. return 0;
  154. case WM_ACTIVATE:
  155. if (child_content_ != nullptr) {
  156. SetFocus(child_content_);
  157. }
  158. return 0;
  159. // Messages that are directly forwarded to embedding.
  160. case WM_FONTCHANGE:
  161. SendMessage(child_content_, WM_FONTCHANGE, NULL, NULL);
  162. return 0;
  163. }
  164. return DefWindowProc(window_handle_, message, wparam, lparam);
  165. }
  166. void Win32Window::Destroy() {
  167. OnDestroy();
  168. if (window_handle_) {
  169. DestroyWindow(window_handle_);
  170. window_handle_ = nullptr;
  171. }
  172. if (g_active_window_count == 0) {
  173. WindowClassRegistrar::GetInstance()->UnregisterWindowClass();
  174. }
  175. }
  176. Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
  177. return reinterpret_cast<Win32Window*>(
  178. GetWindowLongPtr(window, GWLP_USERDATA));
  179. }
  180. void Win32Window::SetChildContent(HWND content) {
  181. child_content_ = content;
  182. SetParent(content, window_handle_);
  183. RECT frame;
  184. GetClientRect(window_handle_, &frame);
  185. MoveWindow(content, frame.left, frame.top, frame.right - frame.left,
  186. frame.bottom - frame.top, true);
  187. SetFocus(child_content_);
  188. }
  189. HWND Win32Window::GetHandle() {
  190. return window_handle_;
  191. }
  192. void Win32Window::SetQuitOnClose(bool quit_on_close) {
  193. quit_on_close_ = quit_on_close;
  194. }
  195. void Win32Window::OnCreate() {
  196. // No-op; provided for subclasses.
  197. }
  198. void Win32Window::OnDestroy() {
  199. // No-op; provided for subclasses.
  200. }