我可以告诉标签上点击了哪个字符吗?

时间:2011-04-16 11:14:35

标签: .net winforms .net-4.0 label

我有一个带有标签的窗体(或其他控件,我打开选项),显示有外来字符。

我想要做的是,当用户点击其中一个字符时,它应该打开一个模态对话框,其中包含有关该字符的信息。

标签盒是否可以知道标签的哪个部分或哪个字符被点击了?

4 个答案:

答案 0 :(得分:2)

我会使用FlowLayoutPanel并将每个字符分别添加为LinkLabel

答案 1 :(得分:0)

您可以自己获取此信息,在标签上使用其中一个MouseEvents。将其与Graphics.MeasureStringMouseEventArgs.Location结合使用,请记住可能的Control.Padding值,您可能会接近所需的值。

但这可能不是实现目标的最佳方式(查看获取此简单信息所需的所有内容)。就个人而言,我会选择Chris' answer

答案 2 :(得分:0)

试一下

    private void label_MouseClick(object sender, MouseEventArgs e)
    {
        Label lbl = sender as Label;
        string s = "";
        foreach (char c in lbl.Text)
        {
            s += c.ToString();
            var x = TextRenderer.MeasureText(s, lbl.Font, lbl.Size, TextFormatFlags.TextBoxControl);
            Rectangle txtRec = new Rectangle(-lbl.Margin.Left, -lbl.Margin.Top, x.Width, x.Height);
            if (txtRec.Contains(e.Location))
            {
                MessageBox.Show("You clicked " + c.ToString());
                break;
            }
        }
    }

答案 3 :(得分:0)

public partial class Form1 : Form
{
    CharInfo[] charinfo;
    int selectedChar = -1;

    public Form1()
    {
        InitializeComponent();
        charinfo = CharInfo.MeasureCharacters(label1);
        label1.BackColor = Color.Transparent;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (selectedChar >= 0)
        {
            Rectangle highlightRect = Rectangle.Round(charinfo[selectedChar].charBounds);
            highlightRect.Offset(label1.Location);
            e.Graphics.FillRectangle(SystemBrushes.Highlight, highlightRect);
        }
        base.OnPaint(e);
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        for (int i = 0; i < charinfo.Length; i++)
        {
            if (charinfo[i].charBounds.Contains(e.Location))
            {
                if (selectedChar != i)
                {
                    selectedChar = i;
                    Invalidate(label1.Bounds);
                }
                break;
            }
        }
    }

    class CharInfo
    {
        public RectangleF charBounds;
        public int charIndex;
        CharInfo(RectangleF rect, int index)
        {
            charBounds = rect;
            charIndex = index;
        }

        public static CharInfo[] MeasureCharacters(Label lbl)
        {
            using (Graphics g = Graphics.FromHwnd(lbl.Handle))
            {
                StringFormat sf = new StringFormat();
                List<CharInfo> result = new List<CharInfo>();
                for (int curIndex = 0; lbl.Text.Length > curIndex; curIndex += 32)
                {
                    int nextGroup = Math.Min(lbl.Text.Length, curIndex + 32);
                    CharacterRange[] ranges = new CharacterRange[nextGroup - curIndex];
                    for (int i = curIndex; i < nextGroup; i++)
                        ranges[i % 32] = new CharacterRange(i, 1);
                    sf.SetMeasurableCharacterRanges(ranges);
                    Region[] charRegions = g.MeasureCharacterRanges(lbl.Text, lbl.Font, lbl.ClientRectangle, sf);
                    for (int i = 0; i < charRegions.Length; i++)
                        result.Add(new CharInfo(charRegions[i].GetBounds(g), i));
                    foreach (Region r in charRegions)
                        r.Dispose();
                }
                sf.Dispose();
                return result.ToArray();
            }
        }
    }

    private void label1_MouseClick(object sender, MouseEventArgs e)
    {
        if (selectedChar >= 0)
            MessageBox.Show("You clicked " + label1.Text[selectedChar]);
    }
}