当前位置:网站首页>C# 自定义并动态切换光标
C# 自定义并动态切换光标
2022-07-01 00:41:00 【dotNET跨平台】
本文经原作者授权以原创方式二次分享,欢迎转载、分享。
原文作者:唐宋元明清的博客
原文地址:https://www.cnblogs.com/kybs0/p/14873136.html
系统有很多光标类型 :Cursors 类 (System.Windows.Input) | Microsoft Docs[1]
本章介绍如何自定义光标、并动态切换光标类型。
动态切换光标类型
以白板书写为例:
鼠标操作时
Cursor为红点;触摸时
Cursor为空;
public MainWindow()
{
InitializeComponent();
MouseEnter += (s, e) =>
{
ShowMouseCursor(e);
};
MouseMove += (s, e) =>
{
ShowMouseCursor(e);
};
StylusMove += (s, e) =>
{
ShowNoneCursor();
};
}设置光标显示;
private void ShowNoneCursor()
{
if (Cursor == Cursors.None)
{
return;
}
Cursor = Cursors.None;
Mouse.UpdateCursor();
}
private void ShowMouseCursor(MouseEventArgs e)
{
if (e.StylusDevice != null && e.StylusDevice.Id > -1)
{
return;
}
if (Cursor == GetFillCursor())
{
return;
}
Cursor = GetFillCursor();
Mouse.UpdateCursor();
}
private Cursor _fillCursor = null;
private Cursor GetFillCursor()
{
return _fillCursor ?? (_fillCursor = CursorHelper.CreateFillCursor());
}触摸书写时,会有个默认光标,所以此处把触摸时的光标置空Cursors.None;
Mouse.UpdateCursor()能强制更新光标。当然,不调用这个更新方法肉眼其实也看不出啥。。。

光标切换效果如上,前面一段是用鼠标书写,后面是触摸书写,光标类型有切换。红点光标自定义方案见下方。
自定义光标1)自定义一个纯色的圆形光标:
public static Cursor CreateFillCursor(int size = 24, Brush fillBrush = null)
{
int unitSize = size / 4;
var bmp = new Bitmap(size, size);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clip = new Region(new Rectangle(0, 0, size, size));
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
using (var pen = new Pen(fillBrush ?? Brushes.Red, unitSize))
{
g.DrawEllipse(pen, new Rectangle(unitSize, unitSize, unitSize, unitSize));
}
}
return BitmapCursor.CreateBmpCursor(bmp);
}2)也可以通过图片资源BitmapSource来生成光标;
public static Cursor CreateFromBitmapSource(BitmapSource source)
{
var bitmap = BitmapSourceToBitmap(source);
return BitmapCursor.CreateBmpCursor(bitmap);
}
private static Bitmap BitmapSourceToBitmap(BitmapSource source)
{
using (var stream = new MemoryStream())
{
var e = new BmpBitmapEncoder();
e.Frames.Add(BitmapFrame.Create(source));
e.Save(stream);
var bmp = new Bitmap(stream);
return bmp;
}
}2)BitmapCursor类;
internal class BitmapCursor : SafeHandle
{
public override bool IsInvalid => handle == (IntPtr)(-1);
public static Cursor CreateBmpCursor(Bitmap cursorBitmap)
{
var c = new BitmapCursor(cursorBitmap);
return CursorInteropHelper.Create(c);
}
protected BitmapCursor(Bitmap cursorBitmap)
: base((IntPtr)(-1), true)
{
handle = cursorBitmap.GetHicon();
}
protected override bool ReleaseHandle()
{
bool result = DestroyIcon(handle);
handle = (IntPtr)(-1);
return result;
}
[DllImport("user32")]
private static extern bool DestroyIcon(IntPtr hIcon);
}参考资料:
WPF 自定义鼠标光标 - DH_青叶 - 博客园[2]
[WPF]自定义鼠标指针 - 周银辉 - 博客园[3]
参考资料
[1]
Cursors 类 (System.Windows.Input) | Microsoft Docs: https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.input.cursors?view=net-5.0
[2]WPF 自定义鼠标光标 - DH_青叶 - 博客园: https://www.cnblogs.com/dhqy/p/7754176.html
[3][WPF]自定义鼠标指针 - 周银辉 - 博客园: https://www.cnblogs.com/zhouyinhui/archive/2010/05/28/1746502.html
边栏推荐
- Service
- (学习力+思考力) x 行动力,技术人成长的飞轮效应总结
- The communication mechanism and extension of Supervisor
- uniapp官方组件点击item无效,解决方案
- Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
- K210工地安全帽
- Kongyiji's first question: how much do you know about service communication?
- K210 site helmet
- Q弹松软的大号吐司,带来更舒服的睡眠
- 2021电赛F题openmv和K210调用openmv api巡线,完全开源。
猜你喜欢

Why not two or four TCP handshakes

For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!

Locking relay ydb-100, 100V

TCP三次握手为什么不是两次或四次

Principes de formation de la programmation robotique

Q弹松软的大号吐司,带来更舒服的睡眠

Exercises on recursion in C language

K210 site helmet

What if the disk of datanode is full?

Left join displays the specified value when the left join matching data is null
随机推荐
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
visual studio 2019 快捷键备忘
Q弹松软的大号吐司,带来更舒服的睡眠
技术人进阶画业务大图,手把手教学来了
Win11安装redis 数据库以及redis desktop manager的下载
使用StrictMode-StrictMode原理(1)
Practical shell knowledge
Installing mongodb database in Windows Environment
StrictMode分析Registion-StrictMode原理(4)
Analysis of blocktoken principle
染色法判断二分图
K210工地安全帽
Chromatic judgement bipartite graph
StrictMode卡顿与泄漏检测-StrictMode原理(2)
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
基础知识之三——标准单元库
蒹葭苍苍,白露为霜。
自定义注解实现校验
CSDN common complex formula template record