如果子控件图形控件已经在滚动,请阻止父容器通过鼠标滚轮滚动

问题描述

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

我在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的问题,我已经使用其他(专有)图形控件进行了测试,并且其行为都相同。

解决方法

我不确定是否有更好的方法可以解决此问题,但是这是一个骇人的解决方法,可以阻止WM_MOUSEWHEEL消息到达控件并仍然执行缩放逻辑。

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

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,pos.X,pos.Y,delta);
            this.OnMouseWheel(args);
            return true;
        }
        return false;
    }
}

然后,使用MyGMapControl代替GMapControl

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


如注释中的Jimi所示,您可以覆盖控件的父容器的WndProc() ,检查WM_MOUSEWHEEL,然后返回鼠标光标是否为在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);
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...