win32_window.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef WIN32_WINDOW_H_
  2. #define WIN32_WINDOW_H_
  3. #include <Windows.h>
  4. #include <Windowsx.h>
  5. #include <functional>
  6. #include <memory>
  7. #include <string>
  8. // A class abstraction for a high DPI-aware Win32 Window. Intended to be
  9. // inherited from by classes that wish to specialize with custom
  10. // rendering and input handling
  11. class Win32Window {
  12. public:
  13. struct Point {
  14. unsigned int x;
  15. unsigned int y;
  16. Point(unsigned int x, unsigned int y) : x(x), y(y) {}
  17. };
  18. struct Size {
  19. unsigned int width;
  20. unsigned int height;
  21. Size(unsigned int width, unsigned int height)
  22. : width(width), height(height) {}
  23. };
  24. Win32Window();
  25. virtual ~Win32Window();
  26. // Creates and shows a win32 window with |title| and position and size using
  27. // |origin| and |size|. New windows are created on the default monitor. Window
  28. // sizes are specified to the OS in physical pixels, hence to ensure a
  29. // consistent size to will treat the width height passed in to this function
  30. // as logical pixels and scale to appropriate for the default monitor. Returns
  31. // true if the window was created successfully.
  32. bool CreateAndShow(const std::wstring& title,
  33. const Point& origin,
  34. const Size& size);
  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. protected:
  45. // Processes and route salient window messages for mouse handling,
  46. // size change and DPI. Delegates handling of these to member overloads that
  47. // inheriting classes can handle.
  48. virtual LRESULT MessageHandler(HWND window,
  49. UINT const message,
  50. WPARAM const wparam,
  51. LPARAM const lparam) noexcept;
  52. // Called when CreateAndShow is called, allowing subclass window-related
  53. // setup.
  54. virtual void OnCreate();
  55. // Called when Destroy is called.
  56. virtual void OnDestroy();
  57. private:
  58. friend class WindowClassRegistrar;
  59. // OS callback called by message pump. Handles the WM_NCCREATE message which
  60. // is passed when the non-client area is being created and enables automatic
  61. // non-client DPI scaling so that the non-client area automatically
  62. // responsponds to changes in DPI. All other messages are handled by
  63. // MessageHandler.
  64. static LRESULT CALLBACK WndProc(HWND const window,
  65. UINT const message,
  66. WPARAM const wparam,
  67. LPARAM const lparam) noexcept;
  68. // Retrieves a class instance pointer for |window|
  69. static Win32Window* GetThisFromHandle(HWND const window) noexcept;
  70. bool quit_on_close_ = false;
  71. // window handle for top level window.
  72. HWND window_handle_ = nullptr;
  73. // window handle for hosted content.
  74. HWND child_content_ = nullptr;
  75. };
  76. #endif // WIN32_WINDOW_H_