前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >win10 uwp 改变鼠标 设置光标移动鼠标

win10 uwp 改变鼠标 设置光标移动鼠标

作者头像
林德熙
发布2018-09-18 16:58:26
2.7K0
发布2018-09-18 16:58:26
举报
文章被收录于专栏:林德熙的博客林德熙的博客

经常在应用需要修改光标,显示点击、显示输入,但是有些元素不是系统的,那么如何设置鼠标? 本文主要:UWP 设置光标,UWP 移动鼠标

设置光标

需要写一点代码来让程序比较容易看到,什么光标对于什么。

UWP 设置的光标有些看不懂,直接看不知道他是干什么

在xaml写代码:

代码语言:javascript
复制
        <StackPanel>
            <TextBlock Margin="10,10,10,10" Text="Hand" PointerEntered="button_OnPointerEntered"></TextBlock>
            <TextBlock Margin="10,10,10,10" Text="Arrow" PointerEntered="button_OnPointerEntered"></TextBlock>
            <TextBlock Margin="10,10,10,10" Text="Cross" PointerEntered="button_OnPointerEntered"></TextBlock>
            <TextBlock Margin="10,10,10,10" Text="Help" PointerEntered="button_OnPointerEntered"></TextBlock>
            <TextBlock Margin="10,10,10,10" Text="Beam" PointerEntered="button_OnPointerEntered"></TextBlock>
        </StackPanel>

代码写好了,他可以在鼠标移入TextBlock 进入函数,可以在函数修改UWP 鼠标光标

首先使用Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor 设置或获取光标。

需要设置光标需要用Windows.UI.Core.CoreCursor

他有一些比较多用的类型,下面是他们对于代码

  • Hand 点击
  • Arrow 正常
  • Cross 十字
  • Help 帮助
  • Wait 等待
  • Beam 输入

于是对应界面

代码语言:javascript
复制
        private void button_OnPointerEntered(object sender, PointerRoutedEventArgs e)
        {
            string str = (sender as TextBlock)?.Text as string;
            uint n = 1;
            switch (str)
            {
                case "Hand":
                    Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, n);
                    break;
                case "Arrow": Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, n); break;
                case "Cross": Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Cross, n); break;
                case "Help": Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Help, n); break;
                case "Beam": Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.IBeam, n); break;
            }
            
        }

试试把代码放到工程,可以看到UWP 光标改变。

如果不知道 n 是什么,我可以说,自定义光标就是使用n,但是复杂。

很少会有需要自己做光标。如果需要自己做,请看自定义光标

移动鼠标

有时候需要把鼠标移动到一个元素上,UWP 移动鼠标和改变光标一样。

移动鼠标,设置CoreWindow.PointerPosition

在界面放一个按钮,点击他,移动鼠标

代码语言:javascript
复制
             var p = new Point(Window.Current.Bounds.X + Window.Current.Bounds.Width / 2, Window.Current.Bounds.Y + Window.Current.Bounds.Height / 2);
            Window.Current.CoreWindow.PointerPosition = p;

这样移动很简单,移动是屏幕坐标,不是应用坐标,需要对移动加上窗口移动

https://blogs.msdn.microsoft.com/devfish/2012/08/01/customcursors-in-windows-8-csharp-metro-applications/


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 设置光标
  • 移动鼠标
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档