Handling WM_MOVING in Windows Forms

Just a quick snippet:

public class FormWithWmMoving :
    Form
{
    private const int WM_MOVING = 0x0216;

    private static readonly object EVENT_MOVING = new object();

    public event EventHandler Moving
    {
        add { Events.AddHandler(EVENT_MOVING, value); }
        remove { Events.RemoveHandler(EVENT_MOVING, value); }
    }

    public class MovingEventArgs : EventArgs
    {
        private readonly Rectangle _rectangle;

        public MovingEventArgs(
            Rectangle rectangle)
        {
            _rectangle = rectangle;
        }

        public Rectangle Rectangle
        {
            get { return _rectangle; }
        }
    }

    protected virtual void OnMoving(MovingEventArgs e)
    {
        var h = (EventHandler)Events[EVENT_MOVING];
        if (h != null)
        {
            h(this, e);
        }
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_MOVING)
        {
            var r = (Win32NativeMethods.RECT)Marshal.PtrToStructure(m.LParam, typeof(Win32NativeMethods.RECT));
            var rectangle = new Rectangle(r.left, r.top, r.Bounds.Width, r.Bounds.Height);

            var args = new MovingEventArgs(rectangle);
            OnMoving(args);
        }

        base.WndProc(ref m);
    }
}

Hope this is helpful someday to me or others.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.