背后开枪的图片卡通:用CLinkControl给MFC程序添加超链接

来源:百度文库 编辑:九乡新闻网 时间:2024/05/08 03:56:14

关于CLinkControl:http://msdn.microsoft.com/en-us/library/tdfd1sb4(v=vs.80).aspx SysLink:http://msdn.microsoft.com/en-us/library/bb760706(VS.85).aspx CLinkCtontrol无法显示:http://msdn.microsoft.com/en-us/library/bb773175(v=VS.85).aspx

Syslink Control in MFC 9.0

several important updates for VC++ and MFC. Among them the possibility to create syslink controls, command or split buttons and network address controls. In this post I will show how you can work with the syslink control. The control provides a way to embed hypertext links in a window. It is actually a window that renders marked-up text just as hyperlinks in a web browser. Multiple links can be put in a single control, and are accessed by a zero-based index.

Currently it supports the anchor tag () with the HREF and ID attributes. HREF is used to specify a URL of any protocol (http, ftp, mailto, etc.). On the other hand ID specifies an unique name within the control, associated with an individual link.

The content is available in the toolbar, so you can simply drag and drop syslink controls onto you dialog template.

You can specify the text, including the anchor tag from the properties page, or you can use the SetWindowText function to set it at run-time.

GetDlgItem(IDC_SYSLINK1)->SetWindowText(
   L"Visit my web site"
   L" and check my blog.");



You have to handle the NM_CLICK notification, check which link was clicked and take the appropriate action:

BEGIN_MESSAGE_MAP(CMFCDemoDlg, CDialog)
        ON_NOTIFY(NM_CLICK, IDC_SYSLINK1, &CMFCDemoDlg::OnNMClickSyslink1)
END_MESSAGE_MAP()  

void CMFCDemoDlg::OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult)
{
        PNMLINK pNMLink = (PNMLINK) pNMHDR;  

        if (wcscmp(pNMLink->item.szUrl, WEB_SITE) == 0)
        {
                ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
        }
        else if(wcscmp(pNMLink->item.szUrl, BLOG_LINK) == 0)
        {
                ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
        }  

        *pResult = 0;
}

In MFC 9.0 (version that will ship with Visual Studio 2008) class CLinkCtrl is a wrapper over the Windows API for working with the syslink control.

You can associate an instance of CLinkCtrl with a syslink control through the DDX mechanism:

void CMFCDemoDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_SYSLINK2, Link2);
}

In my demo application that you can download from here I used a second syslink with an ID attribute. Each time the link is clicked it increments a counter, which is shown. For that I created two helper functions first, one to generate part of the text and the second to set the text to the link control:

CString CMFCDemoDlg::GetClickText() const
{
        CString str;
        str.Format(L"clicked %d times", Clicks);
        return str;
}  

void CMFCDemoDlg::SetLink2Text()
{
        Link2.SetWindowText(L"Link was " + GetClickText() + L"");
}

When handling the NM_CLICK notification, I checked the szID member of structure LITEM and took the appropriate action:

void CMFCDemoDlg::OnNMClickSyslink2(NMHDR *pNMHDR, LRESULT *pResult)
{
        PNMLINK pNMLink = (PNMLINK) pNMHDR;  

        if (wcscmp(pNMLink->item.szID, L"clicked") == 0)
        {
                Clicks++;
                SetLink2Text();
        }  

        *pResult = 0;
}

The result is shown here:

Hopefully the sample code that I put together will help you work with CLinkCtrl and the syslink control in VS 2008.