Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This is open-source software licensed under the terms of the MIT License.
- *
- * Copyright (c) 2023-2025 Petr Δervinka - FortSoft <[email protected]>
- * Copyright (c) 2012 Hamid Sadeghian
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- **
- * Version 1.2.0.0
- */
- using System;
- using System.Runtime.InteropServices;
- namespace FortSoft.Controls.WinForms {
- /// <summary>
- /// Link label control that enforces the platform's native hand cursor
- /// (IDC_HAND) instead of the bitmap provided by <c>Cursors.Hand</c>.
- /// This ensures visual consistency with the operating system theme.
- /// </summary>
- public sealed class LinkLabel : System.Windows.Forms.LinkLabel {
- /// <summary>
- /// IDC_HAND from WinUser.h (0x7F89 == 32649).
- /// </summary>
- private const int IDC_HAND = 0x7F89;
- /// <summary>
- /// Loads a cursor resource.
- /// </summary>
- /// <param name="hInstance">
- /// A module handle to load the cursor from, or <see cref="IntPtr.Zero"/>
- /// to request a predefined system cursor (IDC_*).
- /// </param>
- /// <param name="lpCursorName">
- /// The resource identifier of the cursor. When <paramref name="hInstance"/>
- /// is <see cref="IntPtr.Zero"/>, pass the integer identifier of a predefined
- /// system cursor (for example <see cref="IDC_HAND"/>).
- /// </param>
- /// <returns>
- /// A handle to the cursor (<see cref="IntPtr"/>); returns
- /// <see cref="IntPtr.Zero"/> on failure.
- /// </returns>
- [DllImport("user32.dll", EntryPoint = "LoadCursorW", ExactSpelling = true)]
- private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
- /// <summary>
- /// Lazily created system hand cursor with safe fallback.
- /// </summary>
- private static readonly System.Windows.Forms.Cursor SystemHandCursor = CreateSystemHandCursor();
- /// <summary>
- /// Creates the system hand cursor or falls back to <c>Cursors.Hand</c>
- /// if the system cursor cannot be loaded.
- /// </summary>
- /// <returns>
- /// A <see cref="System.Windows.Forms.Cursor"/> representing the native
- /// hand cursor, or <c>Cursors.Hand</c> if loading fails.
- /// </returns>
- private static System.Windows.Forms.Cursor CreateSystemHandCursor() {
- IntPtr handle = LoadCursor(IntPtr.Zero, IDC_HAND);
- if (handle == IntPtr.Zero) {
- return System.Windows.Forms.Cursors.Hand;
- }
- return new System.Windows.Forms.Cursor(handle);
- }
- /// <summary>
- /// Replaces the base class hand cursor with the native system hand cursor
- /// when the mouse moves over the control and the base logic selects
- /// <c>Cursors.Hand</c>.
- /// </summary>
- /// <param name="e">
- /// Mouse event data provided by the Windows Forms event pipeline.
- /// </param>
- protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
- base.OnMouseMove(e);
- if (OverrideCursor == System.Windows.Forms.Cursors.Hand) {
- OverrideCursor = SystemHandCursor;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment