WndProc WM_NCMOUSEUP no fire

I'm trying to get the Border form (title bar) for a mouse for a bit now and it seems like it's broken (I've found tons of other articles that indicate the same).

I've tested:

Const WM_LBUTTONUP = &H202
Const WM_MBUTTONUP = &H208
Const WM_RBUTTONUP = &H205
Const WM_NCXBUTTONUP = &HAC
Const WM_XBUTTONUP = &H20C
Const WM_MENURBUTTONUP = &H122
Const WM_NCLBUTTONUP = &HA2
Const WM_NCRBUTTONUP = &HA5
Const WM_NCLBUTTONDOWN = &HA1
Const WM_NCMOUSEMOVE = &HA0

      

pretty thorough. I see that while WM_NCLBUTTONUP doesn't work, WM_NCLBUTTONDOWN does, and after release (where I would expect WM_NCLBUTTONUP) I get WM_NCMOUSEMOVE instead. The problem is that you also get WM_NCMOUSEMOVE, as expected, whenever you move your mouse outside of the client area (otherwise it's a form border).

In my first attempt to overcome this, I came up with:

Private MouseIsDown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As Message)
    Const WM_NCLBUTTONDOWN = &HA1
    Const WM_NCMOUSEMOVE = &HA0
    If (m.Msg = WM_NCLBUTTONDOWN) Then
        MouseIsDown = True
        Console.WriteLine("NCLButtonDown")
    ElseIf (m.Msg = WM_NCMOUSEMOVE) Then
        If MouseIsDown Then
            Console.WriteLine("NCMouseMove" + Environment.NewLine + "LParam: " + m.LParam.ToString() + Environment.NewLine + "WParam: " + m.WParam.ToString() + Environment.NewLine + "Res: " + m.Result.ToString())
            MouseIsDown = False
        Else
            Console.WriteLine("Not mouseup")
        End If
    Else
    End If
    MyBase.WndProc(m)
End Sub

      

This seems to work through initial testing, but I'm wondering if this would be appropriate, or if there is a post I just missed (I looked at: http://www.pinvoke.net/default.aspx/Constants.WM pretty closely and can't see anything else that looks right).

+3


source to share


1 answer


You can use NativeWindow or call WndProc to enter a form. Code Translator | VB WindowsMessages

Try the following:



    private NativeWnd _nativeWnd;

    private void Form1_Load(object sender, EventArgs e)
    {
        _nativeWnd = new NativeWnd(this);
    }

    class NativeWnd : NativeWindow, IDisposable
    {
        public NativeWnd(Form owner)
        {
            base.AssignHandle(owner.Handle);
        }

        private enum WindowMessages
        {
            // non client mouse
            WM_NCMOUSEMOVE = 0x00A0,
            WM_NCLBUTTONDOWN = 0x00A1,
            WM_NCLBUTTONUP = 0x00A2,
            WM_NCLBUTTONDBLCLK = 0x00A3,
            WM_NCRBUTTONDOWN = 0x00A4,
            WM_NCRBUTTONUP = 0x00A5,
            WM_NCRBUTTONDBLCLK = 0x00A6,
            WM_NCMBUTTONDOWN = 0x00A7,
            WM_NCMBUTTONUP = 0x00A8,
            WM_NCMBUTTONDBLCLK = 0x00A9,
        }


        private bool MouseIsDown;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //Mouse Down
                case (int)WindowMessages.WM_NCLBUTTONDOWN:
                case (int)WindowMessages.WM_NCRBUTTONDOWN:
                case (int)WindowMessages.WM_NCMBUTTONDOWN:
                    MouseIsDown = true;
                    Debug.Write("NCMouseDown");
                    break;
                //MouseMove
                case (int)WindowMessages.WM_NCMOUSEMOVE:

                    if (MouseIsDown)
                    {
                        Debug.Write("NCMouseMove");
                        MouseIsDown = false;
                    }
                    break;
                //Mouse Up
                case (int)WindowMessages.WM_NCRBUTTONUP:
                case (int)WindowMessages.WM_NCLBUTTONUP:
                case (int)WindowMessages.WM_NCMBUTTONUP:
                    Debug.Write("NCMouseUp");
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        #region IDisposable 

        public void Dispose()
        {
            base.ReleaseHandle();
        }

        #endregion
    }

      

0


source







All Articles