从Panel中的PictureBox获取特定的图像位置

时间:2014-07-14 14:01:31

标签: c#

我在c#中的Panel中有一个PixtureBox,当我点击某个图像位置时,我正试图获取图像位置。因此,当鼠标悬停在图像上时,我正在使用鼠标位置来获取x和y。如果图像大小小于面板的大小(没有滑动条),这可以正常工作。一旦我得到的图像大于面板尺寸,我就会得到滑动条。但是,如果我向下滑动滑块,我会得到相同的位置。是否有更好的方法来获得具有或不具有滑动条的特定图像位置?任何帮助深表感谢 :) enter image description here

1 个答案:

答案 0 :(得分:2)

Mouse_Hover事件具有默认的EventArgs,因此在调用事件时它不会发送当前鼠标位置。 Mouse_Move event确实提供了the mouse location relative to the scroll position。如果您将PictureBox中的Mouse_Move事件中收到的位置存储在局部变量中,则Hover事件可以使用该位置。

该位置对于控件的原点是绝对的,因此如果图片框在面板内滚动则无关紧要。

Point last;  // hold the last mousemove location

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    // store the location in last
    last.X = e.Location.X;
    last.Y = e.Location.Y;
}

private void pictureBox1_MouseHover(object sender, EventArgs e)
{
    // do whatever we want to do with the last location
    Trace.WriteLine(last);
}