MFC静态文本控件设置超链接
有時(shí)我們需要在窗口上設(shè)置一個(gè)超鏈接,比如在Aboutdlg上設(shè)置“我的博客”這樣的超鏈接.具體的設(shè)置方法如下。
1、首先我們?cè)诖绑w上添加一個(gè)Static文本控件,修改Caption屬性,設(shè)置成你想要的超鏈接標(biāo)題,比如“更多內(nèi)容歡迎訪(fǎng)問(wèn)小夢(mèng)的博客”等。
2、這步很重要。由于Static控件不具備設(shè)置超鏈接的能力,我們需要重寫(xiě)一個(gè)繼承自CStatic的類(lèi)CHyperLink.
這里可以直接復(fù)制兩個(gè)文件到你的工程當(dāng)中。
HyperLink.h頭文件:
#pragma once
// CHyperLink
class CHyperLink : public CStatic
{DECLARE_DYNAMIC(CHyperLink)public:CHyperLink();virtual ~CHyperLink();public: void SetURL(CString strURL);CString GetURL() const;void SetColor(COLORREF clr,BYTE clrItem);//設(shè)置顏色COLORREF GetColor(BYTE clrItem);//得到顏色// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHyperLink)
protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementation
protected:int GotoURL(LPCTSTR url, int showcmd);// Protected attributes
protected:COLORREF m_clrHot; // Link normal colorCOLORREF m_clrNor; // Link active colorCOLORREF m_clrBG; // Link active colorBOOL m_bHot; // Is the link active?CString m_strURL; // Hyperlink URL string// Generated message map functions
protected://{{AFX_MSG(CHyperLink)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg void OnPaint();afx_msg LRESULT OnMouseHover(WPARAM wParam,LPARAM lParam);afx_msg LRESULT OnMouseLeave(WPARAM wParam,LPARAM lParam);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);protected:DECLARE_MESSAGE_MAP()
};
然后是HyperLink.cpp文件:
// HyperLink.cpp : 實(shí)現(xiàn)文件
//#include "stdafx.h"
#include "MyPlayer.h"
#include "HyperLink.h"
//上面三個(gè)頭文件利用類(lèi)向?qū)?chuàng)建的話(huà)會(huì)自動(dòng)生成,如果是手動(dòng)創(chuàng)建,需要添加// CHyperLinkIMPLEMENT_DYNAMIC(CHyperLink, CStatic)CHyperLink::CHyperLink()
{m_bHot = FALSE; // Control doesn't own the focus yetm_strURL.Empty(); // Set URL to an empty stringm_clrHot = RGB(0,0,255);m_clrNor = RGB(0,0,255);m_clrBG = RGB(240,240,240);
}CHyperLink::~CHyperLink()
{
}BEGIN_MESSAGE_MAP(CHyperLink, CStatic)//{{AFX_MSG_MAP(CHyperLink)ON_WM_MOUSEMOVE()ON_WM_LBUTTONUP()ON_WM_PAINT()ON_MESSAGE(WM_MOUSEHOVER,&OnMouseHover)ON_MESSAGE(WM_MOUSELEAVE,&OnMouseLeave)ON_WM_SETCURSOR()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////void CHyperLink::PreSubclassWindow() {// TODO: Add your specialized code here and/or call the base classDWORD dwStyle = GetStyle();::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);// SetFont(GetParent()->GetFont());CStatic::PreSubclassWindow();}void CHyperLink::OnMouseMove(UINT nFlags, CPoint point) {TRACKMOUSEEVENT tme; tme.cbSize = sizeof(tme); tme.hwndTrack = m_hWnd; tme.dwFlags = TME_LEAVE | TME_HOVER; tme.dwHoverTime = 1; _TrackMouseEvent(&tme); // CStatic::OnMouseMove(nFlags, point);}//鼠標(biāo)在上面LRESULT CHyperLink::OnMouseHover(WPARAM wParam,LPARAM lParam){ if (!m_bHot){m_bHot = TRUE; Invalidate();} return TRUE;}//鼠標(biāo)離開(kāi)LRESULT CHyperLink::OnMouseLeave(WPARAM wParam,LPARAM lParam){m_bHot = FALSE;Invalidate();return TRUE;}void CHyperLink::OnLButtonUp(UINT nFlags, CPoint point) {if (m_strURL.IsEmpty()){GetWindowText(m_strURL);}GotoURL(m_strURL, SW_SHOW);}/////////////////////////////////////////////////////////////////////////////// CHyperLink operationsvoid CHyperLink::SetURL(CString strURL){m_strURL = strURL;}CString CHyperLink::GetURL() const { return m_strURL; }int CHyperLink::GotoURL(LPCTSTR url, int showcmd){HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd); return (int)result;}void CHyperLink::OnPaint() {CPaintDC dc(this); // device context for painting // TODO: Add your message handler code hereCFont* pFont = GetFont();CFont m_Font;if (pFont != NULL){LOGFONT lf;pFont->GetLogFont(&lf);lf.lfUnderline = m_bHot;if (m_Font.CreateFontIndirect(&lf))dc.SelectObject(m_Font);}if (m_bHot){ dc.SetTextColor(m_clrHot);}else{dc.SetTextColor(m_clrNor);}dc.SetBkMode(TRANSPARENT);///準(zhǔn)備工作CRect rect;CBrush BGBrush,*pOldBrush;int nTextLeft=4,nTextTop=2; //文字輸出的位置this->GetClientRect(&rect);//畫(huà)背景BGBrush.CreateSolidBrush(m_clrBG);pOldBrush=dc.SelectObject(&BGBrush);dc.FillRect(&rect,&BGBrush);dc.SelectObject(pOldBrush);BGBrush.DeleteObject();///輸出文字TEXTMETRIC tm;dc.GetTextMetrics(&tm);CString strText;this->GetWindowText(strText);nTextTop=rect.top+(rect.Height()-tm.tmHeight)/2;if(strText.GetLength()>0){dc.TextOut(nTextLeft,nTextTop,strText);} BGBrush.DeleteObject();m_Font.DeleteObject();}BOOL CHyperLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) {// TODO: Add your message handler code here and/or call defaultif (m_bHot){::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(32649)));return TRUE;}return CStatic::OnSetCursor(pWnd, nHitTest, message);}
3、第2步可以直接使用,不用修改。然后真正使用超鏈接的是后面幾步。在你的父窗口類(lèi)中添加成員變量:
private:CHyperLink m_stWeb; //超鏈接至我的博客
4、這一步比較關(guān)鍵,因?yàn)槲覀兪鞘謩?dòng)創(chuàng)建的變量,需要將m_stWeb和第一步的靜態(tài)控件關(guān)聯(lián)。具體在
void CMyPlayerDlg::DoDataExchange(CDataExchange* pDX)
{//....//IDC_STATIC_blog是該靜態(tài)文本控件的IDDDX_Control(pDX, IDC_STATIC_blog, m_stWeb);
}
5、最后一步,在Oninitdialog函數(shù)中添加:
m_stWeb.SetURL("http://blog.csdn.net/u013051748");
//修改成你自己的超鏈接地址
ok,到這里運(yùn)行就能發(fā)現(xiàn),當(dāng)鼠標(biāo)移動(dòng)到文字上時(shí),文字具有下劃線(xiàn),同時(shí)鼠標(biāo)變成手型,點(diǎn)擊之后系統(tǒng)會(huì)調(diào)用默認(rèn)的瀏覽器打開(kāi)該鏈接。仔細(xì)觀(guān)察你會(huì)發(fā)現(xiàn),執(zhí)行打開(kāi)的函數(shù)依然是ShellExecute(NULL, _T(“open”), url, NULL,NULL, showcmd);這個(gè)函數(shù)windows版本的exec函數(shù)。
到這里就能成功實(shí)現(xiàn)文本的超鏈接了。
如下:
到現(xiàn)在基本是沒(méi)什么事了,就剩下寫(xiě)寫(xiě)論文了,所以沒(méi)事也就自己再添加一些功能,DIY一下什么的,比如上面的超鏈接。
拙見(jiàn),小記!
總結(jié)
以上是生活随笔為你收集整理的MFC静态文本控件设置超链接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MFC按钮添加提示文字
- 下一篇: MFC系统托盘的实现