我能分辨出点击了标签上的哪个字符吗?

问题描述 投票:0回答:5

我有一个带有标签(或另一个控件,我愿意选择)的窗口窗体,它显示外来字符。

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

标签框是否有可能知道点击了标签的哪一部分或哪个字符?

.net winforms .net-4.0 label
5个回答
2
投票

我会使用

FlowLayoutPanel
并将每个字符分别添加为
LinkLabel
.


0
投票

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

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


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;
            }
        }
    }

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]);
    }
}

0
投票

如果有人要使用 DOTNET MAUI,那么可以在这个 Github Repo 上找到一个完整的 Windows 示例应用程序: https://github.com/rafaelduluc/DOTNETMAUI-CHATGPT 这将为您提供解决方案和“专业知识”——让您做更多事情的基础。 希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.