SimpleWindow
载入中...
搜索中...
未找到
UIElement.h
1#pragma once
2
3#include "Alignment.h"
4#include "Binding.h"
5#include "Color.h"
6#include "ContextMenu.h"
7#include "EnumBit.h"
8#include "EventHandlerWrapper.h"
9#include "ILayout.h"
10#include "ITag.h"
11#include "RoutedEvent.h"
12#include "RoutedEventArgs.h"
13#include "Thickness.h"
14#include "WndBase.h"
15#include "WndMsg.h"
16#include <cstdint>
17#include <map>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22namespace sw
23{
24 // 前向声明
25 class UIElement;
26 class DataBinding;
27
31 using DataContextChangedEventHandler = Delegate<void(UIElement &sender, DynamicObject *oldval)>;
32
36 enum class LayoutUpdateCondition : uint32_t {
40 SizeChanged = 1 << 0,
41
45 PositionChanged = 1 << 1,
46
50 ChildAdded = 1 << 2,
51
55 ChildRemoved = 1 << 3,
56
60 TextChanged = 1 << 4,
61
65 FontChanged = 1 << 5,
66
71 MeasureInvalidated = 1 << 29,
72
78 Supressed = 1 << 30,
79 };
80
84 _SW_ENUM_ENABLE_BIT_OPERATIONS(LayoutUpdateCondition);
85
89 class UIElement : public WndBase, public ILayout, public ITag
90 {
91 private:
95 sw::LayoutUpdateCondition _layoutUpdateCondition =
96 sw::LayoutUpdateCondition::SizeChanged |
97 sw::LayoutUpdateCondition::ChildAdded |
98 sw::LayoutUpdateCondition::ChildRemoved;
99
103 bool _collapseWhenHide = true;
104
108 Size _desireSize{};
109
113 Size _origionalSize{};
114
118 Size _minSize{-1, -1};
119
123 Size _maxSize{-1, -1};
124
128 Thickness _margin{};
129
133 HorizontalAlignment _horizontalAlignment = HorizontalAlignment::Center;
134
138 VerticalAlignment _verticalAlignment = VerticalAlignment::Center;
139
143 UIElement *_parent = nullptr;
144
148 std::vector<UIElement *> _children{};
149
153 std::vector<UIElement *> _layoutVisibleChildren{};
154
158 std::unordered_map<RoutedEventType, RoutedEventHandler> _eventMap{};
159
163 uint64_t _tag = 0;
164
168 uint64_t _layoutTag = 0;
169
173 sw::ContextMenu *_contextMenu = nullptr;
174
178 double _arrangeOffsetX = 0;
179
183 double _arrangeOffsetY = 0;
184
188 double _childRightmost = 0;
189
193 double _childBottommost = 0;
194
198 bool _float = false;
199
203 bool _tabStop = false;
204
208 Color _backColor{KnownColor::White};
209
213 Color _textColor{KnownColor::Black};
214
218 bool _transparent = false;
219
223 bool _inheritTextColor = false;
224
228 bool _useDefaultCursor = true;
229
233 HCURSOR _hCursor = NULL;
234
238 Size _lastMeasureAvailableSize{};
239
243 HDWP _hdwpChildren = NULL;
244
248 HBRUSH _hCtlColorBrush = NULL;
249
253 COLORREF _lastTextColor = 0;
254
258 COLORREF _lastBackColor = 0;
259
263 bool _isHitTestVisible = true;
264
268 bool _focusedViaTab = false;
269
273 std::unordered_map<FieldId, std::unique_ptr<BindingBase>> _bindings{};
274
278 DynamicObject *_dataContext = nullptr;
279
280 public:
285
290
295
300
305
310
315
320
325
330
335
340
345
350
356
361
367
372
377
382
387
392
398
403
408
413
419
420 public:
425
429 virtual ~UIElement() = 0;
430
437 [[deprecated("Use AddHandler instead to avoid overwriting existing event handlers.")]]
438 void RegisterRoutedEvent(RoutedEventType eventType, const RoutedEventHandler &handler);
439
445 void AddHandler(RoutedEventType eventType, const RoutedEventHandler &handler);
446
453 bool RemoveHandler(RoutedEventType eventType, const RoutedEventHandler &handler);
454
459 void UnregisterRoutedEvent(RoutedEventType eventType);
460
465 bool IsRoutedEventRegistered(RoutedEventType eventType);
466
470 UIElement &operator[](int index) const;
471
475 UIElement &GetChildAt(int index) const;
476
482 bool AddChild(UIElement *element);
483
489 bool AddChild(UIElement &element);
490
496 bool AddChild(UIElement *element, uint64_t layoutTag);
497
503 bool AddChild(UIElement &element, uint64_t layoutTag);
504
510 bool RemoveChildAt(int index);
511
516 bool RemoveChild(UIElement *element);
517
522 bool RemoveChild(UIElement &element);
523
528
534 int IndexOf(UIElement *element);
535
541 int IndexOf(UIElement &element);
542
551 const Point &point,
552 sw::HorizontalAlignment horz = sw::HorizontalAlignment::Left,
553 sw::VerticalAlignment vert = sw::VerticalAlignment::Top);
554
558 void MoveToTop();
559
564
568 bool IsRootElement() const;
569
574
579
584
589
594
600
606
611 void SetCursor(HCURSOR hCursor);
612
617 void SetCursor(StandardCursor cursor);
618
623
629 void SetAlignment(sw::HorizontalAlignment horz, sw::VerticalAlignment vert);
630
635 void Resize(const Size &size);
636
640 bool IsLayoutUpdateConditionSet(sw::LayoutUpdateCondition condition);
641
646
653
661 bool AddBinding(BindingBase *binding);
662
670 bool AddBinding(Binding *binding);
671
678 bool AddBinding(DataBinding *binding);
679
684 bool RemoveBinding(FieldId propertyId);
685
689 virtual uint64_t GetTag() const override;
690
694 virtual void SetTag(uint64_t tag) override;
695
699 virtual uint64_t GetLayoutTag() const override;
700
705 virtual int GetChildLayoutCount() const override;
706
711 virtual ILayout &GetChildLayoutAt(int index) override;
712
716 virtual Size GetDesireSize() const override;
717
722 virtual void Measure(const Size &availableSize) override;
723
728 virtual void Arrange(const sw::Rect &finalPosition) override;
729
734 virtual UIElement *ToUIElement() override;
735
736 protected:
741 void RaiseRoutedEvent(RoutedEventType eventType);
742
748
754
760
766 double GetChildRightmost(bool update);
767
773 double GetChildBottommost(bool update);
774
778 void UpdateChildrenZOrder(bool invalidateMeasure = true);
779
783 void UpdateSiblingsZOrder(bool invalidateMeasure = true);
784
789 void ClampDesireSize(sw::Size &size) const;
790
795 void ClampDesireSize(sw::Rect &rect) const;
796
802 bool QueryAllChildren(const Func<UIElement *, bool> &queryFunc);
803
810 virtual Size MeasureOverride(const Size &availableSize);
811
816 virtual void ArrangeOverride(const Size &finalSize);
817
823 virtual void SetBackColor(Color color, bool redraw);
824
830 virtual void SetTextColor(Color color, bool redraw);
831
837 virtual bool RequestBringIntoView(const sw::Rect &screenRect);
838
843 virtual void OnAddedChild(UIElement &element);
844
849 virtual void OnRemovedChild(UIElement &element);
850
855 virtual void OnTabMove(bool forward);
856
860 virtual void OnTabStop();
861
865 virtual void OnMinMaxSizeChanged();
866
872 virtual void OnRoutedEvent(RoutedEventArgs &eventArgs, const RoutedEventHandler &handler);
873
878 virtual bool SetParent(WndBase *parent) override;
879
884 virtual void ParentChanged(WndBase *newParent) override;
885
890 virtual bool OnClose() override;
891
897 virtual bool OnMove(const Point &newClientPosition) override;
898
904 virtual bool OnSize(const Size &newClientSize) override;
905
909 virtual void OnTextChanged() override;
910
915 virtual void FontChanged(HFONT hfont) override;
916
920 virtual void VisibleChanged(bool newVisible) override;
921
927 virtual bool OnSetFocus(HWND hPrevFocus) override;
928
934 virtual bool OnKillFocus(HWND hNextFocus) override;
935
942 virtual bool OnChar(wchar_t ch, const KeyFlags &flags) override;
943
950 virtual bool OnKeyDown(VirtualKey key, const KeyFlags &flags) override;
951
958 virtual bool OnKeyUp(VirtualKey key, const KeyFlags &flags) override;
959
966 virtual bool OnMouseMove(const Point &mousePosition, MouseKey keyState) override;
967
972 virtual bool OnMouseLeave() override;
973
981 virtual bool OnMouseWheel(int wheelDelta, const Point &mousePosition, MouseKey keyState) override;
982
989 virtual bool OnMouseLeftButtonDown(const Point &mousePosition, MouseKey keyState) override;
990
997 virtual bool OnMouseLeftButtonUp(const Point &mousePosition, MouseKey keyState) override;
998
1005 virtual bool OnMouseRightButtonDown(const Point &mousePosition, MouseKey keyState) override;
1006
1013 virtual bool OnMouseRightButtonUp(const Point &mousePosition, MouseKey keyState) override;
1014
1021 virtual bool OnMouseMiddleButtonDown(const Point &mousePosition, MouseKey keyState) override;
1022
1029 virtual bool OnMouseMiddleButtonUp(const Point &mousePosition, MouseKey keyState) override;
1030
1037 virtual bool OnContextMenu(bool isKeyboardMsg, const Point &mousePosition) override;
1038
1043 virtual void OnMenuCommand(int id) override;
1044
1051 virtual bool OnColor(HDC hdc, HBRUSH &hRetBrush) override;
1052
1061 virtual bool OnSetCursor(HWND hwnd, HitTestResult hitTest, int message, bool &result) override;
1062
1068 virtual bool OnDropFiles(HDROP hDrop) override;
1069
1075 virtual void OnNcHitTest(const Point &testPoint, HitTestResult &result) override;
1076
1077 private:
1083 bool _SetHorzAlignment(sw::HorizontalAlignment value);
1084
1090 bool _SetVertAlignment(sw::VerticalAlignment value);
1091
1095 void _SetMeasureInvalidated();
1096
1100 void _UpdateLayoutVisibleChildren();
1101
1105 void _RemoveFromLayoutVisibleChildren(UIElement *element);
1106
1111 DynamicObject *_GetCurrentDataContext();
1112
1116 void _OnCurrentDataContextChanged(DynamicObject *oldval);
1117
1121 static UIElement *_GetNextElement(UIElement *element, bool searchChildren = true);
1122
1126 static UIElement *_GetDeepestLastElement(UIElement *element);
1127
1131 static UIElement *_GetPreviousElement(UIElement *element);
1132
1136 static void _GetAllChildren(UIElement *element, std::vector<UIElement *> &children);
1137
1138 private:
1142 template <typename T, typename = void>
1143 struct _CanAddChild : std::false_type {
1144 };
1145
1146 // 已移动到类外,防止clang报错不完整的类型
1147 // /**
1148 // * @brief _CanAddChild模板偏特化,当AddChild有对应类型的重载时该偏特化生效
1149 // */
1150 // template <typename T>
1151 // struct _CanAddChild<
1152 // T, decltype(void(std::declval<UIElement>().AddChild(std::declval<T>())))> : std::true_type {
1153 // };
1154
1158 template <typename First, typename... Rest>
1159 struct _CanAddChildren
1160 : std::integral_constant<bool, _CanAddChild<First>::value && _CanAddChildren<Rest...>::value> {
1161 };
1162
1166 template <typename T>
1167 struct _CanAddChildren<T> : _CanAddChild<T> {
1168 };
1169
1170 public:
1177 template <typename First, typename... Rest>
1178 auto AddChildren(First &&first, Rest &&...rest)
1179 -> typename std::enable_if<_CanAddChildren<First, Rest...>::value, int>::type
1180 {
1181 int count = 0;
1182 if (this->AddChild(std::forward<First>(first)))
1183 count = 1 + this->AddChildren(std::forward<Rest>(rest)...);
1184 return count;
1185 }
1186
1193 template <typename T>
1194 auto AddChildren(T &&child)
1195 -> typename std::enable_if<_CanAddChild<T>::value, int>::type
1196 {
1197 return this->AddChild(std::forward<T>(child)) ? 1 : 0;
1198 }
1199
1208 template <typename T>
1209 [[deprecated("Use AddHandler instead to avoid overwriting existing event handlers.")]]
1210 void RegisterRoutedEvent(RoutedEventType eventType, T &obj, void (T::*handler)(UIElement &, RoutedEventArgs &))
1211 {
1212 if (handler == nullptr) {
1213 this->UnregisterRoutedEvent(eventType);
1214 } else {
1215 this->RegisterRoutedEvent(eventType, RoutedEventHandler(obj, handler));
1216 }
1217 }
1218
1225 template <typename TEventArgs>
1226 [[deprecated("Use AddHandler instead to avoid overwriting existing event handlers.")]]
1228 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value>::type
1229 {
1230 if (!handler) {
1231 this->UnregisterRoutedEvent(TEventArgs::EventType);
1232 } else {
1233 this->RegisterRoutedEvent(TEventArgs::EventType, RoutedEventHandlerWrapper<TEventArgs>(handler));
1234 }
1235 }
1236
1245 template <typename TEventArgs, typename THandleObj>
1246 [[deprecated("Use AddHandler instead to avoid overwriting existing event handlers.")]]
1247 auto RegisterRoutedEvent(THandleObj &obj, void (THandleObj::*handler)(UIElement &, TEventArgs &))
1248 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value>::type
1249 {
1250 if (handler == nullptr) {
1251 this->UnregisterRoutedEvent(TEventArgs::EventType);
1252 } else {
1254 }
1255 }
1256
1264 template <typename T>
1265 void AddHandler(RoutedEventType eventType, T &obj, void (T::*handler)(UIElement &, RoutedEventArgs &))
1266 {
1267 if (handler) this->_eventMap[eventType].Add(obj, handler);
1268 }
1269
1275 template <typename TEventArgs>
1277 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value>::type
1278 {
1279 if (handler) this->AddHandler(TEventArgs::EventType, RoutedEventHandlerWrapper<TEventArgs>(handler));
1280 }
1281
1289 template <typename TEventArgs, typename THandleObj>
1290 auto AddHandler(THandleObj &obj, void (THandleObj::*handler)(UIElement &, TEventArgs &))
1291 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value>::type
1292 {
1293 if (handler) this->AddHandler(TEventArgs::EventType, RoutedEventHandlerWrapper<TEventArgs>(Action<UIElement &, TEventArgs &>(obj, handler)));
1294 }
1295
1302 template <typename TEventArgs>
1303 auto AddHandler(RoutedEventType eventType, const Action<UIElement &, TEventArgs &> &handler)
1304 -> typename std::enable_if<
1305 std::is_base_of<RoutedEventArgs, TEventArgs>::value &&
1306 !std::is_same<TEventArgs, RoutedEventArgs>::value &&
1308 {
1309 if (handler) this->AddHandler(eventType, RoutedEventHandlerWrapper<TEventArgs>(handler));
1310 }
1311
1320 template <typename TEventArgs, typename THandleObj>
1321 auto AddHandler(RoutedEventType eventType, THandleObj &obj, void (THandleObj::*handler)(UIElement &, TEventArgs &))
1322 -> typename std::enable_if<
1323 std::is_base_of<RoutedEventArgs, TEventArgs>::value &&
1324 !std::is_same<TEventArgs, RoutedEventArgs>::value &&
1325 !sw::_IsTypedRoutedEventArgs<TEventArgs>::value>::type
1326 {
1327 if (handler) this->AddHandler(eventType, RoutedEventHandlerWrapper<TEventArgs>(Action<UIElement &, TEventArgs &>(obj, handler)));
1328 }
1329
1338 template <typename T>
1339 bool RemoveHandler(RoutedEventType eventType, T &obj, void (T::*handler)(UIElement &, RoutedEventArgs &))
1340 {
1341 return handler == nullptr ? false : this->_eventMap[eventType].Remove(obj, handler);
1342 }
1343
1350 template <typename TEventArgs>
1352 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value, bool>::type
1353 {
1354 if (handler == nullptr) {
1355 return false;
1356 } else {
1357 return this->RemoveHandler(TEventArgs::EventType, RoutedEventHandlerWrapper<TEventArgs>(handler));
1358 }
1359 }
1360
1369 template <typename TEventArgs, typename THandleObj>
1370 auto RemoveHandler(THandleObj &obj, void (THandleObj::*handler)(UIElement &, TEventArgs &))
1371 -> typename std::enable_if<std::is_base_of<RoutedEventArgs, TEventArgs>::value && sw::_IsTypedRoutedEventArgs<TEventArgs>::value, bool>::type
1372 {
1373 if (handler == nullptr) {
1374 return false;
1375 } else {
1376 return this->RemoveHandler(TEventArgs::EventType, RoutedEventHandlerWrapper<TEventArgs>(Action<UIElement &, TEventArgs &>(obj, handler)));
1377 }
1378 }
1379
1387 template <typename TEventArgs>
1388 auto RemoveHandler(RoutedEventType eventType, const Action<UIElement &, TEventArgs &> &handler)
1389 -> typename std::enable_if<
1390 std::is_base_of<RoutedEventArgs, TEventArgs>::value &&
1391 !std::is_same<TEventArgs, RoutedEventArgs>::value &&
1393 bool>::type
1394 {
1395 if (handler == nullptr) {
1396 return false;
1397 } else {
1398 return this->RemoveHandler(eventType, RoutedEventHandlerWrapper<TEventArgs>(handler));
1399 }
1400 }
1401
1411 template <typename TEventArgs, typename THandleObj>
1412 auto RemoveHandler(RoutedEventType eventType, THandleObj &obj, void (THandleObj::*handler)(UIElement &, TEventArgs &))
1413 -> typename std::enable_if<
1414 std::is_base_of<RoutedEventArgs, TEventArgs>::value &&
1415 !std::is_same<TEventArgs, RoutedEventArgs>::value &&
1416 !sw::_IsTypedRoutedEventArgs<TEventArgs>::value,
1417 bool>::type
1418 {
1419 if (handler == nullptr) {
1420 return false;
1421 } else {
1423 }
1424 }
1425
1430 template <typename T, typename TProperty>
1431 bool RemoveBinding(TProperty T::*prop)
1432 {
1433 return this->RemoveBinding(Reflection::GetFieldId(prop));
1434 }
1435 };
1436
1440 template <typename T>
1441 struct UIElement::_CanAddChild<
1442 T, decltype(void(std::declval<UIElement>().AddChild(std::declval<T>())))> : std::true_type {
1443 };
1444}
数据绑定基类
Definition Binding.h:38
数据绑定类
Definition Binding.h:73
上下文菜单
Definition ContextMenu.h:11
数据绑定,用于UI元素与DataContext之间的属性绑定
Definition DataBinding.h:11
动态对象基类
Definition Reflection.h:26
布局接口
Definition ILayout.h:13
Tag接口
Definition ITag.h:11
属性
Definition Property.h:1743
只读属性
Definition Property.h:1828
static FieldId GetFieldId(TField T::*field) noexcept
获取字段的唯一标识符
Definition Reflection.h:237
路由事件处理函数包装类,用于需要转换RoutedEventArgs为特定事件参数类型的情况
Definition EventHandlerWrapper.h:14
表示界面中的元素
Definition UIElement.h:90
Color GetRealTextColor() const
获取当前要显示的文本颜色
virtual uint64_t GetTag() const override
获取Tag
const Property< HorizontalAlignment > HorizontalAlignment
水平对齐方式
Definition UIElement.h:294
void RaiseRoutedEvent(RoutedEventType eventType)
触发路由事件
UIElement * GetNextElement()
获取当前元素在界面树上的下一个节点,若已是最后一个节点则返回根节点
void RegisterRoutedEvent(RoutedEventType eventType, T &obj, void(T::*handler)(UIElement &, RoutedEventArgs &))
注册成员函数作为路由事件处理函数,当事件已注册时会覆盖已注册的函数
Definition UIElement.h:1210
virtual void SetTag(uint64_t tag) override
设置Tag
void UnregisterRoutedEvent(RoutedEventType eventType)
取消对应类型路由事件的注册,该函数会移除对应事件所有的处理函数
void SetCursor(HCURSOR hCursor)
设置鼠标样式
virtual void OnTabStop()
通过tab键将焦点移动到当前元素时调用该函数
void RaiseRoutedEvent(RoutedEventArgs &eventArgs)
触发路由事件
bool AddChild(UIElement &element)
添加子元素
virtual bool OnMove(const Point &newClientPosition) override
接收到WM_MOVE时调用该函数
UIElement * GetPreviousElement()
获取当前元素在界面树上的上一个节点,若元素为第一个节点则返回最后一个节点
void UpdateSiblingsZOrder(bool invalidateMeasure=true)
更新兄弟元素的Z轴位置
virtual bool OnClose() override
接收到WM_CLOSE时调用该函数
double GetChildRightmost(bool update)
获取所有子元素在当前元素中最右边的位置(只考虑参与布局的子窗口且忽略悬浮的元素)
const ReadOnlyProperty< bool > IsFocusedViaTab
当前元素是否是通过按下Tab键获得的焦点
Definition UIElement.h:407
bool RemoveBinding(TProperty T::*prop)
移除指定属性的绑定对象
Definition UIElement.h:1431
const Property< bool > TabStop
表示用户是否可以通过按下Tab键将焦点移动到当前元素
Definition UIElement.h:339
void RegisterRoutedEvent(RoutedEventType eventType, const RoutedEventHandler &handler)
注册路由事件处理函数,当事件已注册时会覆盖已注册的函数
double & GetInternalArrangeOffsetY()
获取布局时子元素的垂直偏移量
virtual void ArrangeOverride(const Size &finalSize)
安排子元素的位置,可重写该函数以实现自定义布局
bool AddChild(UIElement *element)
添加子元素
const ReadOnlyProperty< DynamicObject * > CurrentDataContext
当前元素的有效数据上下文
Definition UIElement.h:418
const Property< double > MinHeight
最小高度,当值为负数或0时表示不限制
Definition UIElement.h:381
virtual ILayout & GetChildLayoutAt(int index) override
获取对应索引处的子元素,只索引参与布局的子元素
bool AddBinding(Binding *binding)
添加绑定对象
UIElement & operator[](int index) const
通过索引获取子元素
bool AddChild(UIElement &element, uint64_t layoutTag)
添加子元素并设置布局标记
void ResetCursor()
将鼠标样式设置为默认样式
virtual bool OnKeyUp(VirtualKey key, const KeyFlags &flags) override
接收到WM_KEYUP时调用该函数
UIElement & GetChildAt(int index) const
获取子元素
int IndexOf(UIElement *element)
获取指定元素的索引
virtual void OnRoutedEvent(RoutedEventArgs &eventArgs, const RoutedEventHandler &handler)
路由事件经过当前元素时调用该函数
bool RemoveBinding(FieldId propertyId)
移除指定属性的绑定对象
virtual Size GetDesireSize() const override
获取当前元素所需尺寸
virtual bool OnMouseLeftButtonDown(const Point &mousePosition, MouseKey keyState) override
接收到WM_LBUTTONDOWN时调用该函数
auto RegisterRoutedEvent(THandleObj &obj, void(THandleObj::*handler)(UIElement &, TEventArgs &)) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
根据事件参数类型注册成员函数作为路由事件,当事件已注册时会覆盖已注册的函数
Definition UIElement.h:1247
UIElement * GetRootElement()
获取当前元素所在界面树的根节点
virtual UIElement * ToUIElement() override
尝试将对象转换成UIElement
const ReadOnlyProperty< int > ChildCount
子元素数量
Definition UIElement.h:304
virtual bool OnColor(HDC hdc, HBRUSH &hRetBrush) override
父窗口接收到WM_CTLCOLORxxx时调用对应控件的该函数
void ClampDesireSize(sw::Size &size) const
限定指定尺寸在最小和最大尺寸之间
const ReadOnlyProperty< sw::Rect > LogicalRect
元素的逻辑位置和尺寸,即去除布局偏移以及拉伸影响的位置和尺寸
Definition UIElement.h:397
bool QueryAllChildren(const Func< UIElement *, bool > &queryFunc)
查询所有子元素,直到queryFunc返回false或所有子元素均被查询
virtual bool OnSetCursor(HWND hwnd, HitTestResult hitTest, int message, bool &result) override
接收到WM_SETCURSOR消息时调用该函数
virtual bool OnKeyDown(VirtualKey key, const KeyFlags &flags) override
接收到WM_KEYDOWN时调用该函数
bool RemoveChildAt(int index)
移除指定索引处的子元素
bool AddBinding(DataBinding *binding)
添加绑定到DataContext的绑定对象
UIElement * GetNextTabStopElement()
获取下一个TabStop属性为true的元素
virtual bool OnDropFiles(HDROP hDrop) override
接收到WM_DROPFILES时调用该函数
virtual bool OnKillFocus(HWND hNextFocus) override
接收到WM_KILLFOCUS时调用该函数
bool RemoveHandler(RoutedEventType eventType, T &obj, void(T::*handler)(UIElement &, RoutedEventArgs &))
移除已添加的类型为成员函数的路由事件处理函数
Definition UIElement.h:1339
auto AddChildren(First &&first, Rest &&...rest) -> typename std::enable_if< _CanAddChildren< First, Rest... >::value, int >::type
添加多个子元素
Definition UIElement.h:1178
bool ShowContextMenu(const Point &point, sw::HorizontalAlignment horz=sw::HorizontalAlignment::Left, sw::VerticalAlignment vert=sw::VerticalAlignment::Top)
弹出当前元素的上下文菜单
virtual bool OnSize(const Size &newClientSize) override
接收到WM_SIZE时调用该函数
virtual bool OnMouseMiddleButtonUp(const Point &mousePosition, MouseKey keyState) override
接收到WM_MBUTTONUP时调用该函数
const Property< uint64_t > LayoutTag
布局标记,对于不同的布局有不同含义
Definition UIElement.h:324
virtual void OnTabMove(bool forward)
通过tab键将焦点从当前元素移出时调用该函数
void ClampDesireSize(sw::Rect &rect) const
限定指定矩形的尺寸在最小和最大尺寸之间
const ReadOnlyProperty< bool > IsMeasureValid
当前元素的布局状态是否有效
Definition UIElement.h:371
virtual bool OnMouseRightButtonDown(const Point &mousePosition, MouseKey keyState) override
接收到WM_RBUTTONDOWN时调用该函数
void MoveToBottom()
移动到界面底部
virtual bool OnMouseMiddleButtonDown(const Point &mousePosition, MouseKey keyState) override
接收到WM_MBUTTONDOWN时调用该函数
void SetAlignment(sw::HorizontalAlignment horz, sw::VerticalAlignment vert)
设置对齐方式
virtual void OnNcHitTest(const Point &testPoint, HitTestResult &result) override
接收到WM_NCHITTEST后调用该函数
double GetChildBottommost(bool update)
获取所有子元素在当前元素中最底边的位置(只考虑参与布局的子窗口且忽略悬浮的元素)
void AddHandler(RoutedEventType eventType, const RoutedEventHandler &handler)
添加路由事件处理函数
const Property< bool > InheritTextColor
是否继承父元素的文本颜色
Definition UIElement.h:360
virtual void OnTextChanged() override
Text属性更改时调用此函数
void SetCursor(StandardCursor cursor)
设置鼠标样式
virtual uint64_t GetLayoutTag() const override
获取布局标记
const Property< bool > IsHitTestVisible
当前元素是否响应鼠标事件
Definition UIElement.h:402
virtual void ParentChanged(WndBase *newParent) override
父窗口改变时调用此函数
Color GetRealBackColor() const
获取当前要显示的背景颜色
const Property< uint64_t > Tag
储存用户自定义信息的标记
Definition UIElement.h:319
bool IsRootElement() const
判断当前元素是否为根节点
virtual void FontChanged(HFONT hfont) override
字体改变时调用该函数
virtual Size MeasureOverride(const Size &availableSize)
测量元素所需尺寸,无需考虑边框和边距
auto AddHandler(const Action< UIElement &, TEventArgs & > &handler) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
根据事件参数类型添加路由事件处理函数
Definition UIElement.h:1276
auto RemoveHandler(RoutedEventType eventType, THandleObj &obj, void(THandleObj::*handler)(UIElement &, TEventArgs &)) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&!std::is_same< TEventArgs, RoutedEventArgs >::value &&!sw::_IsTypedRoutedEventArgs< TEventArgs >::value, bool >::type
移除已添加的类型为成员函数的路由事件处理函数
Definition UIElement.h:1412
const Property< Thickness > Margin
边距
Definition UIElement.h:289
int IndexOf(UIElement &element)
获取指定元素的索引
bool RemoveHandler(RoutedEventType eventType, const RoutedEventHandler &handler)
移除已添加的路由事件处理函数
bool RemoveChild(UIElement *element)
移除子元素
virtual bool OnMouseWheel(int wheelDelta, const Point &mousePosition, MouseKey keyState) override
接收到WM_MOUSEWHEEL时调用该函数
virtual bool OnChar(wchar_t ch, const KeyFlags &flags) override
接收到WM_CHAR时调用该函数
auto RemoveHandler(const Action< UIElement &, TEventArgs & > &handler) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value, bool >::type
移除已添加的路由事件处理函数
Definition UIElement.h:1351
const Property< bool > CollapseWhenHide
是否在不可见时不参与布局
Definition UIElement.h:309
void ClearChildren()
移除所有子元素
virtual bool OnSetFocus(HWND hPrevFocus) override
接收到WM_SETFOCUS时调用该函数
auto AddHandler(THandleObj &obj, void(THandleObj::*handler)(UIElement &, TEventArgs &)) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
根据事件参数类型添加成员函数作为路由事件处理函数
Definition UIElement.h:1290
virtual void VisibleChanged(bool newVisible) override
Visible属性改变时调用此函数
const Property< double > MinWidth
最小宽度,当值为负数或0时表示不限制
Definition UIElement.h:376
bool IsLayoutUpdateConditionSet(sw::LayoutUpdateCondition condition)
判断指定的布局更新条件是否已设置
bool RemoveChild(UIElement &element)
移除子元素
const Property< DynamicObject * > DataContext
数据上下文
Definition UIElement.h:412
virtual bool OnMouseLeftButtonUp(const Point &mousePosition, MouseKey keyState) override
接收到WM_LBUTTONUP时调用该函数
void UpdateChildrenZOrder(bool invalidateMeasure=true)
更新子元素的Z轴位置
auto AddHandler(RoutedEventType eventType, THandleObj &obj, void(THandleObj::*handler)(UIElement &, TEventArgs &)) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&!std::is_same< TEventArgs, RoutedEventArgs >::value &&!sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
添加成员函数作为路由事件处理函数
Definition UIElement.h:1321
void Resize(const Size &size)
调整当前元素的尺寸
void AddHandler(RoutedEventType eventType, T &obj, void(T::*handler)(UIElement &, RoutedEventArgs &))
添加成员函数作为路由事件处理函数
Definition UIElement.h:1265
const Property< double > MaxHeight
最大高度,当值为负数或0时表示不限制
Definition UIElement.h:391
const Property< bool > Transparent
是否使用透明背景
Definition UIElement.h:355
virtual void OnRemovedChild(UIElement &element)
移除子元素后调用该函数
const Property< Color > BackColor
背景颜色,修改该属性会同时将Transparent属性设为false,对于部分控件该属性可能不生效
Definition UIElement.h:344
UIElement * GetPreviousTabStopElement()
获取上一个TabStop属性为true的元素
const Property< sw::LayoutUpdateCondition > LayoutUpdateCondition
触发布局更新的条件
Definition UIElement.h:366
virtual int GetChildLayoutCount() const override
获取参与布局的子元素数量
virtual void SetTextColor(Color color, bool redraw)
设置文本颜色
auto AddHandler(RoutedEventType eventType, const Action< UIElement &, TEventArgs & > &handler) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&!std::is_same< TEventArgs, RoutedEventArgs >::value &&!sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
添加路由事件处理函数
Definition UIElement.h:1303
DataContextChangedEventHandler DataContextChanged
数据上下问改变时触发该事件
Definition UIElement.h:284
void MoveToTop()
移动到界面顶部
auto RegisterRoutedEvent(const Action< UIElement &, TEventArgs & > &handler) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value >::type
根据事件参数类型注册路由事件,当事件已注册时会覆盖已注册的函数
Definition UIElement.h:1227
bool AddChild(UIElement *element, uint64_t layoutTag)
添加子元素并设置布局标记
double & GetInternalArrangeOffsetX()
获取布局时子元素的水平偏移量
const Property< bool > Float
元素是否悬浮,若元素悬浮则该元素不会随滚动条滚动而改变位置
Definition UIElement.h:334
virtual bool RequestBringIntoView(const sw::Rect &screenRect)
尝试将指定的矩形区域移动到可视区域内
virtual bool OnMouseLeave() override
接收到WM_MOUSELEAVE时调用该函数
virtual void Arrange(const sw::Rect &finalPosition) override
安排元素位置
virtual void OnMenuCommand(int id) override
当WM_COMMAND接收到菜单命令时调用该函数
virtual ~UIElement()=0
析构函数,这里用纯虚函数使该类成为抽象类
auto RemoveHandler(THandleObj &obj, void(THandleObj::*handler)(UIElement &, TEventArgs &)) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&sw::_IsTypedRoutedEventArgs< TEventArgs >::value, bool >::type
移除已添加的类型为成员函数的路由事件处理函数
Definition UIElement.h:1370
const Property< double > MaxWidth
最大宽度,当值为负数或0时表示不限制
Definition UIElement.h:386
virtual bool SetParent(WndBase *parent) override
设置父窗口
const ReadOnlyProperty< UIElement * > Parent
指向父元素的指针,当前元素为顶级窗口时该值为nullptr
Definition UIElement.h:314
const Property< VerticalAlignment > VerticalAlignment
垂直对齐方式
Definition UIElement.h:299
bool BringIntoView()
尝试将当前元素移动到可视区域内
bool IsRoutedEventRegistered(RoutedEventType eventType)
判断路由事件是否已存在事件处理函数
auto RemoveHandler(RoutedEventType eventType, const Action< UIElement &, TEventArgs & > &handler) -> typename std::enable_if< std::is_base_of< RoutedEventArgs, TEventArgs >::value &&!std::is_same< TEventArgs, RoutedEventArgs >::value &&!sw::_IsTypedRoutedEventArgs< TEventArgs >::value, bool >::type
移除已添加的路由事件处理函数
Definition UIElement.h:1388
virtual bool OnMouseMove(const Point &mousePosition, MouseKey keyState) override
接收到WM_MOUSEMOVE时调用该函数
virtual void OnAddedChild(UIElement &element)
添加子元素后调用该函数
const Property< sw::ContextMenu * > ContextMenu
右键按下时弹出的菜单
Definition UIElement.h:329
virtual void OnMinMaxSizeChanged()
当MinWidth、MinHeight、MaxWidth或MaxHeight属性更改时调用此函数
virtual bool OnMouseRightButtonUp(const Point &mousePosition, MouseKey keyState) override
接收到WM_RBUTTONUP时调用该函数
const Property< Color > TextColor
文本颜色,修改该属性会同时将InheritTextColor属性设为false,对于部分控件该属性可能不生效
Definition UIElement.h:349
auto AddChildren(T &&child) -> typename std::enable_if< _CanAddChild< T >::value, int >::type
添加多个子元素
Definition UIElement.h:1194
UIElement()
初始化UIElement
virtual void Measure(const Size &availableSize) override
测量元素所需尺寸
bool AddBinding(BindingBase *binding)
添加绑定对象
virtual bool OnContextMenu(bool isKeyboardMsg, const Point &mousePosition) override
接收到WM_CONTEXTMENU后调用目标控件的该函数
virtual void SetBackColor(Color color, bool redraw)
设置背景颜色
void InvalidateMeasure()
使元素的布局状态失效,并立即触发布局更新
表示一个Windows窗口,是所有窗口和控件的基类
Definition WndBase.h:41
颜色
Definition Color.h:16
表示字段的唯一标识符
Definition Reflection.h:177
https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input#keystroke-message-flags
Definition Keys.h:12
表示相对于左上角的点坐标
Definition Point.h:15
表示一个矩形区域
Definition Rect.h:17
路由事件的参数
Definition RoutedEvent.h:149
尺寸
Definition Size.h:15
表示矩形区域周围边框的厚度
Definition Thickness.h:15
结构体模板,用于检测类型T是否包含事件类型信息
Definition RoutedEventArgs.h:51