您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。
- 内容介绍
- 相关推荐
本文共计1051个文字,预计阅读时间需要5分钟。
有時,一個自定義的鼠標光標能夠為你的程序增色不少。本文將介紹如何在 .NET 桌面程序中自定義鼠標光標。
.NET 桌面程序主要分為 WinForms 和 WPF 兩種。以下分別介紹:
WinForms
在 WinForms 中,你可以通過以下步驟自定義鼠標光標:
1. 將你的圖片賦值給 `Cursor` 屬性。
2.在窗體的 `Load` 事件中設置光標。
csharp
public MyForm(){ InitializeComponent(); this.Cursor=new Cursor(your_cursor.png);}private void MyForm_Load(object sender, EventArgs e){ this.Cursor=new Cursor(your_cursor.png);}
WPF
在 WPF 中,你可以使用以下方法自定義鼠標光標:
1. 在 XAML 中設置 `Cursor` 屬性。
2.如果需要動態改變,可以在 C# 代碼中修改。
xaml
或者 C# 代碼:
csharppublic MyWindow(){ InitializeComponent(); this.Cursor=new ImageCursor(your_cursor.png, new Point(10, 10));}
這些方法可以幫助你在 .NET 桌面程序中自定義鼠標光標,為你的程序增添一絲個性。
有的时候,一个自定义的鼠标光标能给你的程序增色不少。本文这里介绍一下如何在.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; }
然后通过GetIconInfoandCreateIconIndirect两个函数来合成光标。完整代码如下:
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 = 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作为自绘制的光标的,得益于WPF的强大绘图功能,是可以非常容易的绘制漂亮的光标的。测试代码如下:
this.Cursor= CursorHelper.CreateCursor(new UserControl1());
到此这篇关于C#对桌面应用程序自定义鼠标光标的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计1051个文字,预计阅读时间需要5分钟。
有時,一個自定義的鼠標光標能夠為你的程序增色不少。本文將介紹如何在 .NET 桌面程序中自定義鼠標光標。
.NET 桌面程序主要分為 WinForms 和 WPF 兩種。以下分別介紹:
WinForms
在 WinForms 中,你可以通過以下步驟自定義鼠標光標:
1. 將你的圖片賦值給 `Cursor` 屬性。
2.在窗體的 `Load` 事件中設置光標。
csharp
public MyForm(){ InitializeComponent(); this.Cursor=new Cursor(your_cursor.png);}private void MyForm_Load(object sender, EventArgs e){ this.Cursor=new Cursor(your_cursor.png);}
WPF
在 WPF 中,你可以使用以下方法自定義鼠標光標:
1. 在 XAML 中設置 `Cursor` 屬性。
2.如果需要動態改變,可以在 C# 代碼中修改。
xaml
或者 C# 代碼:
csharppublic MyWindow(){ InitializeComponent(); this.Cursor=new ImageCursor(your_cursor.png, new Point(10, 10));}
這些方法可以幫助你在 .NET 桌面程序中自定義鼠標光標,為你的程序增添一絲個性。
有的时候,一个自定义的鼠标光标能给你的程序增色不少。本文这里介绍一下如何在.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; }
然后通过GetIconInfoandCreateIconIndirect两个函数来合成光标。完整代码如下:
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 = 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作为自绘制的光标的,得益于WPF的强大绘图功能,是可以非常容易的绘制漂亮的光标的。测试代码如下:
this.Cursor= CursorHelper.CreateCursor(new UserControl1());
到此这篇关于C#对桌面应用程序自定义鼠标光标的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。

