国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

解決C# winForm自定義鼠標(biāo)樣式的兩種實現(xiàn)方法詳解

2020-01-24 03:16:50
字體:
供稿:網(wǎng)友
第一種:(調(diào)用系統(tǒng)API)
首先引入兩個命名空間
復(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));
        }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广灵县| 延庆县| 读书| 得荣县| 浠水县| 申扎县| 宣汉县| 屏东县| 长乐市| 平邑县| 江陵县| 碌曲县| 苗栗县| 两当县| 盐边县| 九江县| 拉孜县| 大邑县| 清原| 安溪县| 新巴尔虎左旗| 崇左市| 宁武县| 遂川县| 蓝山县| 柳林县| 海宁市| 澄城县| 海伦市| 乌审旗| 竹北市| 温宿县| 雅江县| 商河县| 阜城县| 兴山县| 封开县| 乐安县| 本溪市| 交城县| 墨竹工卡县|