fortsoft

LinkLabel

Mar 10th, 2023 (edited)
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | Source Code | 0 0
  1. /**
  2.  * This is open-source software licensed under the terms of the MIT License.
  3.  *
  4.  * Copyright (c) 2023-2025 Petr Červinka - FortSoft <[email protected]>
  5.  * Copyright (c) 2012 Hamid Sadeghian
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  **
  25.  * Version 1.2.0.0
  26.  */
  27.  
  28. using System;
  29. using System.Runtime.InteropServices;
  30.  
  31. namespace FortSoft.Controls.WinForms {
  32.  
  33.     /// <summary>
  34.     /// Link label control that enforces the platform's native hand cursor
  35.     /// (IDC_HAND) instead of the bitmap provided by <c>Cursors.Hand</c>.
  36.     /// This ensures visual consistency with the operating system theme.
  37.     /// </summary>
  38.     public sealed class LinkLabel : System.Windows.Forms.LinkLabel {
  39.  
  40.         /// <summary>
  41.         /// IDC_HAND from WinUser.h (0x7F89 == 32649).
  42.         /// </summary>
  43.         private const int IDC_HAND = 0x7F89;
  44.  
  45.         /// <summary>
  46.         /// Loads a cursor resource.
  47.         /// </summary>
  48.         /// <param name="hInstance">
  49.         /// A module handle to load the cursor from, or <see cref="IntPtr.Zero"/>
  50.         /// to request a predefined system cursor (IDC_*).
  51.         /// </param>
  52.         /// <param name="lpCursorName">
  53.         /// The resource identifier of the cursor. When <paramref name="hInstance"/>
  54.         /// is <see cref="IntPtr.Zero"/>, pass the integer identifier of a predefined
  55.         /// system cursor (for example <see cref="IDC_HAND"/>).
  56.         /// </param>
  57.         /// <returns>
  58.         /// A handle to the cursor (<see cref="IntPtr"/>); returns
  59.         /// <see cref="IntPtr.Zero"/> on failure.
  60.         /// </returns>
  61.         [DllImport("user32.dll", EntryPoint = "LoadCursorW", ExactSpelling = true)]
  62.         private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
  63.  
  64.         /// <summary>
  65.         /// Lazily created system hand cursor with safe fallback.
  66.         /// </summary>
  67.         private static readonly System.Windows.Forms.Cursor SystemHandCursor = CreateSystemHandCursor();
  68.  
  69.         /// <summary>
  70.         /// Creates the system hand cursor or falls back to <c>Cursors.Hand</c>
  71.         /// if the system cursor cannot be loaded.
  72.         /// </summary>
  73.         /// <returns>
  74.         /// A <see cref="System.Windows.Forms.Cursor"/> representing the native
  75.         /// hand cursor, or <c>Cursors.Hand</c> if loading fails.
  76.         /// </returns>
  77.         private static System.Windows.Forms.Cursor CreateSystemHandCursor() {
  78.             IntPtr handle = LoadCursor(IntPtr.Zero, IDC_HAND);
  79.             if (handle == IntPtr.Zero) {
  80.                 return System.Windows.Forms.Cursors.Hand;
  81.             }
  82.             return new System.Windows.Forms.Cursor(handle);
  83.         }
  84.  
  85.         /// <summary>
  86.         /// Replaces the base class hand cursor with the native system hand cursor
  87.         /// when the mouse moves over the control and the base logic selects
  88.         /// <c>Cursors.Hand</c>.
  89.         /// </summary>
  90.         /// <param name="e">
  91.         /// Mouse event data provided by the Windows Forms event pipeline.
  92.         /// </param>
  93.         protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
  94.             base.OnMouseMove(e);
  95.             if (OverrideCursor == System.Windows.Forms.Cursors.Hand) {
  96.                 OverrideCursor = SystemHandCursor;
  97.             }
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment