如何在丰富的编辑控件中实现URL的鼠标单击 [英] How to implement the mouse click for URLs at rich edit control

查看:89
本文介绍了如何在丰富的编辑控件中实现URL的鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向对话框中添加了一个只读的丰富edit 2.0控件(代码使用C Windows API,该对话框是使用功能

I added a read-only rich edit 2.0 control to my dialog (code is using C windows API, the dialog is created by using function DialogBox)

在对话框的回调中,在WM_INITDIALOG处,添加以下代码以启用url检测,并启用ENM_LINK事件发送到父对话框而不是丰富的编辑控件本身:

At the dialog call back, at the WM_INITDIALOG, I add the following code to enable url detection and also enable the event ENM_LINK is sent to the parent dialog instead of the rich edit control itself:

LRESULT mask = SendMessage(hWndText, EM_GETEVENTMASK, 0, 0); //hWndText is rich edit control
SendMessage(hWndText, EM_SETEVENTMASK, 0, mask | ENM_LINK);
::SendMessage(hWndText, EM_AUTOURLDETECT, TRUE, NULL);  

我在启动对话框时启用url检测时遇到了一些麻烦(这似乎是一个已知问题或行为,因为丰富的编辑控件将仅启用对已修改文本的url检测).但是,我通过在每个WM_PAINT事件上再次设置对话框文本来解决此问题.

I had a little trouble to enable the url detection when dialog is initially launched (which seems a known issue or behavior since rich edit control would only enable url detection of modified text). However I worked around this issue by setting the dialog text again on every WM_PAINT event.

该代码通常可以正常工作.当鼠标悬停在url上时,我还实现了以下代码以在浏览器中启动URL:

The code is generally working. I also implemented the following code to launch the URL at the browser when mouse is hovering over the url:

case WM_NOTIFY:
    plink = (ENLINK *) lParam;
    switch(LOWORD(wParam))
    {   
        case IDC_DISPLAY_TEXT_2: //this is ID for my rich edit control
            szURL =m_strDisplay.Mid(plink->chrg.cpMin, plink->chrg.cpMax - plink->chrg.cpMin);          
            LaunchURL(szURL); //function to launch the url with default browser
            break;
        default:
            break;
    }

似乎每次将鼠标悬停在url上时,都会得到WM_NOTIFY事件.但是,当我单击它时,总是会发生与鼠标悬停相同的事件.

It seems that I would get WM_NOTIFY event every time when I hovered the mouse over the url. However when I clicked on it, I always get same event as the mouse hover over.

基于 ENLINK ,我应该在NMHDR结构上获得更详细的NM事件,但是值plink-> nmhdr.code始终为1803,甚至不是NM_HOVER(其定义值为(NM_FIRST-13),而NM_FIRST为( 0U-0U),因此在我的64位计算机上,NM_HOVER值为4294967283).我知道我在这里错过了一些东西.有人可以在这里点灯吗?如何获取丰富的编辑控件的鼠标单击事件?

Based on the structure of ENLINK, I should get more detailed NM event at the NMHDR structure, however the value plink->nmhdr.code is always 1803 which is not even NM_HOVER (its defined value is (NM_FIRST-13) and NM_FIRST is (0U- 0U), so NM_HOVER value is 4294967283 on my 64 bit machine). I know that I am missing something here. Could someone shed some lights here? How can I get the mouse click event for the rich edit control?

推荐答案

我认为您应该捕获

I think you should capture the EN_LINK notification. I implemented the following code. It enables a url link in a richedit control placed into the parent window, not into a dialog. You could adapt it for your dialog, as well.

请考虑以下代码:

case WM_NOTIFY: {
switch (((LPNMHDR)lParam)->code) { //NMHDR structure contains information about a notification message.
        case EN_LINK: {
            ENLINK *enLinkInfo = (ENLINK *)lParam; // pointer to a ENLINK structure

然后,如果您选择在LBUTTONUP上启动url,则必须检查enLinkInfo->msg中包含的值(不过请记住要使其适应对话框)

then, if you choose to launch url on LBUTTONUP, you have to check the value contained in enLinkInfo->msg (remember to adapt it for your dialog, though)

 if (enLinkInfo->msg == WM_LBUTTONUP) {
// select all the text from enLinkInfo->chrg.cpMin to enLinkInfo->chrg.cpMax
// lauch the url

}

此外,您可以拦截WM_MOUSEMOVE:

Besides, you can intercept WM_MOUSEMOVE:

if(enLinkInfo->msg == WM_MOUSEMOVE) {
                ; // do nothing
}

希望有帮助.

这篇关于如何在丰富的编辑控件中实现URL的鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆