win32_window.h 3.3 KB

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