自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# winForm自定義鼠標(biāo)樣式的兩種方法

開發(fā) 后端
鼠標(biāo)樣式需要通過API來自定義。本文介紹了C#自定義鼠標(biāo)樣式的兩種方法,可以避免自定義鼠標(biāo)變成單色。

以前試過在C# WinForm中自定義鼠標(biāo)樣式,結(jié)果顯示出來的鼠標(biāo)變成單色。

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

首先引入兩個(gè)命名空間

  1. using System.Runtime.InteropServices;  
  2. using System.Reflection; 

C# winForm自定義鼠標(biāo)樣式方法一

導(dǎo)入API

  1. [DllImport("user32.dll")]  
  2. ublic static extern IntPtr LoadCursorFromFile(string fileName);  
  3.  
  4. [DllImport("user32.dll")]  
  5. ublic static extern IntPtr SetCursor(IntPtr cursorHandle);  
  6.  
  7. [DllImport("user32.dll")]  
  8. ublic static extern uint DestroyCursor(IntPtr cursorHandle);  

接下來使用自己的鼠標(biāo)樣式 

  1. private void Form1_Load(object sender, EventArgs e)  
  2.   {  
  3.       Cursor myCursor = new Cursor(Cursor.Current.Handle);  
  4.       IntPtr colorCursorHandle = LoadCursorFromFile("my.cur");//鼠標(biāo)圖標(biāo)路徑  
  5.         myCursor.GetType().InvokeMember("handle", BindingFlags.Public |  
  6.       BindingFlags.NonPublic | BindingFlags.Instance |  
  7.       BindingFlags.SetField, null, myCursor,  
  8.      new object[] { colorCursorHandle });  
  9.      this.Cursor = myCursor;  
  10.   }  

C# winForm自定義鼠標(biāo)樣式方法之二

現(xiàn)在介紹另一種不用API方式的,鼠標(biāo)樣式只需要一張背景透明的圖片就行了,png或gif格式的

寫個(gè)方法 

  1. public void SetCursor(Bitmap cursor, Point hotPoint)  
  2.   {  
  3.      int hotX = hotPoint.X;  
  4.      int hotY = hotPoint.Y;  
  5.       Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);  
  6.       Graphics g = Graphics.FromImage(myNewCursor);  
  7.       g.Clear(Color.FromArgb(0, 0, 0, 0));  
  8.       g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,   
  9.       cursor.Height);  
  10.  
  11.      this.Cursor = new Cursor(myNewCursor.GetHicon());  
  12.       
  13.       g.Dispose();  
  14.       myNewCursor.Dispose();  
  15.   }  

在你想要改變鼠標(biāo)樣式的事件里頭使用這個(gè)方法就行了

  1. private void Form1_Load(object sender, EventArgs e)  
  2.  {  
  3.      Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");  
  4.      SetCursor(a, new Point(0, 0));  
  5.  }       //this.btnBack.FlatStyle = FlatStyle.Flat;    //set the button no frame  
  6. //this.btnBack.FlatAppearance.BorderSize = 0;  

C# winForm自定義鼠標(biāo)樣式舉例:

Test.cs

  1. using System;   
  2. using System.Drawing;   
  3. using System.Windows.Forms;   
  4. using System.Runtime.InteropServices;   
  5. using System.Reflection;   
  6.  
  7. namespace ColorCursor   
  8. {   
  9. /// < summary>   
  10. /// 本例子的作用:   
  11. /// 在.NET中實(shí)現(xiàn)彩色光標(biāo),動(dòng)畫光標(biāo)和自定義光標(biāo)。   
  12. /// < /summary>   
  13. public class Form1 : System.Windows.Forms.Form   
  14. {   
  15. [DllImport("user32.dll")]   
  16. public static extern IntPtr LoadCursorFromFile( string fileName );   
  17.  
  18. [DllImport("user32.dll")]   
  19. public static extern IntPtr SetCursor( IntPtr cursorHandle );   
  20.  
  21. [DllImport("user32.dll")]   
  22. public static extern uint DestroyCursor( IntPtr cursorHandle );   
  23.  
  24.  
  25. [STAThread]   
  26. static void Main()   
  27. {   
  28. Application.Run(new Form1());   
  29. }   
  30.  
  31. public Form1()   
  32. {   
  33. this.Text = "歡迎光臨【孟憲會(huì)之精彩世界】:http://dotnet.aspx.cc/";   
  34. Cursor myCursor = new Cursor(Cursor.Current.Handle);   
  35. //dinosau2.ani為windows自帶的光標(biāo):   
  36. IntPtr colorCursorHandle = LoadCursorFromFile(@"C:\WINNT\Cursors\dinosau2.ani" );   
  37. myCursor.GetType().InvokeMember("handle",BindingFlags.Public |   
  38. BindingFlags.NonPublic | BindingFlags.Instance |   
  39. BindingFlags.SetField,null,myCursor,   
  40. new object [] { colorCursorHandle } );   
  41. this.Cursor = myCursor;   
  42. }   
  43. }   
  44. }     

 

【編輯推薦】

  1. C#多線程控制進(jìn)度條之長(zhǎng)任務(wù)操作
  2. C#多線程控制進(jìn)度條之長(zhǎng)異步操作
  3. C#多線程控制進(jìn)度條之異步調(diào)用
  4. C#多線程控制進(jìn)度條之多線程安全
  5. C# listview進(jìn)度條顯示淺析
責(zé)任編輯:book05 來源: hi.baidu
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)