You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 rivejä
2.5 KiB

  1. //
  2. // Created by yangbin on 2021/11/11.
  3. //
  4. #include <windows.h>
  5. #include "utils.h"
  6. #include <memory>
  7. #include <set>
  8. namespace webview_window {
  9. void ClipOrCenterRectToMonitor(LPRECT prc, UINT flags) {
  10. HMONITOR hMonitor;
  11. MONITORINFO mi;
  12. RECT rc;
  13. int w = prc->right - prc->left;
  14. int h = prc->bottom - prc->top;
  15. //
  16. // get the nearest monitor to the passed rect.
  17. //
  18. hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
  19. //
  20. // get the work area or entire monitor rect.
  21. //
  22. mi.cbSize = sizeof(mi);
  23. GetMonitorInfo(hMonitor, &mi);
  24. if (flags & MONITOR_WORKAREA)
  25. rc = mi.rcWork;
  26. else
  27. rc = mi.rcMonitor;
  28. //
  29. // center or clip the passed rect to the monitor rect
  30. //
  31. if (flags & MONITOR_CENTER) {
  32. prc->left = rc.left + (rc.right - rc.left - w) / 2;
  33. prc->top = rc.top + (rc.bottom - rc.top - h) / 2;
  34. prc->right = prc->left + w;
  35. prc->bottom = prc->top + h;
  36. } else {
  37. prc->left = max(rc.left, min(rc.right - w, prc->left));
  38. prc->top = max(rc.top, min(rc.bottom - h, prc->top));
  39. prc->right = prc->left + w;
  40. prc->bottom = prc->top + h;
  41. }
  42. }
  43. void ClipOrCenterWindowToMonitor(HWND hwnd, UINT flags) {
  44. RECT rc;
  45. GetWindowRect(hwnd, &rc);
  46. ClipOrCenterRectToMonitor(&rc, flags);
  47. SetWindowPos(hwnd, nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  48. }
  49. bool SetWindowBackgroundTransparent(HWND hwnd) {
  50. // TODO
  51. return false;
  52. }
  53. static std::unique_ptr<std::set<LPCWSTR>> class_registered_;
  54. const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc) {
  55. if (!class_registered_ || class_registered_->count(class_name) == 0) {
  56. if (!class_registered_) {
  57. class_registered_ = std::make_unique<std::set<LPCWSTR>>();
  58. }
  59. WNDCLASS window_class{};
  60. window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
  61. window_class.lpszClassName = class_name;
  62. window_class.style = CS_HREDRAW | CS_VREDRAW;
  63. window_class.cbClsExtra = 0;
  64. window_class.cbWndExtra = 0;
  65. window_class.hInstance = GetModuleHandle(nullptr);
  66. window_class.hIcon =
  67. LoadIcon(window_class.hInstance, IDI_APPLICATION);
  68. window_class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  69. window_class.lpszMenuName = nullptr;
  70. window_class.lpfnWndProc = wnd_proc;
  71. RegisterClass(&window_class);
  72. class_registered_->insert(class_name);
  73. }
  74. return class_name;
  75. }
  76. void UnregisterWindowClass(LPCWSTR class_name) {
  77. if (!class_registered_) {
  78. return;
  79. }
  80. class_registered_->erase(class_name);
  81. UnregisterClass(class_name, nullptr);
  82. }
  83. } // namespace webview_window