Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# I';当我在WinForms中单击富格文本框中的超链接时,我试图检测鼠标单击的位置,我该怎么做?_C#_Visual Studio_Winforms - Fatal编程技术网

C# I';当我在WinForms中单击富格文本框中的超链接时,我试图检测鼠标单击的位置,我该怎么做?

C# I';当我在WinForms中单击富格文本框中的超链接时,我试图检测鼠标单击的位置,我该怎么做?,c#,visual-studio,winforms,C#,Visual Studio,Winforms,因此,当我在WinForms中单击富格文本框中的超链接时,我试图检测鼠标单击的位置,我该怎么做?我想,在表单中随时随地进行全局鼠标单击是解决方案,但是使用表单甚至面板鼠标单击事件,只有在我不单击控件时才会发生,这不是我想要的。单击richtextbox中的超链接时,您可以尝试以下代码来实现全局鼠标位置 您可以使用winapiGetCursorPos获取goobal鼠标位置 [DllImport("user32.dll")] static ext

因此,当我在WinForms中单击富格文本框中的超链接时,我试图检测鼠标单击的位置,我该怎么做?我想,在表单中随时随地进行全局鼠标单击是解决方案,但是使用表单甚至面板鼠标单击事件,只有在我不单击控件时才会发生,这不是我想要的。

单击richtextbox中的超链接时,您可以尝试以下代码来实现全局鼠标位置

您可以使用winapi
GetCursorPos
获取goobal鼠标位置

        [DllImport("user32.dll")]
        static extern bool GetCursorPos(ref Point lpPoint);
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.AppendText(
                @"Google:http://www.google.cn/  c#:https://docs.microsoft.com/en-us/dotnet/csharp/ testsssssssssssHelo,worldssssssssssssssssssssssssssssssssssssssssss");
            richTextBox1.LinkClicked += RichTextBox1_LinkClicked;
        }

        private void RichTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            var pt = new Point();

            GetCursorPos(ref pt);
            textBox1.Text = pt.X.ToString();
            textBox2.Text = pt.Y.ToString();
        }
例如:


嘿,伙计们,所以我想出了一个适合我的解决方案

 private void dropDownBoxSite2Term2_LinkClicked(object sender, LinkClickedEventArgs e)
        {
          //I use and bool to check if any link inside the rtb was clicked, and also grab //the text:
            subRTBLinkClicked = true;
            subRTBLinkText = e.LinkText;
        }

        //This is the rtb im using:
        private void dropDownBoxSite2Term3_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
//This is how i grabbed the client position:
                RTBDropDownLMBLocation = panel2.PointToClient(dropDownBoxSite2Term3.PointToScreen(new Point(e.X, e.Y)));

//Using the bool from above i make an action occur
                if (subRTBLinkClicked)
                {
                     //make action happen that i need and use RTBDropDownLMBLocation
                     //for desired needs, like instantiate another textbox at click //position if needed for example
                 }
}

希望这有助于其他人

你能把你已经尝试过的解决方案贴出来吗?你首先想做什么?为什么您关心坐标,为什么在RTF框中显示超链接而不是WebView?我尝试将rtb的linkclicked事件与rtb mouseclick以及rtb mousedown结合使用。当我在富文本框中单击超链接时,鼠标单击或鼠标向下尝试获取鼠标单击的位置,但是,这些事件在单击超链接时不会注册,因此我无法检索单击的位置。我想在rtb内的超链接旁边弹出另一个框,因此我需要链接的位置,因为链接列表永远不会固定。您可以始终获取Control.MouseLocation并将其转换为客户端坐标。您在寻找什么?链接的第一个字符的位置?鼠标指针的相对位置(考虑ClientRectangle)?单击点的绝对位置(考虑控件的滚动位置)?其他…为什么你独自一人时会发出PInvoking(Cursor.Position)呢?这是对这个问题的评论。如果您继续阅读,OP会问:如何将其转换为客户坐标?我很高兴听到您的问题已经解决,您可以单击'✔' 将你的答复标记为答复。它还将帮助其他人解决类似问题。