日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

[转]C# winForm 自定义鼠标样式的两种方法

發布時間:2025/3/14 C# 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]C# winForm 自定义鼠标样式的两种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文轉自:http://www.cnblogs.com/hzbzxm/archive/2008/09/15/1291104.html

以前試過在WinForm中自定義鼠標樣式,結果顯示出來的鼠標變成單色。

  后來百度了下,原來要用API來做。

???? 首先引入兩個命名空間?

?

using?System.Runtime.InteropServices;
using?System.Reflection;

?

?導入API?

?

????????[DllImport("user32.dll")]
????????
public?static?extern?IntPtr?LoadCursorFromFile(string?fileName);

????????[DllImport(
"user32.dll")]
????????
public?static?extern?IntPtr?SetCursor(IntPtr?cursorHandle);

????????[DllImport(
"user32.dll")]
????????
public?static?extern?uint?DestroyCursor(IntPtr?cursorHandle);

?

接下來使用自己的鼠標樣式

?

????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????Cursor?myCursor?
=?new?Cursor(Cursor.Current.Handle);
????????????IntPtr?colorCursorHandle?
=?LoadCursorFromFile("my.cur");//鼠標圖標路徑
??????????????myCursor.GetType().InvokeMember("handle",?BindingFlags.Public?|
????????????BindingFlags.NonPublic?
|?BindingFlags.Instance?|
????????????BindingFlags.SetField,?
null,?myCursor,
????????????
new?object[]?{?colorCursorHandle?});
????????????
this.Cursor?=?myCursor;
????????}

?

?

????? 現在介紹另一種不用API方式的,鼠標樣式只需要一張背景透明的圖片就行了,png或gif格式的

???? 寫個方法

?

?

????????public?void?SetCursor(Bitmap?cursor,?Point?hotPoint)
????????{
????????????
int?hotX?=?hotPoint.X;
????????????
int?hotY?=?hotPoint.Y;
????????????Bitmap?myNewCursor?
=?new?Bitmap(cursor.Width?*?2?-?hotX,?cursor.Height?*?2?-?hotY);
????????????Graphics?g?
=?Graphics.FromImage(myNewCursor);
????????????g.Clear(Color.FromArgb(
0,?0,?0,?0));
????????????g.DrawImage(cursor,?cursor.Width?
-?hotX,?cursor.Height?-?hotY,?cursor.Width,?
????????????cursor.Height);

????????????
this.Cursor?=?new?Cursor(myNewCursor.GetHicon());
???????????
????????????g.Dispose();
????????????myNewCursor.Dispose();
????????}

?

?

在你想要改變鼠標樣式的事件里頭使用這個方法就行了?

?

????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????Bitmap?a
=(Bitmap)Bitmap.FromFile("myCur.png");
????????????SetCursor(a,?
new?Point(0,?0));
????????}

轉載于:https://www.cnblogs.com/freeliver54/archive/2008/11/22/1338921.html

總結

以上是生活随笔為你收集整理的[转]C# winForm 自定义鼠标样式的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。