我在WinForm中有一个带有URL的RichTextBox:s。 可以单击它们,但我想检测用户是否右键单击它们。

有帮助吗?

解决方案

我认为这是你可能正在寻找的东西。在MouseDownEvent上首先检查您是否正在处理右键单击。然后找出点击的位置,然后回到文本中。

    private void DoMouseDown(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Right)
        {
            RichTextBox rtb = (RichTextBox)sender;
            int charIndex = rtb.GetCharIndexFromPosition(new Point(e.X, e.Y));
            int lineIndex = rtb.GetLineFromCharIndex(charIndex);
            string clickedText = rtb.Lines[lineIndex];

            // now check if the text was indeed a link
            Regex re = new Regex("http://(www\\.)?([^\\.]+)\\.([^\\.]+)");
            bool isLink = re.IsMatch(s);
        }            
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top