MFC静态文本超链件
生活随笔
收集整理的這篇文章主要介紹了
MFC静态文本超链件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#pragma once
#include "afxwin.h"
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);//得到顏色
protected:virtual void PreSubclassWindow();// 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()
};#include "stdafx.h"
#include "HyperLink.h"IMPLEMENT_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_MAP
END_MESSAGE_MAP()/
void CHyperLink::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
DWORD 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);
}void 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);
}CHyperLink m_stWebOne;void CSstemPatchDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_STATIC_ONE, m_stWebOne);DDX_Control(pDX, IDC_STATIC_TWO, m_stWebTwo);}m_stWebOne.SetURL(L"https://docs.microsoft.com/zh-cn/security-updates/Securitybulletins/2017/ms17-010");
?
總結
以上是生活随笔為你收集整理的MFC静态文本超链件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Docker快速搭建生产环境
- 下一篇: 十二、程序返回、数据类型表示、代码注释