Un-Antialiased Hand Cursor in Windows Forms Apps!

后端 未结 4 390
鱼传尺愫
鱼传尺愫 2020-12-06 02:46

Okay, so you know how in Windows Vista and Windows 7 MS changed the Hand Cursor (the one that shows up when you hover over a hyperlink), and added more detail to it so it\'s

4条回答
  •  攒了一身酷
    2020-12-06 03:30

    This post solves problems of the other posts:

    • It respects to the link location and shows the hand just when cursor is on link
    • It doesn't flicker on mouse move

    You need to change the cursor to system hand cursor. To do so, you need to handle WM_SETCURSOR and check if OverrideCursor is Cursors.Hand then change it to the system cursor by calling SetCursor:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    public class MyLinkLabel : LinkLabel
    {
        const int IDC_HAND = 32649;
        const int WM_SETCURSOR = 0x0020;
        const int HTCLIENT = 1;
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        static extern IntPtr SetCursor(HandleRef hcursor);
        static readonly Cursor SystemHandCursor = 
            new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == WM_SETCURSOR)
                WmSetCursor(ref msg);
            else
                base.WndProc(ref msg);
        }
        void WmSetCursor(ref Message m)
        {
            if (m.WParam == (IsHandleCreated ? Handle : IntPtr.Zero) &&
               (unchecked((int)(long)m.LParam) & 0xffff) == HTCLIENT) {
                if (OverrideCursor != null) {
                    if (OverrideCursor == Cursors.Hand)
                        SetCursor(new HandleRef(SystemHandCursor, SystemHandCursor.Handle));
                    else
                        SetCursor(new HandleRef(OverrideCursor, OverrideCursor.Handle));
                }
                else {
                    SetCursor(new HandleRef(Cursor, Cursor.Handle));
                }
            }
            else {
                DefWndProc(ref m);
            }
        }
    }
    

提交回复
热议问题