C#跨平台RichTextbox URL或替代解决方案

发布于 2024-12-05 07:01:57 字数 899 浏览 0 评论 0原文

我正在开发一个 C# 应用程序来与 MUD/MOO 服务器通信。基本上它需要文本并将其显示在屏幕上。目前,我正在使用 RichTextBox 来显示文本,并且彩色文本工作正常,我只需要实现 URL,但是在执行此操作时,我发现要添加带有自定义文本的 URL(例如,不是 http://,例如:单击此处)我需要使用 Win32 API,我根本无法做到这一点。这需要在 Linux(也可能是 Mac)上的 Mono 中工作。有办法让这个工作吗?或者我应该寻求哪些替代途径? (我正在考虑切换到 HTML,但是有一个好的跨平台 HTML 控件吗?)(所有这些都必须是免费的)。

提前致谢。

我最终使用以下方法做到了这一点:

Stack<Link> Links = new Stack<Link>();
internal class Link
{
        public int starts = 0;
        public int ends = 0;
        public string url = "";
}
private string GetLink(RichTextBox rtb, Point point)
        {
            int index = rtb.GetCharIndexFromPosition(point);
            foreach (Link link in Links.ToArray())
                if (link.starts <= index && link.ends >= index)
                    return link.url;
            return "";
        }

只需将所有链接附加到链接堆栈,并在 MouseDown 事件中使用 GetLink :)

I am developing a C# application to communicate with a MUD/MOO server. Basically it takes text and displays it on the screen. At the moment i'm using a RichTextBox to display the text and I have coloured text working fine and I only have to implement URLs, however while doing this I discovered that to add URLs with custom text (e.g. NOT http://, like: Click here) I need to use a Win32 API, I can't do this... at all. This needs to work in mono on linux (and possibly mac). Is there anyway to make this work? or what alternative avenues should i pursue? (I was considering switching to HTML, but is there a good cross-platform HTML control?) (All of this HAS to be free).

Thanks in advanced.

I managed to do it in the end using:

Stack<Link> Links = new Stack<Link>();
internal class Link
{
        public int starts = 0;
        public int ends = 0;
        public string url = "";
}
private string GetLink(RichTextBox rtb, Point point)
        {
            int index = rtb.GetCharIndexFromPosition(point);
            foreach (Link link in Links.ToArray())
                if (link.starts <= index && link.ends >= index)
                    return link.url;
            return "";
        }

just append all links to the Links stack, and use GetLink inside the MouseDown event :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2024-12-12 07:01:57

作为替代方案,您可以使用 gtktextview ,它应该是交叉平台。您可以使用下面的代码添加超链接样式的文本:

TextTag tag  = new TextTag("link");
tag.Foreground = "blue";
tag.Underline = Pango.Underline.Single;         
textview1.Buffer.TagTable.Add(tag);

Gtk.TextIter iter = textview1.Buffer.GetIterAtOffset(0);        
textview1.Buffer.InsertWithTagsByName(ref iter, "link text", "link");

其中textView1是gtk.textview。

您应该能够使用“ Motion-Notify-Event”和“事件 - 事后”事件更改光标并在鼠标上进行反应。

希望这对您有所帮助

as an alternative you can use GtkTextView, it should be cross platform. You can add a hyper link styled text using code below:

TextTag tag  = new TextTag("link");
tag.Foreground = "blue";
tag.Underline = Pango.Underline.Single;         
textview1.Buffer.TagTable.Add(tag);

Gtk.TextIter iter = textview1.Buffer.GetIterAtOffset(0);        
textview1.Buffer.InsertWithTagsByName(ref iter, "link text", "link");

where textview1 is Gtk.TextView.

you should be able to change the cursor and react on mouse clicks using "motion-notify-event" and "event-after" events.

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文