win32_window.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef RUNNER_WIN32_WINDOW_H_
  2. #define RUNNER_WIN32_WINDOW_H_
  3. #include <windows.h>
  4. #include <functional>
  5. #include <memory>
  6. #include <string>
  7. // A class abstraction for a high DPI-aware Win32 Window. Intended to be
  8. // inherited from by classes that wish to specialize with custom
  9. // rendering and input handling
  10. class Win32Window
  11. {
  12. public:
  13. struct Point
  14. {
  15. unsigned int x;
  16. unsigned int y;
  17. Point(unsigned int x, unsigned int y) : x(x), y(y) {}
  18. };
  19. struct Size
  20. {
  21. unsigned int width;
  22. unsigned int height;
  23. Size(unsigned int width, unsigned int height)
  24. : width(width), height(height) {}
  25. };
  26. Win32Window();
  27. virtual ~Win32Window();
  28. // Creates and shows a win32 window with |title| and position and size using
  29. // |origin| and |size|. New windows are created on the default monitor. Window
  30. // sizes are specified to the OS in physical pixels, hence to ensure a
  31. // consistent size to will treat the width height passed in to this function
  32. // as logical pixels and scale to appropriate for the default monitor. Returns
  33. // true if the window was created successfully.
  34. bool CreateAndShow(const std::wstring &title,
  35. const Point &origin,
  36. const Size &size);
  37. // Dispatches link if any.
  38. // This method enables our app to be with a single instance too.
  39. bool SendAppLinkToInstance(const std::wstring &title);
  40. // Release OS resources associated with window.
  41. void Destroy();
  42. // Inserts |content| into the window tree.
  43. void SetChildContent(HWND content);
  44. // Returns the backing Window handle to enable clients to set icon and other
  45. // window properties. Returns nullptr if the window has been destroyed.
  46. HWND GetHandle();
  47. // If true, closing this window will quit the application.
  48. void SetQuitOnClose(bool quit_on_close);
  49. // Return a RECT representing the bounds of the current client area.
  50. RECT GetClientArea();
  51. protected:
  52. // Processes and route salient window messages for mouse handling,
  53. // size change and DPI. Delegates handling of these to member overloads that
  54. // inheriting classes can handle.
  55. virtual LRESULT MessageHandler(HWND window,
  56. UINT const message,
  57. WPARAM const wparam,
  58. LPARAM const lparam) noexcept;
  59. // Called when CreateAndShow is called, allowing subclass window-related
  60. // setup. Subclasses should return false if setup fails.
  61. virtual bool OnCreate();
  62. // Called when Destroy is called.
  63. virtual void OnDestroy();
  64. private:
  65. friend class WindowClassRegistrar;
  66. // OS callback called by message pump. Handles the WM_NCCREATE message which
  67. // is passed when the non-client area is being created and enables automatic
  68. // non-client DPI scaling so that the non-client area automatically
  69. // responsponds to changes in DPI. All other messages are handled by
  70. // MessageHandler.
  71. static LRESULT CALLBACK WndProc(HWND const window,
  72. UINT const message,
  73. WPARAM const wparam,
  74. LPARAM const lparam) noexcept;
  75. // Retrieves a class instance pointer for |window|
  76. static Win32Window *GetThisFromHandle(HWND const window) noexcept;
  77. bool quit_on_close_ = false;
  78. // window handle for top level window.
  79. HWND window_handle_ = nullptr;
  80. // window handle for hosted content.
  81. HWND child_content_ = nullptr;
  82. };
  83. #endif // RUNNER_WIN32_WINDOW_H_