`
zhuojb
  • 浏览: 90047 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

自定义鼠标样式的做法

阅读更多

调用外部鼠标样式,一般情况有两种,一种是调用鼠标文件cur、ani文件(cur是静态光标文件,ani是动画光标文件)。

做法,自定义光标文件嵌入dll和调用的方法。把鼠标样式cur文件添加入资源文件(这晓得吧,就是打开项目的property,点击资源,ctrl+v进去),记住在Resources里面选择cur文件,点击属性,修改生成操作为嵌入的资源。(这样做事为了控件封装时候一起封装,如果你是在from中用,那当我没说)

 

代码如下:

                    this.Cursor = new Cursor(GetType(), "Resources.hmove.cur");

 

记住GetType()是为了获得Resources在哪个项目下,如果报错,请检查名字是否正确?是否选定资源为嵌入的资源?

      另外一种是直接把png、jpg图片做为鼠标光标。

 

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);

 

            IntPtr iptr = myNewCursor.GetHicon();

            this.FindForm().Cursor = new Cursor(iptr);

 

            g.Dispose();

            myNewCursor.Dispose();

            cursor.Dispose();

}

 

 

 

调用方法 Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");  

       SetCursor(a, new Point(0, 0));


第二种可以向第一种转换,使用ArtCursors鼠标编辑器,不会用也没关系,只需要导入png,导出cur就成。因为我验证了第二种在调用多次的时候,会出现“GDI+一般性错误”的报错提示。所以最好是调用一次储存Cursor变量是最好的解决办法。切忌反复调用~

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics