Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片
首先,建立圖片與鼠標(biāo)的對(duì)應(yīng)關(guān)系。
class MouseStyle { [DllImport("user32.dll")] public static extern IntPtr SetCursor(IntPtr cursorHandle); static MouseStyle() { InitMouseStyle(); } private static void InitMouseStyle() { if (Hand == null) { Hand = SetCursor("Image//Hand.png"); } if (Arrow == null) { Arrow = SetCursor("Image//Arrow.png"); } } /// <summary> /// 鼠標(biāo)手型樣式 /// </summary> public static Cursor Hand = null; /// <summary> /// 鼠標(biāo)指針樣式 /// </summary> public static Cursor Arrow = null; /// <summary> /// 設(shè)置鼠標(biāo)樣式 /// </summary> /// <param name="fileName">自定義的鼠標(biāo)樣式文件</param> /// <returns>鼠標(biāo)樣式</returns> private static Cursor SetCursor(string fileName) { //文件的絕對(duì)路徑,在debug下 var path = System.IO.Path.GetFullPath(fileName) ; //畫圖 Bitmap bit = (Bitmap)Bitmap.FromFile(path, true); Bitmap myNewCursor = new Bitmap(bit.Width, bit.Height); Graphics g = Graphics.FromImage(myNewCursor); g.Clear(Color.FromArgb(0, 0, 0, 0)); //箭頭和手型有點(diǎn)不一樣 if (System.IO.Path.GetFileName(fileName).Equals("Hand.png")) { g.DrawImage(bit, bit.Width / 2 - 15, bit.Height / 2, bit.Width / 2, bit.Height / 2); } else { g.DrawImage(bit, bit.Width / 2 - 15, bit.Height / 2, bit.Width / 2, bit.Height / 2); } Cursor cursor; //獲取圖片的句柄 try { cursor = new Cursor(myNewCursor.GetHicon()); } catch { cursor = new Cursor(Icon.FromHandle(myNewCursor.GetHicon()).Handle); } //釋放資源 g.Dispose(); return cursor; } }
如果是鼠標(biāo)文件.cur結(jié)尾,可以直接使用。
法1、在每一個(gè)窗體中單獨(dú)修改其中的鼠標(biāo)外觀,這樣鼠標(biāo)離開(kāi)自己的程序后,也會(huì)恢復(fù)到系統(tǒng)默認(rèn)的鼠標(biāo)樣式。
在上述類中,添加代碼:
/// <summary> /// 設(shè)置鼠標(biāo)樣式 /// </summary> /// <param name="col">控件</param> public static void SetMouseStyle(Control col) { InitMouseStyle(); //設(shè)置窗體鼠標(biāo)為箭頭 if (col is Form) { ((Form)col).Cursor = Arrow; } //遍歷控件,如果控件是箭頭或默認(rèn),就改為自定義的箭頭 //如果是手型就改為自定義的手型 foreach (Control con in col.Controls) { if (con.Cursor == Cursors.Hand) { con.Cursor = Hand; } if (con.Cursor == Cursors.Arrow || con.Cursor == Cursors.Default) { con.Cursor = Arrow; } //遞歸遍歷 SetMouseStyle((Control)con); } }
然后在所有窗體中,均調(diào)用SetMouseStyle方法,傳入窗體自身
法2、修改系統(tǒng)鼠標(biāo),待程序退出時(shí),還原系統(tǒng)鼠標(biāo)。首先添加代碼,調(diào)用window的API:
[DllImport("User32.DLL")] public static extern bool SetSystemCursor(IntPtr hcur, uint id); public const uint OCR_NORMAL = 32512; public const uint OCR_HAND = 32649; public const uint OCR_IBEAM = 32513; //速查 https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setsystemcursor?redirectedfrom=MSDN //OCR_APPSTARTING:標(biāo)準(zhǔn)箭頭和小的沙漏;32650 //OCR_NORMAL:標(biāo)準(zhǔn)箭頭 32512 //OCR_CROSS:交叉十字線光標(biāo): 32515 //OCR_HAND:手的形狀(WindowsNT5.0和以后版本) 32649 //OCR_HELP:箭頭和向東標(biāo)記; 32651 //OCR_IBEAM:I形梁; 32513 //OCR_NO:斜的圓 32648 //OCR_SIZEALL:四個(gè)方位的箭頭分別指向北、南、東、西 32646 //OCR_SIZENESEW:雙箭頭分別指向東北和西南; 32643 //OCR_SIZENS:雙箭頭,分別指向北和南 32645 //OCR_SIZENWSE:雙箭頭分別指向西北和東南; 32642 //OCR_SIZEWE:雙箭頭分別指向西和東 32644 //OCR_UP:垂直箭頭: 32516 //OCR_WAIT:32514 沙漏返回值:如果成功,返回非零值;如果失敗,返回值為零。 [DllImport("User32.DLL")] public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni); public const uint SPI_SETCURSORS = 87; public const uint SPIF_SENDWININICHANGE = 2;
程序啟動(dòng)和退出時(shí)分別調(diào)用設(shè)置方法和恢復(fù)方法:
private void button1_Click(object sender, EventArgs e) { //設(shè)置 SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_NORMAL); SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_IBEAM); //..可以根據(jù)情況加 } private void button2_Click(object sender, EventArgs e) { //恢復(fù) SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, SPIF_SENDWININICHANGE); }
以上就是Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片的詳細(xì)內(nèi)容,更多關(guān)于Winform 自定義鼠標(biāo)圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C# Winform下載文件并顯示進(jìn)度條的實(shí)現(xiàn)代碼
- WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法
- winform 實(shí)現(xiàn)控制輸入法
- C#中winform控制textbox輸入只能為數(shù)字的方法
- 在Winform動(dòng)態(tài)啟動(dòng)、控制臺(tái)命令行的方法
- C# WinForm-Timer控件的使用
- C# Winform實(shí)現(xiàn)波浪滾動(dòng)效果
- C# Winform中如何繪制動(dòng)畫示例詳解
- C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程(附源碼)
- visual studio 2019使用net core3.0創(chuàng)建winform無(wú)法使用窗體設(shè)計(jì)器
- Winform 實(shí)現(xiàn)進(jìn)度條彈窗和任務(wù)控制
相關(guān)文章
Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例
這篇文章主要為大家介紹了Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法,可實(shí)現(xiàn)系統(tǒng)服務(wù)的啟動(dòng)和停止功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04