如果子(图形)控件已经在滚动,则防止父容器通过鼠标滚轮滚动

Prevent parent container from scrolling by the mouse wheel if a child (graphic) control is already scrolling

如果子(图形)控件已经获得焦点并且正在滚动,如何防止父容器通过鼠标滚轮滚动?

我在 WinForms 中嵌入了一个 GMap.NET WinForms control。每当通过鼠标滚轮聚焦并滚动该控件时,父窗体也将滚动。当我通过鼠标滚轮滚动 GMap.NET 控件时,我只希望父窗体保持不动。而当我想滚动父窗体时,我总是可以让 GMap.NET 控件失去焦点。 如何做我想做的事?

这是我的代码:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.gMapControl = new GMap.NET.WindowsForms.GMapControl();
        this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
        this.SuspendLayout();
        // 
        // gMapControl
        // 
        this.gMapControl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.gMapControl.Bearing = 0F;
        this.gMapControl.CanDragMap = true;
        this.gMapControl.EmptyTileColor = System.Drawing.Color.Navy;
        this.gMapControl.GrayScaleMode = false;
        this.gMapControl.HelperLineOption = GMap.NET.WindowsForms.HelperLineOptions.DontShow;
        this.gMapControl.LevelsKeepInMemmory = 5;
        this.gMapControl.Location = new System.Drawing.Point(525, 1);
        this.gMapControl.MarkersEnabled = true;
        this.gMapControl.MaxZoom = 2;
        this.gMapControl.MinZoom = 2;
        this.gMapControl.MouseWheelZoomEnabled = true;
        this.gMapControl.MouseWheelZoomType = GMap.NET.MouseWheelZoomType.MousePositionAndCenter;
        this.gMapControl.Name = "gMapControl";
        this.gMapControl.NegativeMode = false;
        this.gMapControl.PolygonsEnabled = true;
        this.gMapControl.RetryLoadTile = 0;
        this.gMapControl.RoutesEnabled = true;
        this.gMapControl.ScaleMode = GMap.NET.WindowsForms.ScaleModes.Integer;
        this.gMapControl.SelectedAreaFillColor = System.Drawing.Color.FromArgb(((int)(((byte)(33)))), ((int)(((byte)(65)))), ((int)(((byte)(105)))), ((int)(((byte)(225)))));
        this.gMapControl.ShowTileGridLines = false;
        this.gMapControl.Size = new System.Drawing.Size(198, 378);
        this.gMapControl.TabIndex = 0;
        this.gMapControl.Zoom = 0D;
        // 
        // flowLayoutPanel1
        // 
        this.flowLayoutPanel1.BackColor = System.Drawing.Color.Black;
        this.flowLayoutPanel1.Location = new System.Drawing.Point(1, 1);
        this.flowLayoutPanel1.Name = "flowLayoutPanel1";
        this.flowLayoutPanel1.Size = new System.Drawing.Size(489, 885);
        this.flowLayoutPanel1.TabIndex = 6;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;
        this.ClientSize = new System.Drawing.Size(745, 533);
        this.Controls.Add(this.flowLayoutPanel1);
        this.Controls.Add(this.gMapControl);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private GMap.NET.WindowsForms.GMapControl gMapControl;
    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        gMapControl.MapProvider = GMapProviders.GoogleTerrainMap;
        gMapControl.MinZoom = 5;
        gMapControl.MaxZoom = 12;
        gMapControl.Zoom = 7.5;
        gMapControl.ShowCenter = true;
        gMapControl.DragButton = MouseButtons.Middle;
    }
}

这肯定不仅仅是 GMap.NET 的问题,我已经用其他(专有)图形控件进行了测试,它的行为完全相同。

我不确定是否有更好的解决方法,但这里有一个 hacky 解决方法来阻止 WM_MOUSEWHEEL 消息到达控件并仍然执行缩放逻辑。

将以下 class 添加到您的项目中:

public class MyGMapControl : GMapControl, IMessageFilter
{
    public MyGMapControl()
    {
        Application.AddMessageFilter(this);
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing) Application.RemoveMessageFilter(this);
        base.Dispose(disposing);
    }
    public bool PreFilterMessage(ref Message m)
    {
        const int WM_MOUSEWHEEL = 0x020A;
        if (m.HWnd == this.Handle && m.Msg == WM_MOUSEWHEEL)
        {
            Point posOnScreen = new Point(m.LParam.ToInt32());
            Point pos = PointToClient(posOnScreen);
            int delta = m.WParam.ToInt32();

            var args = new MouseEventArgs(MouseButtons.None, 0, pos.X, pos.Y, delta);
            this.OnMouseWheel(args);
            return true;
        }
        return false;
    }
}

然后,使用 MyGMapControl 而不是 GMapControl

在这里,我们正在创建一个 MessageFilter 来拦截 WM_MOUSEWHEEL 消息,并在 PreFilterMessage 中创建 return true 来阻止消息到达控制。现在,滚动不会发生,但缩放逻辑也不会发生,因为它是由 WM_MOUSEWHEEL 消息触发的 implemented inside OnMouseWheel()。因此,我们在 returning true 之前手动调用 OnMouseWheel() 方法以确保发生缩放。


正如评论中 Jimi 所建议的那样,您可以覆盖 WndProc() 控件的父容器 ,检查 WM_MOUSEWHEEL,并 return 如果鼠标光标在 GMapControl:

protected override void WndProc(ref Message m)
{
    const int WM_MOUSEWHEEL = 0x020A;
    if (m.Msg == WM_MOUSEWHEEL)
    {
        if (gMapControl.Bounds.Contains(PointToClient(MousePosition))) return;
    }
    base.WndProc(ref m);
}