前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 鼠标光标大全

WPF 鼠标光标大全

作者头像
林德熙
发布2021-12-23 10:53:47
2.3K0
发布2021-12-23 10:53:47
举报
文章被收录于专栏:林德熙的博客

在 WPF 中,可以通过 Cursors 静态类里面的各个预定义属性来设置移入到某个元素时,鼠标光标的外观样式。今天小伙伴问我哪个是鼠标移动控件的鼠标外观属性,在看到 Cursors 静态类里面那么多属性时,我也不知道用哪个好。于是我就写了一个叫鼠标光标大全的应用,可以让大家快速知道有哪些可以用的光标

以下是我的应用界面

鼠标移动到不同的属性上,即可修改当前的鼠标外观

这个应用我在 githubgitee 上完全开源

不嫌弃麻烦的话,还请自行下载代码,自己构建。可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

代码语言:javascript
复制
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 0db985a1c785e0a3d090c2eb2bcb2da9e032f156

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

代码语言:javascript
复制
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

获取代码之后,进入 KeregabaneyairWeqainowafobar 文件夹

如果嫌弃麻烦的话,而且 CSDN 积分不要钱的话,还请到 CSDN 下载 我构建好的应用

以下是核心的逻辑,可以通过编写一个 ListView 控件,让这个控件里面的每一项绑定光标。在 WPF 中,可以在不同的元素上,给各个元素设置自己光标,如果没有设置,那将会使用元素的上一层容器的鼠标光标属性

代码语言:javascript
复制
          <Border Margin="10,10,10,10" Height="50" Width="90" Background="#AFAFAF" 
                  IsHitTestVisible="True"
                  Cursor="{Binding Cursor}">
            <TextBlock Text="{Binding Name}" 
                       HorizontalAlignment="Center" 
                       VerticalAlignment="Center" />
          </Border>

以上的代码是放在 ItemTemplate 作为列表里面的某个元素

绑定的 Model 定义如下

代码语言:javascript
复制
    public class CursorInfo
    {
        public CursorInfo(Cursor cursor)
        {
            Name = cursor.ToString();
            Cursor = cursor;
        }

        public string Name { get; }

        public Cursor Cursor { get; }
    }

在 MainWindow 设置了所有默认的光标

代码语言:javascript
复制
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            CursorInfoList = new List<CursorInfo>()
            {
                new CursorInfo(Cursors.AppStarting),
                new CursorInfo(Cursors.Arrow),
                new CursorInfo(Cursors.ArrowCD),
                new CursorInfo(Cursors.Cross),
                new CursorInfo(Cursors.Hand),
                new CursorInfo(Cursors.Help),
                new CursorInfo(Cursors.IBeam),
                new CursorInfo(Cursors.No),
                new CursorInfo(Cursors.None),
                new CursorInfo(Cursors.Pen),
                new CursorInfo(Cursors.ScrollAll),
                new CursorInfo(Cursors.ScrollE),
                new CursorInfo(Cursors.ScrollN),
                new CursorInfo(Cursors.ScrollNE),
                new CursorInfo(Cursors.ScrollNS),
                new CursorInfo(Cursors.ScrollNW),
                new CursorInfo(Cursors.ScrollS),
                new CursorInfo(Cursors.ScrollSE),
                new CursorInfo(Cursors.ScrollSW),
                new CursorInfo(Cursors.ScrollW),
                new CursorInfo(Cursors.ScrollWE),
                new CursorInfo(Cursors.SizeAll),
                new CursorInfo(Cursors.SizeNESW),
                new CursorInfo(Cursors.SizeNS),
                new CursorInfo(Cursors.SizeNWSE),
                new CursorInfo(Cursors.SizeWE),
                new CursorInfo(Cursors.UpArrow),
                new CursorInfo(Cursors.Wait),
            };

            DataContext = this;

            InitializeComponent();
        }

        public List<CursorInfo> CursorInfoList { get; }

        public CursorInfo CurrentCursor
        {
            set
            {
                _currentCursor = value;
                Cursor = value.Cursor;
            }
            get => _currentCursor;
        }

        private CursorInfo _currentCursor;
    }

全部的 XAML 代码如下

代码语言:javascript
复制
<Window x:Class="KeregabaneyairWeqainowafobar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:KeregabaneyairWeqainowafobar"
        mc:Ignorable="d"
        Title="光标" Height="600" Width="800">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <ListView Grid.Column="1" Margin="10,10,10,10" ItemsSource="{Binding CursorInfoList}"
              SelectedItem="{Binding CurrentCursor,Mode=TwoWay}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
      <ListView.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel />
        </ItemsPanelTemplate>
      </ListView.ItemsPanel>
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
      </ListBox.ItemContainerStyle>
      <ListView.ItemTemplate>
        <DataTemplate DataType="local:CursorInfo">
          <Border Margin="10,10,10,10" Height="50" Width="90" Background="#AFAFAF" 
                  IsHitTestVisible="True"
                  Cursor="{Binding Cursor}">
            <TextBlock Text="{Binding Name}" 
                       HorizontalAlignment="Center" 
                       VerticalAlignment="Center" />
          </Border>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </Grid>
</Window>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档