当前位置:网站首页>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
CursorRed dot ;When touching
CursorIt'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
边栏推荐
- User defined annotation implementation verification
- 二季度最后一天
- "Open math input panel" in MathType editing in win11 is gray and cannot be edited
- 数字IC设计流程总结
- 编译安装oh-my-zsh
- Uniapp official component clicking item is invalid, solution
- 使用 C# 创造 ASCII 艺术
- Draw some interesting figures with flutter's canvas
- 微生物检测,土壤微生物的作用有哪些?
- About the general input operation mode of unity
猜你喜欢

Institute of Microbiology, commonly used biochemical reactions in microbiological testing

Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images

New opportunities for vr/ar brought by metauniverse

Exploration and practice of "flow batch integration" in JD

Install redis database and download redis Desktop Manager in win11

微生物健康,食品微生物检测为什么很重要

zabbix如何配置告警短信?(预警短信通知设置流程)

1500w播放下还藏着什么热点?B站2个未来趋势你不得错过

个人博客搭建与美化
![[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number](/img/51/e48e222c14f4a4e9f2be91a677033f.png)
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
随机推荐
Open3D 点云颜色渲染
Service grid ASM year end summary: how do end users use the service grid?
Solve idea:class' xxx 'not found in module' xxx‘
关于白盒测试,这些技巧你得游刃有余~
The argument type 'function' can't be assigned to the parameter type 'void function()‘
About the general input operation mode of unity
流批一体在京东的探索与实践
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
StrictMode分析Activity泄漏-StrictMode原理(3)
使用StrictMode-StrictMode原理(1)
StrictMode分析Registion-StrictMode原理(4)
None of the following candidates is applicable because of a receiver type mismatch
DC學習筆記正式篇之零——綜述與基本流程介紹
亲测有效,快速创建JMeter桌面快捷方式
Open3d point cloud bounding box
Document service design
3dsmax plug-in development traversal node object and object acquisition and inode transformation matrix description
[queue] 933 Number of Recent Calls
Typora的使用
MFC TCP通信服务端客户端Demo备忘vs2019