超级链接控件
使用步骤:
1.在对话框中扔一个Static Text,改一下ID号
2.右键选此控件,添加成员函数
3.把成员函数的类名(CStatic)改为HyperLink。
4.把这个控件的notify属性改为true
5.接口介绍
SetLabel 设置显示的文字
SetLink 设置连接到的地址或者文件
还有相应的Get方法
这些属性可以运行时设定。




























































// HyperLink.cpp : implementation file
//
#include "stdafx.h"
#include "HyperLink.h"
// HyperLink
IMPLEMENT_DYNAMIC(HyperLink, CStatic)
HyperLink::HyperLink()
{
m_isOnLink = false;
m_isEnter = false;
}
HyperLink::~HyperLink()
{
}
BEGIN_MESSAGE_MAP(HyperLink, CStatic)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
// HyperLink message handlers
int HyperLink::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
void HyperLink::SetLabel( CString text )
{
m_label = text;
RedrawWindow();
}
void HyperLink::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
m_linkFont.CreateFont(
14, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
TRUE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFacename
m_onLinkFont.CreateFont(
14, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight
TRUE, // bItalic
TRUE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFacename
GetWindowText(m_label);
CStatic::PreSubclassWindow();
}
void HyperLink::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if ( m_link.Trim().Compare(_T("")) != 0)
{
::ShellExecute(NULL, _T("open"), m_link, NULL, NULL, SW_SHOWNORMAL);
}
CStatic::OnLButtonDown(nFlags, point);
}
void HyperLink::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CWnd* pParentWnd = GetParent();
CClientDC cdc(pParentWnd);
CBrush bkBrush(cdc.GetPixel(0, 0));
CRect rect;
GetClientRect(rect);
dc.FillRect(rect, &bkBrush);
dc.SetTextColor(RGB(0,0,255));
dc.SetBkMode(TRANSPARENT);
CFont* adjustFont = NULL;
if (m_isOnLink)
{
adjustFont = &m_onLinkFont;
}
else
{
adjustFont = &m_linkFont;
}
HGDIOBJ oldFont = dc.SelectObject(adjustFont);
dc.TextOut(0, 0, m_label);
dc.SelectObject(oldFont);
// Do not call CStatic::OnPaint() for painting messages
}
void HyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
//监控鼠标离开
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
if (::_TrackMouseEvent(&tme))
{
}
m_isOnLink = true;
if (m_isEnter == false)
{
m_isEnter = true;
RedrawWindow();
}
//Invalidate(FALSE);
//RedrawWindow();
CStatic::OnMouseMove(nFlags, point);
}
LRESULT HyperLink::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_isOnLink = false;
m_isEnter = false;
RedrawWindow();
return TRUE;
}
void HyperLink::SetLink( CString link )
{
m_link = link;
}
CString HyperLink::GetLabel() const
{
return m_label;
}
CString HyperLink::GetLink() const
{
return m_link;
}