zoukankan      html  css  js  c++  java
  • [转]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(
    0000));
                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(00));
            }
  • 相关阅读:
    C#图片无损压缩
    as3.0 动态文本属性大全
    卡​马​克​卷​轴​算​法​研​究​_​地​图​双​缓​冲
    春卷活动心得
    移动端videojs视频插件使用直播流rtmp、hls、http-flv的注意事项
    在Windows2008系统中利用IIS建立FTP服务器
    winform 窗体自适应 根据新窗体大小按比例放缩
    HTTPS抓包
    数据库 事物 锁
    sql 事物 锁 快照(转发的,写的非常好)
  • 原文地址:https://www.cnblogs.com/freeliver54/p/1338921.html
Copyright © 2011-2022 走看看