如何在winform中使用位图替换光标

3

我正在使用C# .net开发一个鼠标移动控制的放大镜应用程序。我需要用放大镜控件(放大镜控件是一个picturebox)来替换光标。请问是否有任何方法可以实现这一点。


2
http://www.switchonthecode.com/tutorials/csharp-tutorial-how-to-use-custom-cursors - David Brabant
很好...我将用这个来实现我的概念。 - DjMalaikallan
2个回答

7
下面的示例代码展示了如何在Windows表单上设置光标。同样的方法也可以用于为控件设置光标。
public class Form_With_A_Cursor_Example {
    public void Shows_A_Form_With_A_Cursor_Loaded_From_A_pictureBox() {         
        Form frm = new Form();
        PictureBox pb = new PictureBox() { Image = Image.FromFile( @"C:\Users\xxx\Pictures\someImage.bmp" ) };

        frm.Cursor = new Cursor( ( (Bitmap)pb.Image ).GetHicon() );

        frm.ShowDialog();
    }
}

3

首先将位图添加到您的项目资源中:
项目->项目名称属性->添加现有文件(从“添加资源”旁边的菜单中)添加您的BMP

Bitmap b = new Bitmap(projectname.Properties.Resources.yourCursorName);
b.MakeTransparent(b.GetPixel(0,0));
Graphics g = Graphics.FromImage(b);
IntPtr ptr = b.GetHicon();
Cursor = new System.Windows.Forms.Cursor(ptr);

其中"projectname"是您项目的名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接