MFC 超级链接的控件类

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

 

#pragma  once


//  HyperLink

class  HyperLink :  public  CStatic
{
    DECLARE_DYNAMIC(HyperLink)

public:
    HyperLink();
    
virtual ~HyperLink();

protected:
    DECLARE_MESSAGE_MAP()

protected:

    afx_msg 
int OnCreate(LPCREATESTRUCT lpCreateStruct);

    afx_msg 
void OnMouseMove(UINT nFlags, CPoint point);

    afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);

    afx_msg 
void OnLButtonDown(UINT nFlags, CPoint point);

    afx_msg 
void OnPaint();
    
protected:

    
virtual void PreSubclassWindow();

public:

    
void SetLabel(CString text);

    CString GetLabel()
const;

    
void SetLink(CString link);;

    CString GetLink()
const;

private:

    CFont m_linkFont;

    CFont m_onLinkFont;

    
bool m_isOnLink;

    
bool m_isEnter;

    CString m_label;

    CString m_link;
}
;


// 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值