解決C# winForm自定義鼠標(biāo)樣式的兩種實現(xiàn)方法詳解
更新時間:2013年05月20日 16:44:16 作者:
本篇文章是對在C#中winForm自定義鼠標(biāo)樣式的兩種實現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
第一種:(調(diào)用系統(tǒng)API)
首先引入兩個命名空間
using System.Runtime.InteropServices;
using System.Reflection;
導(dǎo)入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);
接下來使用自己的鼠標(biāo)樣式
private void Form1_Load(object sender, EventArgs e)
{
Cursor myCursor = new Cursor(Cursor.Current.Handle);
IntPtr colorCursorHandle = LoadCursorFromFile("my.cur");//鼠標(biāo)圖標(biāo)路徑
myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.SetField, null, myCursor,
new object[] { colorCursorHandle });
this.Cursor = myCursor;
}
第二種:(不用API方式的,鼠標(biāo)樣式只需要一張背景透明的圖片就行了,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();
}
在你想要改變鼠標(biāo)樣式的事件里頭使用這個方法就行了,如:
private void Form1_Load(object sender, EventArgs e)
{
Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
SetCursor(a, new Point(0, 0));
}
首先引入兩個命名空間
復(fù)制代碼 代碼如下:
using System.Runtime.InteropServices;
using System.Reflection;
導(dǎo)入API
復(fù)制代碼 代碼如下:
[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);
接下來使用自己的鼠標(biāo)樣式
復(fù)制代碼 代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
Cursor myCursor = new Cursor(Cursor.Current.Handle);
IntPtr colorCursorHandle = LoadCursorFromFile("my.cur");//鼠標(biāo)圖標(biāo)路徑
myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.SetField, null, myCursor,
new object[] { colorCursorHandle });
this.Cursor = myCursor;
}
第二種:(不用API方式的,鼠標(biāo)樣式只需要一張背景透明的圖片就行了,png或gif格式的)
復(fù)制代碼 代碼如下:
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();
}
在你想要改變鼠標(biāo)樣式的事件里頭使用這個方法就行了,如:
復(fù)制代碼 代碼如下:
private void Form1_Load(object sender, EventArgs e)
{
Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
SetCursor(a, new Point(0, 0));
}
您可能感興趣的文章:
- c# Winform自定義控件-儀表盤功能
- winform實現(xiàn)可拖動的自定義Label控件
- C#中Winform 實現(xiàn)Ajax效果自定義按鈕
- C# winform自定義翻頁控件詳解
- WinForm實現(xiàn)自定義右下角提示效果的方法
- C# Winform使用擴(kuò)展方法實現(xiàn)自定義富文本框(RichTextBox)字體顏色
- C# WinForm中實現(xiàn)快捷鍵自定義設(shè)置實例
- WinForm自定義函數(shù)FindControl實現(xiàn)按名稱查找控件
- WinForm自定義控件應(yīng)用實例
- .Net WInform開發(fā)筆記(三)談?wù)勛灾瓶丶?自定義控件)
- Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片
相關(guān)文章
c#動態(tài)調(diào)用Webservice的兩種方法實例
這篇文章介紹了c#動態(tài)調(diào)用Webservice的兩種方法實例,有需要的朋友可以參考一下2013-08-08基于C#調(diào)用c++Dll結(jié)構(gòu)體數(shù)組指針的問題詳解
下面小編就為大家分享一篇基于C#調(diào)用c++Dll結(jié)構(gòu)體數(shù)組指針的問題詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12WPF利用ValueConverter實現(xiàn)值轉(zhuǎn)換器
值轉(zhuǎn)換器在WPF開發(fā)中是非常常見的,值轉(zhuǎn)換器可以幫助我們很輕松地實現(xiàn),界面數(shù)據(jù)展示的問題。本文將通過WPF?ValueConverter實現(xiàn)簡單的值轉(zhuǎn)換器,希望對大家有所幫助2023-03-03C#中的IEnumerable簡介及簡單實現(xiàn)實例
這篇文章主要介紹了C#中的IEnumerable簡介及簡單實現(xiàn)實例,本文講解了IEnumerable一些知識并給出了一個簡單的實現(xiàn),需要的朋友可以參考下2015-03-03C#制作多線程處理強(qiáng)化版網(wǎng)絡(luò)爬蟲
這篇文章主要介紹了C#制作多線程處理強(qiáng)化版網(wǎng)絡(luò)爬蟲的相關(guān)代碼,有想學(xué)習(xí)C#多線程編程的小伙伴可以參考下2016-09-09