1

I have a WinForm and now I need to change the cursor when it's in the windows caption part. I have some code working, it has 2 problems:

  1. It also changes the cursor when on the edges (normal resize cursor should be shown). I found out the I need something like this WM_NCHITTEST & HTTOP, but how do I combine that?
  2. There's some flicker when moving the mouse.

I also tried placing the code below the base.WndProc(ref m);.

This is the code I already have:

if ((m.Msg == Win32.WM.NCMOUSEMOVE
    || m.Msg == Win32.WM.NCLBUTTONDOWN || m.Msg == Win32.WM.NCLBUTTONUP
    || m.Msg == Win32.WM.NCRBUTTONDOWN || m.Msg == Win32.WM.NCRBUTTONUP)
)
{
    if (m.WParam.ToInt32() != Win32.HT.TOP && m.WParam.ToInt32() != Win32.HT.RIGHT && m.WParam.ToInt32() != Win32.HT.BOTTOM && m.WParam.ToInt32() != Win32.HT.LEFT)
    {
        Cursor = Cursors.Hand;
    }
}

EDIT:
I wasn't logging the message correctly in Spy++. Found the solution to the window edges (see updated code).

Thnx, J

jerone
  • 16,206
  • 4
  • 39
  • 57
  • What about faking the window caption part? just a thought. – NinethSense Jun 26 '11 at 08:29
  • I have extended the window caption part to bellow, to allow glass transparency. On top of that I have drawn something, but I can't figure out how to correctly change the mouse cursor on the whole caption. – jerone Jun 26 '11 at 08:31

1 Answers1

5

It flickers because you use the wrong message. Any mouse move is followed by WM_SETCURSOR to allow the app to change the cursor. So the cursor changes back to the default. Intercept WM_SETCURSOR instead. The low word of LParam contains the hit test code.

    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x20) {  // Trap WM_SETCUROR
            if ((m.LParam.ToInt32() & 0xffff) == 2) { // Trap HTCAPTION
                Cursor.Current = Cursors.Hand;
                m.Result = (IntPtr)1;  // Processed
                return;
            }
        }
        base.WndProc(ref m);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thnx. Aldo this fixes the flicker I was having, this does enable the other 'bug' partly. Only the top and topleft resize cursor is shown. – jerone Jun 26 '11 at 15:44
  • That seems to have very little to do with the snippet you posted. I'm getting good resize cursors on all edges and corners with the snippet that I posted. Testing the hit test code is essential, the edges and corners have a different hit code, it won't be 2. – Hans Passant Jun 26 '11 at 16:00
  • Sorry was my fault in converting it. Thank you for the answer – jerone Jun 26 '11 at 18:25