我不知道如何在鼠标悬停在图像上时将光标更改为“指针”或其他名称。
我尝试使用MouseOver,但无法使其工作。这是我的当前代码;
private void image_Phone_MouseOver(object sender, EventArgs e)
{
Cursor.Current = Cursors.Hand;
}
然而,光标并没有改变。
图像
上时更改光标的方法:
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);
}
}
Image
、SizeMode
或Size
时,您需要重新计算PictureBox
的ImgArea
。对于任何 PowerShell/Windows Forms 程序员:
您可以将此用于表单中的基本每个元素:
$pictureBox1.Add_MouseHover({ $this.Cursor = "Hand" })
不要使用Cursor.Current,而是使用image_Phone.Cursor = Cursors.Hand;。
然后等待鼠标松开事件并设置默认光标
Application.DoEvents();
。否则消息循环将被阻塞...使用此方法将会传送消息... 或者尝试image_phone.Cursor = Cursors.Hand;
。 - TrevorDoEvents
还是实际设置 picturebox 的光标? - Trevor