win32_window.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 in to 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. // Release OS resources associated with window.
  35. void Destroy();
  36. // Inserts |content| into the window tree.
  37. void SetChildContent(HWND content);
  38. // Returns the backing Window handle to enable clients to set icon and other
  39. // window properties. Returns nullptr if the window has been destroyed.
  40. HWND GetHandle();
  41. // If true, closing this window will quit the application.
  42. void SetQuitOnClose(bool quit_on_close);
  43. // Return a RECT representing the bounds of the current client area.
  44. RECT GetClientArea();
  45. protected:
  46. // Processes and route salient window messages for mouse handling,
  47. // size change and DPI. Delegates handling of these to member overloads that
  48. // inheriting classes can handle.
  49. virtual LRESULT MessageHandler(HWND window,
  50. UINT const message,
  51. WPARAM const wparam,
  52. LPARAM const lparam) noexcept;
  53. // Called when CreateAndShow is called, allowing subclass window-related
  54. // setup. Subclasses should return false if setup fails.
  55. virtual bool OnCreate();
  56. // Called when Destroy is called.
  57. virtual void OnDestroy();
  58. private:
  59. friend class WindowClassRegistrar;
  60. // OS callback called by message pump. Handles the WM_NCCREATE message which
  61. // is passed when the non-client area is being created and enables automatic
  62. // non-client DPI scaling so that the non-client area automatically
  63. // responsponds to changes in DPI. All other messages are handled by
  64. // MessageHandler.
  65. static LRESULT CALLBACK WndProc(HWND const window,
  66. UINT const message,
  67. WPARAM const wparam,
  68. LPARAM const lparam) noexcept;
  69. // Retrieves a class instance pointer for |window|
  70. static Win32Window* GetThisFromHandle(HWND const window) noexcept;
  71. bool quit_on_close_ = false;
  72. // window handle for top level window.
  73. HWND window_handle_ = nullptr;
  74. // window handle for hosted content.
  75. HWND child_content_ = nullptr;
  76. };
  77. #endif // RUNNER_WIN32_WINDOW_H_