MFC静态文本控件设置超链接
有時我們需要在窗口上設置一個超鏈接,比如在Aboutdlg上設置“我的博客”這樣的超鏈接.具體的設置方法如下。
1、首先我們在窗體上添加一個Static文本控件,修改Caption屬性,設置成你想要的超鏈接標題,比如“更多內容歡迎訪問小夢的博客”等。
2、這步很重要。由于Static控件不具備設置超鏈接的能力,我們需要重寫一個繼承自CStatic的類CHyperLink.
這里可以直接復制兩個文件到你的工程當中。
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);//設置顏色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 : 實現文件
//#include "stdafx.h"
#include "MyPlayer.h"
#include "HyperLink.h"
//上面三個頭文件利用類向導創建的話會自動生成,如果是手動創建,需要添加// 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);}//鼠標在上面LRESULT CHyperLink::OnMouseHover(WPARAM wParam,LPARAM lParam){ if (!m_bHot){m_bHot = TRUE; Invalidate();} return TRUE;}//鼠標離開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);///準備工作CRect rect;CBrush BGBrush,*pOldBrush;int nTextLeft=4,nTextTop=2; //文字輸出的位置this->GetClientRect(&rect);//畫背景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步可以直接使用,不用修改。然后真正使用超鏈接的是后面幾步。在你的父窗口類中添加成員變量:
private:CHyperLink m_stWeb; //超鏈接至我的博客
4、這一步比較關鍵,因為我們是手動創建的變量,需要將m_stWeb和第一步的靜態控件關聯。具體在
void CMyPlayerDlg::DoDataExchange(CDataExchange* pDX)
{//....//IDC_STATIC_blog是該靜態文本控件的IDDDX_Control(pDX, IDC_STATIC_blog, m_stWeb);
}
5、最后一步,在Oninitdialog函數中添加:
m_stWeb.SetURL("http://blog.csdn.net/u013051748");
//修改成你自己的超鏈接地址
ok,到這里運行就能發現,當鼠標移動到文字上時,文字具有下劃線,同時鼠標變成手型,點擊之后系統會調用默認的瀏覽器打開該鏈接。仔細觀察你會發現,執行打開的函數依然是ShellExecute(NULL, _T(“open”), url, NULL,NULL, showcmd);這個函數windows版本的exec函數。
到這里就能成功實現文本的超鏈接了。
如下:
到現在基本是沒什么事了,就剩下寫寫論文了,所以沒事也就自己再添加一些功能,DIY一下什么的,比如上面的超鏈接。
拙見,小記!
總結
以上是生活随笔為你收集整理的MFC静态文本控件设置超链接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC按钮添加提示文字
- 下一篇: MFC系统托盘的实现