Advertisement
FlyFar

win32_window.h

Jul 25th, 2023
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | Cybersecurity | 0 0
  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 and shows a win32 window with |title| and position and size 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 to will treat the width height passed in to this function
  35.   // as logical pixels and scale to appropriate for the default monitor. Returns
  36.   // true if the window was created successfully.
  37.   bool CreateAndShow(const std::wstring& title,
  38.                      const Point& origin,
  39.                      const Size& size);
  40.  
  41.   // Release OS resources associated with window.
  42.   void Destroy();
  43.  
  44.   // Inserts |content| into the window tree.
  45.   void SetChildContent(HWND content);
  46.  
  47.   // Returns the backing Window handle to enable clients to set icon and other
  48.   // window properties. Returns nullptr if the window has been destroyed.
  49.   HWND GetHandle();
  50.  
  51.   // If true, closing this window will quit the application.
  52.   void SetQuitOnClose(bool quit_on_close);
  53.  
  54.   // Return a RECT representing the bounds of the current client area.
  55.   RECT GetClientArea();
  56.  
  57.  protected:
  58.   // Processes and route salient window messages for mouse handling,
  59.   // size change and DPI. Delegates handling of these to member overloads that
  60.   // inheriting classes can handle.
  61.   virtual LRESULT MessageHandler(HWND window,
  62.                                  UINT const message,
  63.                                  WPARAM const wparam,
  64.                                  LPARAM const lparam) noexcept;
  65.  
  66.   // Called when CreateAndShow is called, allowing subclass window-related
  67.   // setup. Subclasses should return false if setup fails.
  68.   virtual bool OnCreate();
  69.  
  70.   // Called when Destroy is called.
  71.   virtual void OnDestroy();
  72.  
  73.  private:
  74.   friend class WindowClassRegistrar;
  75.  
  76.   // OS callback called by message pump. Handles the WM_NCCREATE message which
  77.   // is passed when the non-client area is being created and enables automatic
  78.   // non-client DPI scaling so that the non-client area automatically
  79.   // responsponds to changes in DPI. All other messages are handled by
  80.   // MessageHandler.
  81.   static LRESULT CALLBACK WndProc(HWND const window,
  82.                                   UINT const message,
  83.                                   WPARAM const wparam,
  84.                                   LPARAM const lparam) noexcept;
  85.  
  86.   // Retrieves a class instance pointer for |window|
  87.   static Win32Window* GetThisFromHandle(HWND const window) noexcept;
  88.  
  89.   bool quit_on_close_ = false;
  90.  
  91.   // window handle for top level window.
  92.   HWND window_handle_ = nullptr;
  93.  
  94.   // window handle for hosted content.
  95.   HWND child_content_ = nullptr;
  96. };
  97.  
  98. #endif  // RUNNER_WIN32_WINDOW_H_
Tags: Header
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement