如何自定義Winform應(yīng)用程序的鼠標(biāo)圖片?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
首先,建立圖片與鼠標(biāo)的對應(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) { //文件的絕對路徑,在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)); //箭頭和手型有點不一樣 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; } }
新聞名稱:如何自定義Winform應(yīng)用程序的鼠標(biāo)圖片-創(chuàng)新互聯(lián)
路徑分享:http://sd-ha.com/article14/jjgge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、App設(shè)計、電子商務(wù)、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容