Is there a way to determine if the mouse is within the LinkArea of a LinkLabel control in C#?

Any help would be greatly appreciated.

有帮助吗?

解决方案

You can use Mouse Enter Event

linkLabel1.MouseEnter += new EventHandler(linkLabel1_MouseEnter);

private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
    MessageBox.Show("Mouse is within link area");
}

其他提示

This cannot be done. The MouseEnter event suggested in the other answer at the time of this writing will fire any time the mouse enters the control area, regardless of whether it is actually over link text or not.

If your LinkLabel control's boundaries are small relative to its content, then the MouseEnter event may work well enough. But in the case where (for example) you want your link to change color when the mouse hovers over the link text and there is a lot of area within the control around the text, then this approach will not work.

On a somewhat unrelated note, this also prevents you from detecting which link within the LinkLabel is currently being hovered over if you have multiple, as mentioned here.

One more detail in case anybody is wondering: the LinkLabel.LinkArea property is not what you are looking for, either. That only determines which characters within the LinkLabel are actually part of a link, and not the actual area they occupy onscreen.

To wrap up, the only way you may be able to get the functionality you are looking for is to implement your own custom control which behaves similarly to the Label or LinkLabel control, but adds the methods and/or properties that you need.

Since the original LinkLabel has a protected function PointInLink this is not hard to do:

using System;
using System.Windows.Forms;

namespace MyControlNameSpace
{
    /// <summary>
    /// Data for a HoveredLinkChanged-Handler.
    /// </summary>
    public class HoveredLinkChangedEventArgs : EventArgs
    {
        private readonly LinkLabel.Link m_Link;

        /// <summary>
        /// Creates data for a HoveredLinkChanged-Handler
        /// </summary>
        /// <param name="link">the Link, with the mouse pointer over it</param>
        public HoveredLinkChangedEventArgs(LinkLabel.Link link)
        {
            m_Link = link;
        }

        /// <summary>
        /// Returns the hovered Link
        /// </summary>
        public LinkLabel.Link HoveredLink
        {
            get { return m_Link; }
        }
    }

    /// <summary>
    /// The structure of a HoveredLinkChanged-Handler
    /// </summary>
    public delegate void HoveredLinkChangedEventHandler(
              object sender, HoveredLinkChangedEventArgs e);

    /// <summary>
    /// Adds to LinkLabel the possiblity to react on changes
    /// of the hovered Link (e.g. to alter a TooltipText).
    /// </summary>
    public class LinkLabelEx : LinkLabel
    {
        private Link m_HoveredLink;

        /// <summary>
        /// Occurs, when another Link is hovered.
        /// </summary>
        public event HoveredLinkChangedEventHandler HoveredLinkChanged;

        /// <summary>
        /// Raises the HoveredLinkChanged event
        /// </summary>
        /// <param name="hoveredLink">the hovered Link</param>
        protected virtual void OnHoveredLinkChanged(Link hoveredLink)
        {
            if (HoveredLinkChanged != null)
                HoveredLinkChanged(this,
                    new HoveredLinkChangedEventArgs(hoveredLink));
        }

        /// <summary>
        /// Raises the Control.OnMouseMove(MouseEventArgs) event.
        /// </summary>
        /// <param name="e">a MouseEventArgs containing the event data</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Link currentLink = PointInLink(e.X, e.Y);
            if (Equals(currentLink, m_HoveredLink)) return;
            m_HoveredLink = currentLink;
            OnHoveredLinkChanged(m_HoveredLink);
        }
    }
}

Then there are only two things left to do:

Add an eventhandler to your LinkLabelEx, e.g.:

linkLabelEx1.HoveredLinkChanged += linkLabelEx1_HoveredLinkChanged;

Set the tooltiptext according to the link given in the eventArgs, e.g.:

void linkLabelEx1_HoveredLinkChanged(object sender, HoveredLinkChangedEventArgs e)
{
    string ttt = e.Link == null
        ? string.Empty
        : e.Link.Description;
    toolTip1.SetToolTip((Control)sender, ttt);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top