ここでの答えは私にはうまくいきません。スクロール可能なペインにピクチャーボックスがあり、正しく機能するために行う作業はほとんどありません。
あなたがしなければならないことはOnMouseWheel()
、フォームの関数を上書きすることです。そこでホイールイベントが発生し、マウスがピクチャーボックス内にあるかどうかを確認する必要があります。しかし、それだけでは十分ではありません。画像のごく一部のみを表示するスクロール可能なペイン内に5000x5000ピクセルの画像を表示していると想像してください。次に、マウスがペインとそのすべての親の上にあるかどうかも確認する必要があります。以下のコードは、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
....
}