網(wǎng)站首頁 編程語言 正文
有的時候,一個自定義的鼠標(biāo)光標(biāo)能給你的程序增色不少。本文這里介紹一下如何在.net桌面程序中自定義鼠標(biāo)光標(biāo)。由于.net的桌面程序分為WinForm和WPF兩種,這里分別介紹一下。
WinForm程序
對于WinForm程序,可以通過修改Control.Cursor屬性來實現(xiàn)光標(biāo)的修改,如果我們有光標(biāo)文件的話,可以直接通過如下代碼實現(xiàn)自定義光標(biāo):
this.Cursor = new Cursor("myCursor.cur");
但這種方式不是本文介紹的重點,本文主要介紹如何自己繪制光標(biāo),這樣則具有更多的可控性和靈活性。
創(chuàng)建一個自定義光標(biāo),首先需要定義需要一個光標(biāo)結(jié)構(gòu)?ICONINFO?,它的.net版本如下:
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
然后通過GetIconInfo?and?CreateIconIndirect兩個函數(shù)來合成光標(biāo)。完整代碼如下:?
public class CursorHelper
{
static class NativeMethods
{
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[DllImport("user32.dll")]
public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
}
public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
{
var icon = new NativeMethods.IconInfo
{
xHotspot = xHotSpot,
yHotspot = yHotSpot,
fIcon = false
};
NativeMethods.GetIconInfo(bmp.GetHicon(), ref icon);
return new Cursor(NativeMethods.CreateIconIndirect(ref icon));
}
}
測試代碼為:
using (Bitmap bitmap = new Bitmap(21, 26))
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawRectangle(Pens.Red, 0, 0, 20, 25);
this.Cursor = CursorHelper.CreateCursor(bitmap, 3, 3);
}
WPF程序
至于WPF程序,和WinForm程序是非常類似的,一方面,它也可以通過光標(biāo)文件來實現(xiàn)寫入Cursor屬性來自定義光標(biāo)文件。
至于自己繪制光標(biāo),上面的代碼基本上也是可以復(fù)用的,不過相對的要重新封裝一下,完整代碼如下:?
public class CursorHelper
{
static class NativeMethods
{
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[DllImport("user32.dll")]
public static extern SafeIconHandle CreateIconIndirect(ref IconInfo icon);
[DllImport("user32.dll")]
public static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SafeIconHandle()
: base(true)
{
}
protected override bool ReleaseHandle()
{
return NativeMethods.DestroyIcon(handle);
}
}
static Cursor InternalCreateCursor(System.Drawing.Bitmap bitmap, int xHotSpot, int yHotSpot)
{
var iconInfo = new NativeMethods.IconInfo
{
xHotspot = xHotSpot,
yHotspot = yHotSpot,
fIcon = false
};
NativeMethods.GetIconInfo(bitmap.GetHicon(), ref iconInfo);
var cursorHandle = NativeMethods.CreateIconIndirect(ref iconInfo);
return CursorInteropHelper.Create(cursorHandle);
}
public static Cursor CreateCursor(UIElement element, int xHotSpot = 0, int yHotSpot = 0)
{
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(), element.DesiredSize));
var renderTargetBitmap = new RenderTargetBitmap(
(int)element.DesiredSize.Width, (int)element.DesiredSize.Height,
96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(element);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
using (var bitmap = new System.Drawing.Bitmap(memoryStream))
{
return InternalCreateCursor(bitmap, xHotSpot, yHotSpot);
}
}
}
}
需要注意的是,由于使用的System.Drawing.BitMap,是需要引用System.Drawing.dll的
封裝之后,是可以直接傳入UIElement作為自繪制的光標(biāo)的,得益于WPF的強(qiáng)大繪圖功能,是可以非常容易的繪制漂亮的光標(biāo)的。測試代碼如下:
this.Cursor= CursorHelper.CreateCursor(new UserControl1());
原文鏈接:https://www.cnblogs.com/TianFang/p/5186497.html
相關(guān)推薦
- 2022-07-01 python神經(jīng)網(wǎng)絡(luò)ShuffleNetV2模型復(fù)現(xiàn)詳解_python
- 2022-05-31 Python中的列表及其操作方法_python
- 2024-03-10 SpringMVC中Model和ModelAndView的區(qū)別
- 2022-01-16 對象的綁定、滾輪滾動事件及鍵盤事件
- 2022-07-08 Python如何通過地址獲取變量_python
- 2022-10-11 詳解pandas?df.iloc[]的典型用法_python
- 2024-03-18 bootstrap application 和 nacos 中配置文件的優(yōu)先級
- 2022-09-22 vant tab組件動態(tài)改變van-tab title后顯示不全問題,需要手動滑動
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支