0

I have a program which has a FlowLayoutPanel inside its Form. I'm coding on the FlowLayoutPanel's MouseMove event and everything works fine; except that it doesn't fire the MouseMove event while the cursor moves on its scroll bar.

I searched the web and found the following approach which uses a derived class from FlowLayoutPanel and then overrides its WndProc method:

    class FlowLayoutPanelEx : FlowLayoutPanel
    {
        const int WM_NCMOUSEMOVE = 0x00A0;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCMOUSEMOVE)
            {
                base.OnMouseMove(null);
            }

            base.WndProc(ref m);
        }
    }

I'm having problem with raising (or calling) the base.OnMouseMove(). It requires a MouseEventArgs object to be passed in as parameter. I need to pass the correct MouseEventArgs because my event handler relies on it, but I don't know how to set/where to get the right properties (Buttons, X, Y, etc.) to pass the correct MouseMoveEvent.

Any help will be appreciated.

Ramtin Soltani
  • 2,650
  • 3
  • 24
  • 45

1 Answers1

1

You can detect the mouse moving across the scrollbar by intercepting the WM_NCMOUSEMOVE message. Like this:

protected override void WndProc(ref Message m) {
    if (m.Msg == 0xA0) {  // WM_NCMOUSEMOVE.
        var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
        var evt = new MouseEventArgs(Control.MouseButtons, 0, pos.X, pos.Y, 0);
        OnMouseMove(evt);
    }
    base.WndProc(ref m);
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Alex
  • 13,024
  • 33
  • 62
  • There are two problems with the code you provided: First, the X and Y values are positions of the pointer on the whole screen and not on user control. I need the coordinate of the pointer on user control only, and I can't do operations on the X and Y passed in the event handler. Second, I need the MouseButtons property. BTW, what does OnNcMouseMove do? – Ramtin Soltani Apr 21 '15 at 03:49
  • Updates with translation of screen to window coords. OnNcMouseMove would be the normal recipient of this message. – Alex Apr 21 '15 at 04:25
  • there is no OnNcMouseMove method! Both OnNcMouseMove and base.OnNcMouseMove results in these errors: "'System.Windows.Forms.FlowLayoutPanel' does not contain a definition for 'OnNcMouseMove'", "The name 'OnNcMouseMove' does not exist in the current context". – Ramtin Soltani Apr 22 '15 at 07:37