这里的答案对我不起作用。我在一个可滚动的窗格中有图片框,为了正确运行,我还需要做更多的工作。
您需要做的是覆盖OnMouseWheel()
表单中的函数。在那里你得到了滚轮事件,你必须检查鼠标是否在图片框内。但这还不够。想象一下,您在一个仅显示一小部分图像的可滚动窗格中显示 5000 x 5000 像素的图像。然后您还必须检查鼠标是否在窗格及其所有父项上。下面的代码与pictureBox 的任何父控件的滚动条的滚动位置无关。
/// <summary>
/// This must be overridden in the Form because the pictureBox never receives MouseWheel messages
/// </summary>
protected override void OnMouseWheel(MouseEventArgs e)
{
// Do not use MouseEventArgs.X, Y because they are relative!
Point pt_MouseAbs = Control.MousePosition;
Control i_Ctrl = pictureBox;
do
{
Rectangle r_Ctrl = i_Ctrl.RectangleToScreen(i_Ctrl.ClientRectangle);
if (!r_Ctrl.Contains(pt_MouseAbs))
{
base.OnMouseWheel(e);
return; // mouse position is outside the picturebox or it's parents
}
i_Ctrl = i_Ctrl.Parent;
}
while (i_Ctrl != null && i_Ctrl != this);
// here you have the mouse position relative to the pictureBox if you need it
Point pt_MouseRel = pictureBox.PointToClient(pt_MouseAbs);
// Do your work here
....
}