我无法找到如何将光标更改为"指针"或悬停图像时调用的任何内容.
我尝试使用MouseOver,但我无法让它工作.这是我目前的代码;
private void image_Phone_MouseOver(object sender, EventArgs e)
{
Cursor.Current = Cursors.Hand;
}
Run Code Online (Sandbox Code Playgroud)
但是,光标不会改变.
这是一种在实际的 位置上更改光标的方法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请注意,当更改Image或SizeMode或 时,Size您将需要重新计算PictureBox。
小智 6
对于任何 PowerShell/Windows 窗体程序员:
您基本上可以将其用于表单中的每个元素:
$pictureBox1.Add_MouseHover({ $this.Cursor = "Hand" })
Run Code Online (Sandbox Code Playgroud)