MFC静态文本超链件

VC++ 专栏收录该内容
34 篇文章 0 订阅
#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 color
	COLORREF m_clrNor;      // Link active color
	COLORREF m_clrBG;       // Link active color
	BOOL     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 yet
	m_strURL.Empty();               // Set URL to an empty string
	m_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 here
	CFont* 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 default
	if (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");

 

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:黑客帝国 设计师:我叫白小胖 返回首页

打赏作者

墨痕诉清风

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值