如何在C#中悬停时更改光标

Chr*_*ler 3 c# winforms

我无法找到如何将光标更改为"指针"或悬停图像时调用的任何内容.

我尝试使用MouseOver,但我无法让它工作.这是我目前的代码;

private void image_Phone_MouseOver(object sender, EventArgs e)
{
    Cursor.Current = Cursors.Hand;
}
Run Code Online (Sandbox Code Playgroud)

但是,光标不会改变.

rpe*_*kov 11

在控件属性窗口中设置适当的光标.

这是为picturebox设置"Hand"光标的示例.

在此输入图像描述


TaW*_*TaW 8

这是一种在实际的 位置上更改光标的方法Image

在此输入图像描述

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    pictureBox1.Cursor = ImageArea(pictureBox1).Contains(e.Location) ?
                                                Cursors.Hand : Cursors.Default;
}

Rectangle ImageArea(PictureBox pbox)
{
    Size si = pbox.Image.Size;
    Size sp = pbox.ClientSize;
    float ri = 1f * si.Width / si.Height;
    float rp = 1f * sp.Width / sp.Height;
    if (rp > ri)
    {
        int width = si.Width * sp.Height / si.Height;
        int left = (sp.Width - width) / 2;
        return new Rectangle(left, 0, width, sp.Height);
    }
    else
    {
        int height = si.Height * sp.Width / si.Width;
        int top = (sp.Height - height) / 2;
        return new Rectangle(0, top, sp.Width, height);
    }
}
Run Code Online (Sandbox Code Playgroud)

ImageArea请注意,当更改ImageSizeMode或 时,Size您将需要重新计算PictureBox


小智 6

对于任何 PowerShell/Windows 窗体程序员:

您基本上可以将其用于表单中的每个元素:

$pictureBox1.Add_MouseHover({ $this.Cursor = "Hand" })
Run Code Online (Sandbox Code Playgroud)