win32_window.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "win32_window.h"
  2. #include <flutter_windows.h>
  3. #include "resource.h"
  4. #define IDR_PAUSE 12
  5. #define IDR_START 13
  6. namespace {
  7. constexpr const wchar_t kWindowClassName[] = L"O2OA";
  8. // The number of Win32Window objects that currently exist.
  9. static int g_active_window_count = 0;
  10. using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd);
  11. // Scale helper to convert logical scaler values to physical using passed in
  12. // scale factor
  13. int Scale(int source, double scale_factor) {
  14. return static_cast<int>(source * scale_factor);
  15. }
  16. // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module.
  17. // This API is only needed for PerMonitor V1 awareness mode.
  18. void EnableFullDpiSupportIfAvailable(HWND hwnd) {
  19. HMODULE user32_module = LoadLibraryA("User32.dll");
  20. if (!user32_module) {
  21. return;
  22. }
  23. auto enable_non_client_dpi_scaling =
  24. reinterpret_cast<EnableNonClientDpiScaling*>(
  25. GetProcAddress(user32_module, "EnableNonClientDpiScaling"));
  26. if (enable_non_client_dpi_scaling != nullptr) {
  27. enable_non_client_dpi_scaling(hwnd);
  28. FreeLibrary(user32_module);
  29. }
  30. }
  31. } // namespace
  32. // Manages the Win32Window's window class registration.
  33. class WindowClassRegistrar {
  34. public:
  35. ~WindowClassRegistrar() = default;
  36. // Returns the singleton registar instance.
  37. static WindowClassRegistrar* GetInstance() {
  38. if (!instance_) {
  39. instance_ = new WindowClassRegistrar();
  40. }
  41. return instance_;
  42. }
  43. // Returns the name of the window class, registering the class if it hasn't
  44. // previously been registered.
  45. const wchar_t* GetWindowClass();
  46. // Unregisters the window class. Should only be called if there are no
  47. // instances of the window.
  48. void UnregisterWindowClass();
  49. private:
  50. WindowClassRegistrar() = default;
  51. static WindowClassRegistrar* instance_;
  52. bool class_registered_ = false;
  53. };
  54. WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr;
  55. const wchar_t* WindowClassRegistrar::GetWindowClass() {
  56. if (!class_registered_) {
  57. WNDCLASS window_class{};
  58. window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
  59. window_class.lpszClassName = kWindowClassName;
  60. window_class.style = CS_HREDRAW | CS_VREDRAW;
  61. window_class.cbClsExtra = 0;
  62. window_class.cbWndExtra = 0;
  63. window_class.hInstance = GetModuleHandle(nullptr);
  64. window_class.hIcon =
  65. LoadIcon(window_class.hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
  66. window_class.hbrBackground = 0;
  67. window_class.lpszMenuName = nullptr;
  68. window_class.lpfnWndProc = Win32Window::WndProc;
  69. RegisterClass(&window_class);
  70. class_registered_ = true;
  71. }
  72. return kWindowClassName;
  73. }
  74. void WindowClassRegistrar::UnregisterWindowClass() {
  75. UnregisterClass(kWindowClassName, nullptr);
  76. class_registered_ = false;
  77. }
  78. Win32Window::Win32Window() {
  79. ++g_active_window_count;
  80. }
  81. Win32Window::~Win32Window() {
  82. --g_active_window_count;
  83. Destroy();
  84. }
  85. bool Win32Window::CreateAndShow(const std::wstring& title,
  86. const Point& origin,
  87. const Size& size) {
  88. Destroy();
  89. const wchar_t* window_class =
  90. WindowClassRegistrar::GetInstance()->GetWindowClass();
  91. const POINT target_point = { static_cast<LONG>(origin.x),
  92. static_cast<LONG>(origin.y) };
  93. HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST);
  94. UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
  95. double scale_factor = dpi / 96.0;
  96. HWND window = CreateWindow(
  97. window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  98. Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
  99. Scale(size.width, scale_factor), Scale(size.height, scale_factor),
  100. nullptr, nullptr, GetModuleHandle(nullptr), this);
  101. if (!window) {
  102. return false;
  103. }
  104. return OnCreate();
  105. }
  106. // static
  107. LRESULT CALLBACK Win32Window::WndProc(HWND const window,
  108. UINT const message,
  109. WPARAM const wparam,
  110. LPARAM const lparam) noexcept {
  111. if (message == WM_NCCREATE) {
  112. auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
  113. SetWindowLongPtr(window, GWLP_USERDATA,
  114. reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
  115. auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
  116. EnableFullDpiSupportIfAvailable(window);
  117. that->window_handle_ = window;
  118. }
  119. else if (Win32Window* that = GetThisFromHandle(window)) {
  120. return that->MessageHandler(window, message, wparam, lparam);
  121. }
  122. return DefWindowProc(window, message, wparam, lparam);
  123. }
  124. HMENU hmenu;
  125. LRESULT
  126. Win32Window::MessageHandler(HWND hwnd,
  127. UINT const message,
  128. WPARAM const wparam,
  129. LPARAM const lparam) noexcept {
  130. //NOTIFYICONDATA nid;
  131. // UINT WM_TASKBARCREATED;
  132. // POINT pt;
  133. //int xx;
  134. //WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
  135. switch (message) {
  136. case WM_USER:
  137. //if (lparam == WM_LBUTTONDOWN) {
  138. // Shell_NotifyIcon(NIM_DELETE, &m_trayIcon);
  139. // ShowWindow(hwnd, SW_SHOWNORMAL);
  140. //}
  141. // 双击恢复主界面窗口
  142. if (lparam == WM_LBUTTONDBLCLK) {
  143. SetForegroundWindow(hwnd);
  144. ShowWindow(hwnd, SW_SHOWNORMAL);
  145. }
  146. // 右键显示菜单
  147. if (lparam == WM_RBUTTONDOWN)
  148. {
  149. POINT pt;
  150. int xx;
  151. GetCursorPos(&pt);
  152. ::SetForegroundWindow(hwnd);
  153. //EnableMenuItem(hmenu, IDR_PAUSE, MF_GRAYED);
  154. xx = TrackPopupMenu(hmenu, TPM_RETURNCMD, pt.x, pt.y, NULL, hwnd, NULL);
  155. if (xx == IDR_PAUSE) {// 恢复主界面窗口
  156. SetForegroundWindow(hwnd);
  157. ShowWindow(hwnd, SW_SHOWNORMAL);
  158. }
  159. if (xx == IDR_START) {// 退出程序
  160. Win32Window::quitAndDestroy(hwnd);
  161. }
  162. if (xx == 0) PostMessage(hwnd, WM_LBUTTONDOWN, NULL, NULL);
  163. }
  164. return 0;
  165. case WM_DESTROY:
  166. printf("dddd WM_DESTROY 这里好像执行不到 ");
  167. Win32Window::quitAndDestroy(hwnd);
  168. //window_handle_ = nullptr;
  169. //Destroy();
  170. //if (quit_on_close_) {
  171. // Shell_NotifyIcon(NIM_DELETE, &nid);
  172. // PostQuitMessage(0);
  173. //}
  174. return 0;
  175. case WM_DPICHANGED: {
  176. auto newRectSize = reinterpret_cast<RECT*>(lparam);
  177. LONG newWidth = newRectSize->right - newRectSize->left;
  178. LONG newHeight = newRectSize->bottom - newRectSize->top;
  179. SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
  180. newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
  181. return 0;
  182. }
  183. case WM_SIZE: {
  184. if (wparam == SIZE_MINIMIZED) {
  185. printf("ssssss WM_SIZE 最小化到托盘");
  186. Win32Window::AddTrayIcon(hwnd);
  187. }
  188. RECT rect = GetClientArea();
  189. if (child_content_ != nullptr) {
  190. // Size and position the child window.
  191. MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
  192. rect.bottom - rect.top, TRUE);
  193. }
  194. return 0;
  195. }
  196. case WM_ACTIVATE:
  197. if (child_content_ != nullptr) {
  198. SetFocus(child_content_);
  199. }
  200. return 0;
  201. }
  202. //if (message == WM_TASKBARCREATED) {
  203. // SendMessage(hwnd, WM_CREATE, wparam, lparam);
  204. //}
  205. return DefWindowProc(window_handle_, message, wparam, lparam);
  206. }
  207. // 退出程序 删除托盘图标
  208. void Win32Window::quitAndDestroy(HWND hwnd) {
  209. window_handle_ = nullptr;
  210. Destroy();
  211. if (quit_on_close_) {
  212. Shell_NotifyIcon(NIM_DELETE, &m_trayIcon);
  213. PostQuitMessage(0);
  214. }
  215. }
  216. // 添加托盘图标
  217. void Win32Window::AddTrayIcon(HWND hwnd) {
  218. memset(&m_trayIcon, 0, sizeof(NOTIFYICONDATA));
  219. m_trayIcon.cbSize = sizeof(NOTIFYICONDATA);
  220. //m_trayIcon.hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
  221. //m_trayIcon.hWnd = m_hWnd;
  222. //m_trayIcon.cbSize = sizeof(m_trayIcon);
  223. m_trayIcon.hWnd = hwnd;
  224. //m_trayIcon.uID = 0;
  225. m_trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  226. m_trayIcon.uCallbackMessage = WM_USER;
  227. m_trayIcon.hIcon = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_APP_ICON));
  228. lstrcpy(m_trayIcon.szTip, kWindowClassName);
  229. Shell_NotifyIcon(NIM_ADD, &m_trayIcon);
  230. hmenu = CreatePopupMenu();//
  231. AppendMenu(hmenu, MF_STRING, IDR_PAUSE, L"打开");
  232. AppendMenu(hmenu, MF_STRING, IDR_START, L"退出");
  233. ShowWindow(hwnd, SW_HIDE);
  234. }
  235. void Win32Window::Destroy() {
  236. OnDestroy();
  237. if (window_handle_) {
  238. DestroyWindow(window_handle_);
  239. window_handle_ = nullptr;
  240. }
  241. if (g_active_window_count == 0) {
  242. WindowClassRegistrar::GetInstance()->UnregisterWindowClass();
  243. }
  244. }
  245. Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
  246. return reinterpret_cast<Win32Window*>(
  247. GetWindowLongPtr(window, GWLP_USERDATA));
  248. }
  249. void Win32Window::SetChildContent(HWND content) {
  250. child_content_ = content;
  251. SetParent(content, window_handle_);
  252. RECT frame = GetClientArea();
  253. MoveWindow(content, frame.left, frame.top, frame.right - frame.left,
  254. frame.bottom - frame.top, true);
  255. SetFocus(child_content_);
  256. }
  257. RECT Win32Window::GetClientArea() {
  258. RECT frame;
  259. GetClientRect(window_handle_, &frame);
  260. return frame;
  261. }
  262. HWND Win32Window::GetHandle() {
  263. return window_handle_;
  264. }
  265. void Win32Window::SetQuitOnClose(bool quit_on_close) {
  266. quit_on_close_ = quit_on_close;
  267. }
  268. bool Win32Window::OnCreate() {
  269. // No-op; provided for subclasses.
  270. return true;
  271. }
  272. void Win32Window::OnDestroy() {
  273. // No-op; provided for subclasses.
  274. }