Un-Antialiased Hand Cursor in Windows Forms Apps!

后端 未结 4 391
鱼传尺愫
鱼传尺愫 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:27

    Excuse me for resurrecting a year-old thread!!!

    After messing around with the original solution and taking a look at the reflected LinkLabel source code, I "finally" found a quick yet clean way of doing it :

    using System.Runtime.InteropServices;
    
    namespace System.Windows.Forms {
        public class LinkLabelEx : LinkLabel {
            private const int IDC_HAND = 32649;
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
    
            private static readonly Cursor SystemHandCursor = new Cursor(LoadCursor(IntPtr.Zero, IDC_HAND));
    
            protected override void OnMouseMove(MouseEventArgs e) {
                base.OnMouseMove(e);
    
                // If the base class decided to show the ugly hand cursor
                if(OverrideCursor == Cursors.Hand) {
                    // Show the system hand cursor instead
                    OverrideCursor = SystemHandCursor;
                }
            }
        }
    }
    

    This class actually does what we want: It shows the proper system hand cursor without flickering and does this only on the LinkArea of the control.

提交回复
热议问题