有的时候,一个自定义的鼠标光标能给你的程序增色不少。本文这里介绍一下如何在.net桌面程序中自定义鼠标光标。由于.net的桌面程序分为WinForm和WPF两种,这里分别介绍一下。

WinForm程序

对于WinForm程序,可以通过修改Control.Cursor属性来实现光标的修改,如果我们有光标文件的话,可以直接通过如下代码实现自定义光标:

this.Cursor = new Cursor("myCursor.cur");

但这种方式不是本文介绍的重点,本文主要介绍如何自己绘制光标,这样则具有更多的可控性和灵活性。

创建一个自定义光标,首先需要定义需要一个光标结构 ICONINFO ,它的.net版本如下:

public struct IconInfo
    {
        public
bool
fIcon;
        public
int
xHotspot;
        public
int
yHotspot;
        public
IntPtr
hbmMask;
        public
IntPtr
hbmColor;
    }

然后通过GetIconInfo and CreateIconIndirect两个函数来合成光标。完整代码如下:

     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程序是非常类似的,一方面,它也可以通过光标文件来实现写入Cursor属性来自定义光标文件。

至于自己绘制光标,上面的代码基本上也是可以复用的,不过相对的要重新封装一下,完整代码如下:

     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 = , int yHotSpot = )
{
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,
, , 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作为自绘制的光标的,得益于WPF的强大绘图功能,是可以非常容易的绘制漂亮的光标的。测试代码如下:

this.Cursor= CursorHelper.CreateCursor(new UserControl1());

在.net桌面程序中自定义鼠标光标的更多相关文章

  1. 自定义鼠标光标cursor

    通过css属性 Cursor:url()自定义鼠标光标. {cursor:url('图标路径'),default;} url是自定义鼠标图标路径 default指的是定义默认的光标(通常是一个箭头), ...

  2. WPF 自定义鼠标光标

    在程序中使用自定义鼠标光标的三种方式: RadioButton senderButton = sender as RadioButton; 方式一:                       str ...

  3. Html中自定义鼠标的形状

    Html中自定义鼠标的形状 <html> <head> <title>自定义的鼠标形状</title> <meta http-equiv=&quo ...

  4. 2 weekend110的hadoop的自定义排序实现 + mr程序中自定义分组的实现

    我想得到按流量来排序,而且还是倒序,怎么达到实现呢? 达到下面这种效果, 默认是根据key来排, 我想根据value里的某个排, 解决思路:将value里的某个,放到key里去,然后来排 下面,开始w ...

  5. 微信小程序中自定义modal

    微信小程序中自定义modal .wxml <modal hidden="{{hidden}}" title="这里是title" confirm-text ...

  6. HTML中的鼠标光标属性

    在网页中默认的鼠标指针只有两种,一种是最普通的箭头,另一种是当移动到链接上时出现的“小手”.但现在越来越多的网页都使用了CSS鼠标指针技术,当将鼠标移动到链接上时,可以看到多种不同的效果.CSS可以通 ...

  7. Combox程序中自定义数据源

    有combox控件,命名为cbxPutStatus,在程序中为其手工定义数据源,并绑定 private void POrderSplitFrm_Load(object sender, EventArg ...

  8. JQuery模拟网页中自定义鼠标右键菜单

    题外话.......最近在开发一个网站项目的时候,需要用到网页自定义右键菜单,在网上看了各路前辈大神的操作,头晕目眩,为了达到目的,突然灵机一动,于是便有了这篇文章. 先放个效果图(沾沾自喜,大神勿喷 ...

  9. 自定义鼠标光标,制作cur,设置热点,中心点。

    ..没哈好说的,,只是推荐一个软件 ArtCursor..非常好用. 关于另外一个更改光标的重要问题:鼠标的hotspot,也就是鼠标的作用点问题,本人关于这个问题纠结了很久,始终无法找到更改HCUR ...

随机推荐

  1. SQL中case when then用法

    sql语句判断方式之一Case.具有两种格式:简单的Case函数.Case搜索函数. 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' E ...

  2. Spring利器之包扫描器

    在学习Spring这门技术中为了大大减少applicationContext.xml配置的代码量于是有了包扫描器. 闲话不多说我们马上来实现一下吧 示例架构如下: 第一步我们先来修改我们的配置appl ...

  3. 黑马程序员:Java编程_String

    =========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 描述字符串对象的类是java.lang.String,String类是不可变(f ...

  4. Interproscan, xml文件转化为tsv

    将interproscan的结果转化格式 很奇怪 tsv格式里没有go, kegg, inter-domain信息,但是xml文件里面却有,tsv文件比较好处理,所以先将xml文件转化为tsv.用软件 ...

  5. JS 点击复制Copy (share)

    分享自:http://www.cnblogs.com/athens/archive/2013/01/16/2862981.html 1.实现点击按钮,复制文本框中的的内容 1 <script t ...

  6. Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)

    什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定).      这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户 ...

  7. 事务的ACID特性

    事务(Transaction)是并发控制的基本单位.    所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转帐工作:从一个帐号扣款并使另一个帐号增 ...

  8. poj3114 强连通+最短路

    题意:有 n 个城市,城市之间能够通过邮件或者电子邮件传递消息,已知 m 条邮件线路,每条线路代表 A 能送邮件到 B,并且花费 V 时间,如果几个城市之间能够相互邮件送达,那么他们就在同一个国家内, ...

  9. vb6 关闭显示器

    Option Explicit Private Const WM_SYSCOMMAND As Long = &H112 Private Const SC_MONITORPOWER As Lon ...

  10. weave

    Docker的原生网络支持非常有限,且没有跨主机的集群网络方案.目前实现Docker网络的开源方案有Weave.Kubernetes.Flannel.Pipework以及SocketPlane等,其中 ...