WinUI3入门11:改变鼠标形状 设置光标
初级代码游戏的专栏介绍与文章目录-CSDN博客
我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。
这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。
源码指引:github源码指引_初级代码游戏的博客-CSDN博客
C#是我多年以来的业余爱好,新搞的东西能用C#的就用C#了。
前面我们做了可以用鼠标改变比例的SplitPanel,按照我们以前的习惯,这种情景应该使用SizeWestEast光标,就是左右箭头。
通常很容易设置控件/窗口的光标(鼠标形状),设置一个属性即可。然而,WinUI3里面做这件事有些麻烦,因为控件的光标属性ProtectedCursor是保护的。
目录
一、创建控件的子类,暴露受保护的ProtectedCursor
二、替换xaml控件名称
三、代码设置光标
四、常用光标
一、创建控件的子类,暴露受保护的ProtectedCursor
为此,首先我们需要继承一个类,然后在新的类里面操作光标属性,当然,只需要把光标属性暴露出来就可以了:
首先新建一个类:
主要代码替换掉:
public class PanelBar : Microsoft.UI.Xaml.Controls.Grid{public InputCursor Cursor{get => ProtectedCursor;set => ProtectedCursor = value;}}
注意我是继承自Grid的。原来我用的控件是Border啊,为什么不用Border呢?因为Border是个密封类,不能被继承。幸好我换成Grid以后一切正常,用到的属性都支持。
二、替换xaml控件名称
<!-- 分隔条 --><local:PanelBar Grid.Column="1" x:Name="border_split" BorderBrush="Gray" BorderThickness="3" Margin="0,2,0,2" />
改动的仅仅是:
原来是“Border”,替换即可。后面的属性Grid都支持。
三、代码设置光标
简简单单一句:
border_split.Cursor = Microsoft.UI.Input.InputSystemCursor.Create(Microsoft.UI.Input.InputSystemCursorShape.SizeWestEast);
四、常用光标
枚举值InputSystemCursorShape表示系统自带光标:
Name | Value | Description |
---|---|---|
Arrow | 0 | A standard arrow pointing left-upward (northwest). |
Cross | 1 | A crosshair. |
Hand | 3 | A hand. |
Help | 4 | An arrow pointing left-upward (northwest) with a question mark. |
IBeam | 5 | An "I"-shape, typically used for text insertion and selection. |
SizeAll | 6 | Crossed arrows pointing north, south, east, and west, typically used for resizing. |
SizeNortheastSouthwest | 7 | A dual arrow pointing right-upward and left-downward, typically used for element sizing. |
SizeNorthSouth | 8 | A dual arrow pointing upward and downward, typically used for vertical sizing. |
SizeNorthwestSoutheast | 9 | A dual arrow pointing left-upward and right-downward, typically used for element sizing. |
SizeWestEast | 10 | A dual arrow pointing left and right, typically used for horizontal sizing. |
UniversalNo | 11 | A red circle with 45-degree diagonal line from upper left to lower right, typically used to indicate an action that cannot be performed. |
UpArrow | 12 | An arrow pointing up. |
Wait | 13 | An animated, cycling cursor, typically used to indicate that an element or behavior is busy and cannot respond. |
Pin | 14 | A hand with a pin symbol. |
Person | 15 | A hand with a person symbol. |
AppStarting | 16 | The cursor that appears when an app starts, typically used to indicate something is loading but the UI can handle input. |
Arrow是默认的箭头光标,Size开头的是调整大小的光标,Hand是个手,Help是箭头旁边带个问号,Wait是等待光标。
(这里是文档结束)