如何在 RichEdit 2.0 中自动检测 url?

发布于 2024-07-12 19:36:57 字数 366 浏览 3 评论 0原文

当我们有一个 RichEdit 控件并向其发送一条 WPARAM 设置为 TRUE 的 EM_AUTOURLDETECT 消息时,它会很好地突出显示检测到的 URL 并发送 EN_LINK 通知。 但它仅对输入到控件中的文本执行此操作。 我还没有找到使用 SetWindowTextEM_STREAMIN 加载到控件中的文本的方法。 请帮忙! 谢谢

更新: 我从头开始创建了一个测试应用程序,它运行良好。 我认为问题可能是我对控件进行了超类化,即创建了一个新的窗口类并仅使用原始类的窗口过程。 我将尝试对控件进行子类化..

When we have a RichEdit control and send it an EM_AUTOURLDETECT message with WPARAM set to TRUE, it nicely hightlights the detected URLs and sends the EN_LINK notifications.
But it does this only for text that is entered into the control. I haven't found the way to do it for text that's loaded into the control with SetWindowText or EM_STREAMIN.
Please help! Thanks

Upd:
I've created a test application from scratch and it works fine there. I think the problem might be that I have superclassed the control, that is, created a new window class and just use the window procedure of the original class. I'm gonna try subclassing the control instead..

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

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

发布评论

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

评论(3

白昼 2024-07-19 19:37:06

您可能只需将文本重写到控件即可重新解析。

You might just have to rewrite the text to the control to get it to re-parse.

毁梦 2024-07-19 19:37:04

在不知道您尝试使用 SetWindowText 和 EM_STREAMIN 添加到控件的文本格式的情况下,我将进行猜测并说这可能与控件的文本模式有关。 设置控件的内容后,尝试向其发送 EM_GETTEXTMODE< /a> 消息并查看 TM_PLAINTEXT 位是否已设置。 如果是这种情况,请尝试发送 EM_SETTEXTMODE 消息后跟 EM_AUTOURLDETECT。 您的代码应该如下所示:

UINT textmode = (UINT)::SendMessage(handle_to_control, EM_GETTEXTMODE, 0, 0);
if(textmode & TM_PLAINTEXT) {
    textmode &= ~TM_PLAINTEXT;    // Clear the TM_PLAINTEXT bit
    textmode |= TM_RICHTEXT;      // Set the TM_RICHTEXT bit
    if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) {
        // Failed to set the text mode
    }
}
::SendMessage(handle_to_control, EM_AUTOURLDETECT, TRUE, 0);

Without knowing the format of the text you are trying to add to the control with SetWindowText and EM_STREAMIN I'm going to take a guess and say this might have something to do with the control's text mode. After setting the contents of the control try sending it a EM_GETTEXTMODE message and see if the TM_PLAINTEXT bit is set. If this is the case then try sending a EM_SETTEXTMODE message followed by EM_AUTOURLDETECT. Your code should look something like this:

UINT textmode = (UINT)::SendMessage(handle_to_control, EM_GETTEXTMODE, 0, 0);
if(textmode & TM_PLAINTEXT) {
    textmode &= ~TM_PLAINTEXT;    // Clear the TM_PLAINTEXT bit
    textmode |= TM_RICHTEXT;      // Set the TM_RICHTEXT bit
    if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) {
        // Failed to set the text mode
    }
}
::SendMessage(handle_to_control, EM_AUTOURLDETECT, TRUE, 0);
你不是我要的菜∠ 2024-07-19 19:37:01

我刚刚构建了一个基于 WTL 对话框的基本应用程序,其中包含 riched20 控件,并且以下工作正常:

CRichEditCtrl richedit = GetDlgItem(IDC_RICHEDIT);
richedit.SetAutoURLDetect(TRUE);
richedit.SetWindowText(_T("http://www.stackoverflow.com"));

我有一些旧的 MFC 代码可以执行类似的操作,尽管使用 ES_STREAM,并且它也工作正常。

FWIW WTL CRichEditCtrl 包装器非常薄。 SetAutoURLDetect 只需调用 SendMessage 并传递 EM_AUTOURLDETECT 即可。

我正在将 _RICHEDIT_VER 设置为 0x0200 FWIW 进行编译。

I just knocked up a basic WTL dialog based app containing a riched20 control and the following works fine:

CRichEditCtrl richedit = GetDlgItem(IDC_RICHEDIT);
richedit.SetAutoURLDetect(TRUE);
richedit.SetWindowText(_T("http://www.stackoverflow.com"));

I have some old MFC code that does something similar, albeit with ES_STREAM, and it works OK too.

FWIW the WTL CRichEditCtrl wrapper is pretty thin. SetAutoURLDetect simply calls SendMessage passing it EM_AUTOURLDETECT.

I am compiling with _RICHEDIT_VER set to 0x0200 FWIW.

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