SimpleWindow
载入中...
搜索中...
未找到
WndBase.h
1#pragma once
2
3#include "Delegate.h"
4#include "Font.h"
5#include "FrameworkElement.h"
6#include "HitTestResult.h"
7#include "IComparable.h"
8#include "IToString.h"
9#include "Keys.h"
10#include "Point.h"
11#include "ProcMsg.h"
12#include "Property.h"
13#include "Rect.h"
14#include "Size.h"
15#include <string>
16#include <windows.h>
17#include <windowsx.h>
18
19namespace sw
20{
21 class UIElement; // UIElement.h
22
26 class WndBase : public FrameworkElement,
27 public IToString<WndBase>,
28 public IEqualityComparable<WndBase>
29 {
30 // 部分控件可能会改变HWND,设为友元类向Control类暴露_hwnd字段
31 friend class Control;
32
33 // HwndWrapper不使用InitWindow或InitControl初始化句柄,向其暴露底层细节以便实现相关功能
34 friend class HwndWrapper;
35
36 private:
40 const uint32_t _check;
41
45 HWND _hwnd = NULL;
46
50 HFONT _hfont = NULL;
51
55 sw::Font _font;
56
60 sw::Rect _rect{};
61
65 std::wstring _text{};
66
70 bool _focused = false;
71
75 bool _isDestroyed = false;
76
80 bool _isControl = false;
81
85 WNDPROC _originalWndProc = NULL;
86
87 public:
92
97
102
107
112
117
122
127
132
137
142
147
152
157
162
167
172
177
182
187
192
198
203
204 protected:
209
210 public:
214 virtual ~WndBase() = 0;
215
221
225 bool Equals(const WndBase &other) const;
226
230 virtual std::wstring ToString() const;
231
236 virtual WndBase *GetParent() const override;
237
242 virtual int GetChildCount() const override;
243
249 virtual WndBase &GetChildAt(int index) const override;
250
251 protected:
256
261
266
271
276 virtual std::wstring &GetInternalText();
277
282 virtual void SetInternalText(const std::wstring &value);
283
288 virtual bool OnCreate();
289
294 virtual bool OnClose();
295
300 virtual bool OnDestroy();
301
306 virtual bool OnPaint();
307
311 virtual void OnEndPaint();
312
318 virtual bool OnNcPaint(HRGN hRgn);
319
323 virtual void OnEndNcPaint();
324
330 virtual bool OnMove(const Point &newClientPosition);
331
337 virtual bool OnSize(const Size &newClientSize);
338
342 virtual void OnTextChanged();
343
350
357
364 virtual bool OnMouseMove(const Point &mousePosition, MouseKey keyState);
365
370 virtual bool OnMouseLeave();
371
379 virtual bool OnMouseWheel(int wheelDelta, const Point &mousePosition, MouseKey keyState);
380
387 virtual bool OnMouseLeftButtonDown(const Point &mousePosition, MouseKey keyState);
388
395 virtual bool OnMouseLeftButtonUp(const Point &mousePosition, MouseKey keyState);
396
403 virtual bool OnMouseLeftButtonDoubleClick(const Point &mousePosition, MouseKey keyState);
404
411 virtual bool OnMouseRightButtonDown(const Point &mousePosition, MouseKey keyState);
412
419 virtual bool OnMouseRightButtonUp(const Point &mousePosition, MouseKey keyState);
420
427 virtual bool OnMouseRightButtonDoubleClick(const Point &mousePosition, MouseKey keyState);
428
435 virtual bool OnMouseMiddleButtonDown(const Point &mousePosition, MouseKey keyState);
436
443 virtual bool OnMouseMiddleButtonUp(const Point &mousePosition, MouseKey keyState);
444
451 virtual bool OnMouseMiddleButtonDoubleClick(const Point &mousePosition, MouseKey keyState);
452
459 virtual bool OnChar(wchar_t ch, const KeyFlags &flags);
460
467 virtual bool OnDeadChar(wchar_t ch, const KeyFlags &flags);
468
475 virtual bool OnKeyDown(VirtualKey key, const KeyFlags &flags);
476
483 virtual bool OnKeyUp(VirtualKey key, const KeyFlags &flags);
484
491 virtual bool OnSysChar(wchar_t ch, const KeyFlags &flags);
492
499 virtual bool OnSysDeadChar(wchar_t ch, const KeyFlags &flags);
500
507 virtual bool OnSysKeyDown(VirtualKey key, const KeyFlags &flags);
508
515 virtual bool OnSysKeyUp(VirtualKey key, const KeyFlags &flags);
516
520 virtual void VisibleChanged(bool newVisible);
521
526 virtual bool SetParent(WndBase *parent);
527
533
538 virtual void OnCommand(int code);
539
546 virtual void OnControlCommand(WndBase *pControl, int code, int id);
547
552 virtual void OnMenuCommand(int id);
553
558 virtual void OnAcceleratorCommand(int id);
559
564 virtual void HandleInitialized(HWND hwnd);
565
570 virtual void FontChanged(HFONT hfont);
571
580 virtual bool OnSetCursor(HWND hwnd, HitTestResult hitTest, int message, bool &result);
581
588 virtual bool OnContextMenu(bool isKeyboardMsg, const Point &mousePosition);
589
598 virtual bool OnMenuSelect(HMENU hMenu, int id, int flags);
599
607
615
622 virtual bool OnVerticalScroll(int event, int pos);
623
630 virtual bool OnHorizontalScroll(int event, int pos);
631
637 virtual bool OnEnabledChanged(bool newValue);
638
647
654 virtual bool OnColor(HDC hdc, HBRUSH &hRetBrush);
655
662
670
677 virtual bool OnDrawItem(int id, DRAWITEMSTRUCT *pDrawItem);
678
685
693
700
706 virtual bool OnDropFiles(HDROP hDrop);
707
708 public:
713
718
722 void Show(int nCmdShow);
723
727 void Close();
728
732 void Update();
733
738
743
749 void Redraw(bool erase = false, bool updateWindow = false);
750
754 bool IsVisible() const;
755
760
765
770 bool GetStyle(DWORD mask) const;
771
777 void SetStyle(DWORD mask, bool value);
778
783
788
794
800 void SetExtendedStyle(DWORD mask, bool value);
801
808
815
819 LRESULT SendMessageA(UINT uMsg, WPARAM wParam, LPARAM lParam);
820
824 LRESULT SendMessageW(UINT uMsg, WPARAM wParam, LPARAM lParam);
825
829 BOOL PostMessageA(UINT uMsg, WPARAM wParam, LPARAM lParam);
830
834 BOOL PostMessageW(UINT uMsg, WPARAM wParam, LPARAM lParam);
835
841
846 void Invoke(const Action<> &action);
847
852 void InvokeAsync(const Action<> &action);
853
858
862 bool CheckAccess() const;
863
867 bool CheckAccess(const WndBase &other) const;
868
869 public:
875 static WndBase *GetWndBase(HWND hwnd) noexcept;
876
880 static bool IsPtrValid(const WndBase *ptr) noexcept;
881
882 private:
886 static LRESULT CALLBACK _WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
887
891 static WndBase *_GetControlInitContainer();
892
896 static int _NextControlId();
897
903 static void _SetWndBase(HWND hwnd, WndBase &wnd);
904 };
905}
控件
Definition Control.h:12
字体类
Definition Font.h:137
框架元素类,提供数据上下文和绑定功能
Definition FrameworkElement.h:33
将Win32 window包装为SimpleWindow对象
Definition HwndWrapper.h:12
相等性比较接口
Definition IComparable.h:14
为支持ToString方法的类提供统一接口
Definition IToString.h:13
值转换器接口
Definition IValueConverter.h:14
表示界面中的元素
Definition UIElement.h:69
表示一个Windows窗口,是所有窗口和控件的基类
Definition WndBase.h:29
const ReadOnlyProperty< sw::Rect > ClientRect
用户区尺寸
Definition WndBase.h:141
virtual bool OnPaint()
接收到WM_PAINT时调用该函数
void SetExtendedStyle(DWORD mask, bool value)
打开或关闭指定的扩展样式
void SetExtendedStyle(DWORD style)
设置扩展窗口样式
bool CheckAccess(const WndBase &other) const
判断当前对象所属线程是否与另一个WndBase对象所属线程相同
virtual bool OnMouseWheel(int wheelDelta, const Point &mousePosition, MouseKey keyState)
接收到WM_MOUSEWHEEL时调用该函数
virtual bool OnClose()
接收到WM_CLOSE时调用该函数
virtual bool OnMenuSelect(HMENU hMenu, int id, int flags)
接收到WM_MENUSELECT后调用该函数
bool Equals(const WndBase &other) const
判断当前对象与另一个WndBase是否相等
virtual bool OnMeasureItemSelf(MEASUREITEMSTRUCT *pMeasure)
父窗口接收到WM_MEASUREITEM后且父窗口OnMeasureItem函数返回false时调用发出通知控件的该函数
virtual void OnNcHitTest(const Point &testPoint, HitTestResult &result)
接收到WM_NCHITTEST后调用该函数
virtual bool OnMouseRightButtonDown(const Point &mousePosition, MouseKey keyState)
接收到WM_RBUTTONDOWN时调用该函数
virtual WndBase * GetParent() const override
获取父元素
virtual bool OnSysKeyDown(VirtualKey key, const KeyFlags &flags)
接收到WM_SYSKEYDOWN时调用该函数
static bool IsPtrValid(const WndBase *ptr) noexcept
检查指针是否指向有效的WndBase对象
virtual void SetInternalText(const std::wstring &value)
修改窗口文本,该函数默认实现为调用SetWindowTextW
virtual LRESULT WndProc(ProcMsg &refMsg)
对WndProc的封装
Point PointToScreen(const Point &point) const
获取用户区点在屏幕上点的位置
bool GetExtendedStyle(DWORD mask)
判断窗口是否设有指定扩展样式
HFONT GetFontHandle()
获取字体句柄
LRESULT SendMessageA(UINT uMsg, WPARAM wParam, LPARAM lParam)
发送消息(ASCII)
void SetStyle(DWORD mask, bool value)
打开或关闭指定的样式
bool CheckAccess() const
判断当前线程是否为窗口所属线程
const ReadOnlyProperty< std::wstring > ClassName
窗口类名
Definition WndBase.h:191
virtual bool OnMouseRightButtonUp(const Point &mousePosition, MouseKey keyState)
接收到WM_RBUTTONUP时调用该函数
const ReadOnlyProperty< double > ClientHeight
用户区高度
Definition WndBase.h:151
void Close()
发送关闭消息
const Property< bool > AcceptFiles
是否接受拖放文件
Definition WndBase.h:181
virtual bool OnMouseLeftButtonDown(const Point &mousePosition, MouseKey keyState)
接收到WM_LBUTTONDOWN时调用该函数
const Property< double > Width
宽度
Definition WndBase.h:131
Point PointFromScreen(const Point &screenPoint) const
获取屏幕上点在当前用户区点的位置
virtual bool OnMouseLeftButtonUp(const Point &mousePosition, MouseKey keyState)
接收到WM_LBUTTONUP时调用该函数
void InitWindow(LPCWSTR lpWindowName, DWORD dwStyle, DWORD dwExStyle)
初始化为窗口,该函数会调用CreateWindowExW
virtual int GetChildCount() const override
获取子元素数量
BOOL PostMessageA(UINT uMsg, WPARAM wParam, LPARAM lParam)
发送消息(ASCII)并立即返回
virtual bool OnSetFocus(HWND hPrevFocus)
接收到WM_SETFOCUS时调用该函数
virtual void HandleInitialized(HWND hwnd)
窗口句柄初始化完成
const ReadOnlyProperty< bool > IsControl
当前对象是否是控件
Definition WndBase.h:186
virtual void ParentChanged(WndBase *newParent)
父窗口改变时调用此函数
virtual void OnEndNcPaint()
在OnNcPaint函数完成之后调用该函数
virtual bool OnMeasureItem(int id, MEASUREITEMSTRUCT *pMeasure)
接收到WM_MEASUREITEM时调用该函数
const Property< double > Left
左边
Definition WndBase.h:121
const Property< sw::FontWeight > FontWeight
字体粗细
Definition WndBase.h:111
virtual bool OnSysKeyUp(VirtualKey key, const KeyFlags &flags)
接收到WM_SYSKEYUP时调用该函数
const Property< double > Height
高度
Definition WndBase.h:136
const Property< sw::Font > Font
字体
Definition WndBase.h:96
virtual bool OnNcPaint(HRGN hRgn)
接收到WM_NCPAINT时调用该函数
const ReadOnlyProperty< bool > IsDestroyed
是否已销毁,当该值为true时不应该继续使用当前对象
Definition WndBase.h:176
virtual bool OnMouseMiddleButtonDown(const Point &mousePosition, MouseKey keyState)
接收到WM_MBUTTONDOWN时调用该函数
virtual bool OnKeyUp(VirtualKey key, const KeyFlags &flags)
接收到WM_KEYUP时调用该函数
const Property< bool > IsGroupStart
窗口是否为一组控件中的第一个控件
Definition WndBase.h:197
virtual bool SetParent(WndBase *parent)
设置父窗口
virtual bool OnNotified(NMHDR *pNMHDR, LRESULT &result)
父窗口接收到WM_NOTIFY后且父窗口OnNotify函数返回false时调用发出通知控件的该函数
virtual void OnCommand(int code)
当父窗口接收到控件的WM_COMMAND时调用该函数
virtual bool OnChar(wchar_t ch, const KeyFlags &flags)
接收到WM_CHAR时调用该函数
virtual bool OnSysChar(wchar_t ch, const KeyFlags &flags)
接收到WM_SYSCHAR时调用该函数
const ReadOnlyProperty< bool > IsMouseCaptured
鼠标是否被当前窗口捕获
Definition WndBase.h:202
virtual std::wstring & GetInternalText()
获取内部记录窗口文本的字符串引用
virtual void OnControlCommand(WndBase *pControl, int code, int id)
当WM_COMMAND接收到控件命令时调用该函数
virtual bool OnMouseRightButtonDoubleClick(const Point &mousePosition, MouseKey keyState)
接收到WM_RBUTTONDBLCLK时调用该函数
void Redraw(bool erase=false, bool updateWindow=false)
重画
virtual ~WndBase()=0
析构函数,这里用纯虚函数使该类成为抽象类
const Property< double > Top
顶边
Definition WndBase.h:126
virtual bool OnKillFocus(HWND hNextFocus)
接收到WM_KILLFOCUS时调用该函数
virtual bool OnDrawItem(int id, DRAWITEMSTRUCT *pDrawItem)
接收到WM_DRAWITEM时调用该函数
const Property< bool > Focused
窗口是否拥有焦点
Definition WndBase.h:171
virtual bool OnVerticalScroll(int event, int pos)
接收到WM_VSCROLL时调用目标控件的该函数
const Property< std::wstring > FontName
字体名称
Definition WndBase.h:101
virtual bool OnEnabledChanged(bool newValue)
接收到WM_ENABLE时调用该函数
const Property< sw::Rect > Rect
位置和尺寸
Definition WndBase.h:116
virtual bool OnDrawItemSelf(DRAWITEMSTRUCT *pDrawItem)
父窗口接收到WM_DRAWITEM后且父窗口OnDrawItem函数返回false时调用发出通知控件的该函数
virtual bool OnCreate()
接收到WM_CREATE时调用该函数
const Property< bool > Visible
窗口或控件是否可见
Definition WndBase.h:161
virtual bool OnMouseMove(const Point &mousePosition, MouseKey keyState)
接收到WM_MOUSEMOVE时调用该函数
HitTestResult NcHitTest(const Point &testPoint)
测试指定点在窗口的哪一部分
static WndBase * GetWndBase(HWND hwnd) noexcept
通过窗口句柄获取WndBase
void UpdateInternalRect()
同步窗口位置和尺寸到内部记录的Rect
DWORD GetThreadId() const
获取当前窗口所属线程的线程id
virtual void OnAcceleratorCommand(int id)
当WM_COMMAND接收到快捷键命令时调用该函数
bool IsVisible() const
判断当前对象在界面中是否可视,与Visible属性不同的是该函数返回值会受父窗口的影响
const ReadOnlyProperty< double > ClientWidth
用户区宽度
Definition WndBase.h:146
virtual bool OnMove(const Point &newClientPosition)
接收到WM_MOVE时调用该函数
virtual bool OnDestroy()
接收到WM_DESTROY时调用该函数
const Property< std::wstring > Text
窗口标题或控件文本
Definition WndBase.h:166
virtual bool OnDropFiles(HDROP hDrop)
接收到WM_DROPFILES时调用该函数
virtual bool OnCtlColor(WndBase *pControl, HDC hdc, HBRUSH &hRetBrush)
接收到WM_CTLCOLORxxx时调用该函数
LRESULT DefaultWndProc(const ProcMsg &msg)
调用默认的WndProc,对于窗口则调用DefWindowProcW,控件则调用_controlOldWndProc
virtual bool OnColor(HDC hdc, HBRUSH &hRetBrush)
父窗口接收到WM_CTLCOLORxxx时调用对应控件的该函数
WndBase()
初始化WndBase
virtual bool OnMouseLeave()
接收到WM_MOUSELEAVE时调用该函数
void InvokeAsync(const Action<> &action)
在窗口线程上执行指定委托,并立即返回
virtual bool OnSetCursor(HWND hwnd, HitTestResult hitTest, int message, bool &result)
接收到WM_SETCURSOR消息时调用该函数
void Update()
该函数调用UpdateWindow
bool GetStyle(DWORD mask) const
判断窗口是否设有指定样式
const ReadOnlyProperty< HWND > Handle
窗口句柄
Definition WndBase.h:91
virtual UIElement * ToUIElement()
尝试将对象转换成UIElement
virtual void FontChanged(HFONT hfont)
字体改变时调用该函数
DWORD GetExtendedStyle() const
获取扩展窗口样式
const Property< double > FontSize
字体大小
Definition WndBase.h:106
virtual bool OnMouseLeftButtonDoubleClick(const Point &mousePosition, MouseKey keyState)
接收到WM_LBUTTONDBLCLK时调用该函数
virtual bool OnContextMenu(bool isKeyboardMsg, const Point &mousePosition)
接收到WM_CONTEXTMENU后调用目标控件的该函数
virtual WndBase & GetChildAt(int index) const override
获取指定索引处的子元素
virtual bool OnDeadChar(wchar_t ch, const KeyFlags &flags)
接收到WM_DEADCHAR时调用该函数
LRESULT SendMessageW(UINT uMsg, WPARAM wParam, LPARAM lParam)
发送消息(UNICODE)
const Property< bool > Enabled
窗口或控件是否可用
Definition WndBase.h:156
virtual void OnEndPaint()
在OnPaint函数完成之后调用该函数
virtual void OnTextChanged()
Text属性更改时调用此函数
void UpdateInternalText()
同步窗口文本到内部记录的字符串
void Show(int nCmdShow)
该函数调用ShowWindow
void Invoke(const Action<> &action)
在窗口线程上执行指定委托
virtual bool OnMouseMiddleButtonDoubleClick(const Point &mousePosition, MouseKey keyState)
接收到WM_MBUTTONDBLCLK时调用该函数
BOOL PostMessageW(UINT uMsg, WPARAM wParam, LPARAM lParam)
发送消息(UNICODE)并立即返回
virtual bool OnSize(const Size &newClientSize)
接收到WM_SIZE时调用该函数
virtual bool OnSysDeadChar(wchar_t ch, const KeyFlags &flags)
接收到WM_SYSDEADCHAR时调用该函数
virtual bool OnNotify(NMHDR *pNMHDR, LRESULT &result)
接收到WM_NOTIFY后调用该函数
void InitControl(LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, DWORD dwExStyle, LPVOID lpParam=NULL)
初始化为控件,该函数会调用CreateWindowExW
virtual bool OnMouseMiddleButtonUp(const Point &mousePosition, MouseKey keyState)
接收到WM_MBUTTONUP时调用该函数
virtual void VisibleChanged(bool newVisible)
Visible属性改变时调用此函数
virtual std::wstring ToString() const
获取当前对象的描述字符串
virtual bool OnKeyDown(VirtualKey key, const KeyFlags &flags)
接收到WM_KEYDOWN时调用该函数
virtual bool OnHorizontalScroll(int event, int pos)
接收到WM_HSCROLL时调用目标控件的该函数
void UpdateFont()
更新字体
DWORD GetStyle() const
获取窗口样式
void SetStyle(DWORD style)
设置窗口样式
virtual bool OnEraseBackground(HDC hdc, LRESULT &result)
接收到WM_ERASEBKGND时调用该函数
virtual void OnMenuCommand(int id)
当WM_COMMAND接收到菜单命令时调用该函数
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
MouseKey
鼠标事件时用于判断按键状态
Definition Keys.h:291
VirtualKey
虚拟按键
Definition Keys.h:65
HitTestResult
NcHitTest(WM_NCHITTEST)的返回值
Definition HitTestResult.h:10
https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input#keystroke-message-flags
Definition Keys.h:12
表示相对于左上角的点坐标
Definition Point.h:15
对Windows窗口消息的封装
Definition ProcMsg.h:10
表示一个矩形区域
Definition Rect.h:17
尺寸
Definition Size.h:15