winforms 如何在C#中改变光标悬停

l2osamch  于 8个月前  发布在  C#
关注(0)|答案(6)|浏览(71)

我不知道如何在悬停图像时将光标更改为“指针”或其他名称。
我试过使用MouseOver,但我不能让它工作。下面是我的当前代码:

private void image_Phone_MouseOver(object sender, EventArgs e)
{
    Cursor.Current = Cursors.Hand;
}

但是,光标不会改变。

wh6knrhe

wh6knrhe1#

在控件属性窗口中设置适当的光标。
这里是一个设置“手”光标为picturebox的例子。

svmlkihl

svmlkihl2#

这是一种在实际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);
    }
}

请注意,在更改PictureBoxImageSizeModeSize时,您需要重新计算ImageArea

dgsult0t

dgsult0t3#

对于任何PowerShell/Windows窗体程序员:
你可以对表单中的每个元素使用这个:

$pictureBox1.Add_MouseHover({ $this.Cursor = "Hand" })
hgc7kmma

hgc7kmma4#

当前使用image_Phone.Cursor = Cursors.Hand;

7bsow1i6

7bsow1i65#

在WinForms中(由标记做出的假设)-PictureBox控件上有一个Cursor属性.(实际上是在Control上)尝试设置这个?

jhkqcmku

jhkqcmku6#

1.您可以使用鼠标按下事件更改光标
1.比用鼠标移动事件检查位置是否与图像相同

  • 而不是等待鼠标抬起事件并设置默认光标
  • 或者只是设置游标的属性

相关问题