当前位置:网站首页>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
边栏推荐
- 软件开发完整流程
- Introduction and principle analysis of cluster and LVS
- [Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
- Gin configuration file
- [Deepin] 常用集合
- Microbiological health, why is food microbiological testing important
- 数字IC设计流程总结
- What will Web3 bring in the future?
- Matlab farthest point sampling (FPS improved version)
- 编译安装oh-my-zsh
猜你喜欢
Unknown database connection database error
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Poor students can also play raspberry pie
微生物健康,食品微生物检测为什么很重要
neo4j安装、运行以及项目的构建和功能实现
Call the classic architecture and build the model based on the classic
编译安装oh-my-zsh
Qt5 mvc: revealing the secrets of data visualization
Install redis database and download redis Desktop Manager in win11
随机推荐
3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
Open3D 点云包围盒
工作6年,来盘点一下职场人混迹职场的黄金法则
用recyclerReview展示Banner,很简单
StrictMode分析Activity泄漏-StrictMode原理(3)
Visual studio 2019 Download
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
visual studio 2019 下载
Service grid ASM year end summary: how do end users use the service grid?
未来的 Web3会带来什么?
图的连通性基础
Strictmode jamming and leakage detection -strictmode principle (2)
TypeError: Argument ‘angle‘ can not be treated as a double
New opportunities for vr/ar brought by metauniverse
Solve idea:class' xxx 'not found in module' xxx‘
ESP8266 RC522
OCR的一些项目
做生意更加务实
Matlab farthest point sampling (FPS improved version)
基础知识之三——标准单元库