MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)

 

1、新建一个基于对话框的工程MyLink,在对话框中拖一个Static控件,ID可改为IDC_SLink。

2、在头文件中添加成员变量:

private:
 CRect m_Rect;
 CFont* m_cfNtr;
 CFont m_cfUL;
 LOGFONT m_lfNtr, m_lfUL;
 COLORREF m_color;
 CBrush m_brush;

 

3、在.cpp文件中利用类向导添加初始化虚函数,并添加代码:

BOOL CMyLinkDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。

    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标

    // TODO: 在此添加额外的初始化代码
    GetDlgItem(IDC_SLink)->GetWindowRect(&m_Rect);
    ScreenToClient (&m_Rect);

    m_cfNtr = this->GetFont();
    m_cfNtr->GetLogFont ( &m_lfNtr );
    m_cfNtr->GetLogFont ( &m_lfUL );
    m_lfUL.lfUnderline = TRUE;
    m_cfUL.CreateFontIndirect ( &m_lfUL );

    m_brush.CreateSysColorBrush ( COLOR_MENU );
    m_color = RGB (0,0,0);

    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

 

再利用类向导添加下面几个消息:

void CMyLinkDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CString strLink;
    UpdateData(TRUE);
    GetDlgItem(IDC_SLink)->GetWindowText(strLink);
    if (point.x > m_Rect.left && point.x < m_Rect.right && point.y < m_Rect.bottom && point.y > m_Rect.top )
    {
        ShellExecute (NULL, NULL, strLink,NULL, NULL, SW_NORMAL);
    }

    CDialog::OnLButtonUp(nFlags, point);
}
void CMyLinkDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if ( point.x > m_Rect.left && point.x < m_Rect.right && point.y < m_Rect.bottom && point.y > m_Rect.top )
    {
        HCURSOR hCursor;
        hCursor = ::LoadCursor ( NULL, IDC_HAND );
        ::SetCursor ( hCursor );

        GetDlgItem(IDC_SLink)->SetFont ( &m_cfUL );

        m_color = RGB (0,0,225);
        CStatic* m_pStatic = (CStatic*)GetDlgItem(IDC_SLink);
        m_pStatic->RedrawWindow ();
    }
    else
    {
        GetDlgItem(IDC_SLink)->SetFont ( m_cfNtr );

        m_color = RGB (0,0,0);
        CStatic* m_pStatic = (CStatic*)GetDlgItem(IDC_SLink);
        m_pStatic->RedrawWindow();
    }

    CDialog::OnMouseMove(nFlags, point);
}
HBRUSH CMyLinkDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO:  在此更改 DC 的任何属性
    if ( nCtlColor == CTLCOLOR_STATIC )
    {
        pDC->SetBkMode ( TRANSPARENT );
        pDC->SetTextColor ( m_color );

        return (HBRUSH)m_brush.GetSafeHandle ();
    }

    // TODO:  如果默认的不是所需画笔,则返回另一个画笔
    return hbr;
}

效果图如下:

 

资料来源:http://www.pudn.com/downloads513/sourcecode/windows/shell/detail2129288.html

posted @ 2013-06-19 00:38  紫清婷  阅读(11490)  评论(0编辑  收藏  举报