[转]C# winForm 自定义鼠标样式的两种方法
生活随笔
收集整理的這篇文章主要介紹了
[转]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 自定义鼠标样式的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外部编辑Infopath的表单模板(xs
- 下一篇: Asp.net中执行.EXE程序的方法