Tom
Tom

Reputation: 577

Disable all mouse messages on control

I have a graph control that plots data points. The data points are plotted as 1 point per pixel. If the number of data points get larger than a certain amount, or the size of the window is increased the performance of the plotting when you move your mouse over the control suffers. If you move quickly the plotting actually stops during the motion.

Is there a way to disable all the messages when the mouse is over that control except for button clicks?

I have not been able to find anything.

Upvotes: 0

Views: 415

Answers (1)

TnTinMn
TnTinMn

Reputation: 11801

Based on your description, I believe that it should be sufficient to filter out MouseMove messages sent to the control. This can be accomplished by having the Form implement IMessageFilter similar to the example presented below. Returning true from IMessageFilter.PreFilterMessage prevents the message from being sent to the control (a panel in the example). A registered filter is active application wide, so it is added/removed when the form actives/deactivates.

public partial class Form1 : Form, IMessageFilter
{
    private Panel pnl;

    public Form1()
    {
        InitializeComponent();
        pnl = new Panel { Size = new Size(200, 200), Location = new Point(20, 20), BackColor = Color.Aqua };
        Controls.Add(pnl);
        pnl.Click += panel_Click;
        pnl.MouseMove += panel_MouseMove;
        pnl.MouseHover += panel_MouseHover;

    }

    private void panel_MouseHover(sender As Object, e As EventArgs)
    {
        // this should not occur
        throw new NotImplementedException();
    }

    private void panel_MouseMove(object sender, MouseEventArgs e)
    {
        // this should not occur
        throw new NotImplementedException();
    }

    private void panel_Click(object sender, EventArgs e)
    {
        MessageBox.Show("panel clicked");
    }

    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);
        // install message filter when form activates
        Application.AddMessageFilter(this);
    }

    protected override void OnDeactivate(EventArgs e)
    {
        base.OnDeactivate(e);
        // remove message filter when form deactivates
        Application.RemoveMessageFilter(this);
    }

    bool IMessageFilter.PreFilterMessage(ref Message m)
    {
        bool handled = false;
        if (m.HWnd == pnl.Handle && (WM) m.Msg == WM.MOUSEMOVE)
        {
            handled = true;
        }
        return handled;
    }

    public  enum WM : int
    {
        #region Mouse Messages
        MOUSEFIRST = 0x200,
        MOUSEMOVE = 0x200,
        LBUTTONDOWN = 0x201,
        LBUTTONUP = 0x202,
        LBUTTONDBLCLK = 0x203,
        RBUTTONDOWN = 0x204,
        RBUTTONUP = 0x205,
        RBUTTONDBLCLK = 0x206,
        MBUTTONDOWN = 0x207,
        MBUTTONUP = 0x208,
        MBUTTONDBLCLK = 0x209,
        MOUSELAST = 0x209
        #endregion
    }
}

Upvotes: 1

Related Questions