C#跨平台RichTextbox URL或替代解决方案
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为替代方案,您可以使用 gtktextview ,它应该是交叉平台。您可以使用下面的代码添加超链接样式的文本:
其中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:
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