当前位置:网站首页>C # customize and dynamically switch cursor
C # customize and dynamically switch cursor
2022-07-01 01:33:00 【Dotnet cross platform】
This article is authorized by the original author to share it twice in an original way , Welcome to reprint 、 Share .
Original author : Blogs in Tang, song, yuan, Ming and Qing Dynasties
Original address :https://www.cnblogs.com/kybs0/p/14873136.html
The system has many cursor types :Cursors class (System.Windows.Input) | Microsoft Docs[1]
This chapter describes how to customize the cursor 、 And dynamically switch cursor types .
Dynamically switch cursor types
Take whiteboard writing as an example :
During mouse operation
Cursor
Red dot ;When touching
Cursor
It's empty ;
public MainWindow()
{
InitializeComponent();
MouseEnter += (s, e) =>
{
ShowMouseCursor(e);
};
MouseMove += (s, e) =>
{
ShowMouseCursor(e);
};
StylusMove += (s, e) =>
{
ShowNoneCursor();
};
}
Set the cursor display ;
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());
}
When touching writing , There will be a default cursor , So set the cursor blank when touching here Cursors.None
;
Mouse.UpdateCursor()
The cursor can be forcibly updated . Of course , Without calling this update method, the naked eye can't see anything ...

The cursor switching effect is as above , The first paragraph is written with a mouse , Behind it is touch writing , Cursor type has switching . See the following figure for the red dot cursor customization scheme .
Custom cursor 1) Customize a solid color Circular cursor
:
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) You can also use image resources BitmapSource
To generate the cursor ;
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
class ;
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);
}
Reference material :
WPF Custom mouse cursor - DH_ Green leaves - Blog Garden [2]
[WPF] Custom mouse pointer - Zhou Yinhui - Blog Garden [3]
Reference material
[1]
Cursors class (System.Windows.Input) | Microsoft Docs: https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.input.cursors?view=net-5.0
[2]WPF Custom mouse cursor - DH_ Green leaves - Blog Garden : https://www.cnblogs.com/dhqy/p/7754176.html
[3][WPF] Custom mouse pointer - Zhou Yinhui - Blog Garden : https://www.cnblogs.com/zhouyinhui/archive/2010/05/28/1746502.html
边栏推荐
- 微研所,微生物检验中常用的生化反应
- 为什么要搭建个人博客
- zabbix如何配置告警短信?(预警短信通知设置流程)
- Microbiological health, why is food microbiological testing important
- Matlab farthest point sampling (FPS improved version)
- [问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
- Gin configuration file
- Institute of Microbiology, commonly used biochemical reactions in microbiological testing
- [Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
- neo4j安装、运行以及项目的构建和功能实现
猜你喜欢
随机推荐
【动态规划】路径dp:931. Minimum Falling Path Sum
Strictmode jamming and leakage detection -strictmode principle (2)
Exploration and practice of "flow batch integration" in JD
元宇宙为 VR/AR 带来的新机会
Open3D 点云包围盒
[deepin] common sets
Construction and beautification of personal blog
gin 配置文件
短信在企业中的应用有哪些?
Using recyclerreview to show banner is very simple
Pre training / transfer learning of models
visual studio 2019 下载
Installing mongodb database in Windows Environment
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
qt5-MVC:数据可视化的层次揭秘
一些本质的区别
【多源bfs】934. Shortest Bridge
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Creating ASCII art with C #