win32_window.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if (!window) {
  100. return false;
  101. }
  102. return OnCreate();
  103. }
  104. // static
  105. LRESULT CALLBACK Win32Window::WndProc(HWND const window,
  106. UINT const message,
  107. WPARAM const wparam,
  108. LPARAM const lparam) noexcept {
  109. if (message == WM_NCCREATE) {
  110. auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
  111. SetWindowLongPtr(window, GWLP_USERDATA,
  112. reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
  113. auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
  114. EnableFullDpiSupportIfAvailable(window);
  115. that->window_handle_ = window;
  116. } else if (Win32Window* that = GetThisFromHandle(window)) {
  117. return that->MessageHandler(window, message, wparam, lparam);
  118. }
  119. return DefWindowProc(window, message, wparam, lparam);
  120. }
  121. LRESULT
  122. Win32Window::MessageHandler(HWND hwnd,
  123. UINT const message,
  124. WPARAM const wparam,
  125. LPARAM const lparam) noexcept {
  126. switch (message) {
  127. case WM_DESTROY:
  128. window_handle_ = nullptr;
  129. Destroy();
  130. if (quit_on_close_) {
  131. PostQuitMessage(0);
  132. }
  133. return 0;
  134. case WM_DPICHANGED: {
  135. auto newRectSize = reinterpret_cast<RECT*>(lparam);
  136. LONG newWidth = newRectSize->right - newRectSize->left;
  137. LONG newHeight = newRectSize->bottom - newRectSize->top;
  138. SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
  139. newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
  140. return 0;
  141. }
  142. case WM_SIZE: {
  143. RECT rect = GetClientArea();
  144. if (child_content_ != nullptr) {
  145. // Size and position the child window.
  146. MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
  147. rect.bottom - rect.top, TRUE);
  148. }
  149. return 0;
  150. }
  151. case WM_ACTIVATE:
  152. if (child_content_ != nullptr) {
  153. SetFocus(child_content_);
  154. }
  155. return 0;
  156. }
  157. return DefWindowProc(window_handle_, message, wparam, lparam);
  158. }
  159. void Win32Window::Destroy() {
  160. OnDestroy();
  161. if (window_handle_) {
  162. DestroyWindow(window_handle_);
  163. window_handle_ = nullptr;
  164. }
  165. if (g_active_window_count == 0) {
  166. WindowClassRegistrar::GetInstance()->UnregisterWindowClass();
  167. }
  168. }
  169. Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
  170. return reinterpret_cast<Win32Window*>(
  171. GetWindowLongPtr(window, GWLP_USERDATA));
  172. }
  173. void Win32Window::SetChildContent(HWND content) {
  174. child_content_ = content;
  175. SetParent(content, window_handle_);
  176. RECT frame = GetClientArea();
  177. MoveWindow(content, frame.left, frame.top, frame.right - frame.left,
  178. frame.bottom - frame.top, true);
  179. SetFocus(child_content_);
  180. }
  181. RECT Win32Window::GetClientArea() {
  182. RECT frame;
  183. GetClientRect(window_handle_, &frame);
  184. return frame;
  185. }
  186. HWND Win32Window::GetHandle() {
  187. return window_handle_;
  188. }
  189. void Win32Window::SetQuitOnClose(bool quit_on_close) {
  190. quit_on_close_ = quit_on_close;
  191. }
  192. bool Win32Window::OnCreate() {
  193. // No-op; provided for subclasses.
  194. return true;
  195. }
  196. void Win32Window::OnDestroy() {
  197. // No-op; provided for subclasses.
  198. }