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