用来点击可以跳转连接的按钮,大家可以参考参考!
#pragma once
// CLinkButton
class CLinkButton : public CButton
{
private:
//各种颜色
COLORREF m_normalTextColor;//按钮文本颜色
COLORREF m_overTextColor;//鼠标位于按钮之上的文本颜色
CFont m_textFont;//按钮文本字体
CString m_strLink;//链接地址
//按钮的状态
bool m_bOver; //鼠标位于按钮之上时该值为true,反之为flase
bool m_bTracking; //在鼠标按下没有释放时该值为true
DECLARE_DYNAMIC(CLinkButton)
public:
CLinkButton();
virtual ~CLinkButton();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);//设置光标
afx_msg BOOL OnEraseBkgnd(CDC* pDC);//去除背景颜色
virtual void PreSubclassWindow();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
void SetLink(CString link);
void AdjustPos();//根据文字大小修改按钮大小
};
// NormalButton.cpp : 实现文件
//
#include "stdafx.h"
#include "LinkButton.h"
// CNormalButton
IMPLEMENT_DYNAMIC(CLinkButton, CButton)
CLinkButton::CLinkButton()
{
m_normalTextColor = RGB(109, 174, 240);
m_overTextColor = RGB(255, 0 , 0);
m_textFont.CreatePointFont(100, _T("宋体"));
m_strLink = _T("http://www.baidu.com");
m_bOver = false;
m_bTracking = false;
}
CLinkButton::~CLinkButton()
{
m_textFont.DeleteObject();
}
BEGIN_MESSAGE_MAP(CLinkButton, CButton)
ON_WM_MOUSEHOVER()
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CNormalButton 消息处理程序
BOOL CLinkButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//设置鼠标移动到按钮上的光标
::SetCursor(LoadCursor(NULL, IDC_HAND));
return true;
//return CButton::OnSetCursor(pWnd, nHitTest, message);
}
void CLinkButton::OnMouseHover(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bOver = true;
//设置鼠标样式
InvalidateRect(NULL);
CButton::OnMouseHover(nFlags, point);
}
void CLinkButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 1;
m_bTracking = _TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}
void CLinkButton::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bOver = false;
m_bTracking = false;
InvalidateRect(NULL, FALSE);
CButton::OnMouseLeave();
}
void CLinkButton::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}
BOOL CLinkButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//修改按钮大小
AdjustPos();
return true;
//return CButton::OnEraseBkgnd(pDC);
}
void CLinkButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
//从lpDrawItemStruct获取控件的相关信息
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
int nSaveDC=pDC->SaveDC();
UINT state = lpDrawItemStruct->itemState;
TCHAR strText[MAX_PATH + 1];
::GetWindowText(m_hWnd, strText, MAX_PATH);
//根据按钮的状态画出按钮
COLORREF nowTextColor;//当前的文本颜色
if (m_bOver)
{
nowTextColor = m_overTextColor;
}
else
{
nowTextColor = m_normalTextColor;
}
//显示按钮的文本
if (strText != NULL)
{
CFont* hOldFont = pDC->SelectObject(&m_textFont);
pDC->SetBkMode(TRANSPARENT);//设置使用透明方式输出,防止破坏背景
CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
pDC->SetTextColor(nowTextColor);//设置文字颜色
pDC->TextOutW(pt.x, pt.y, strText, lstrlen(strText));
pDC->SelectObject(hOldFont);
}
pDC->RestoreDC(nSaveDC);
}
void CLinkButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//打开链接
ShellExecute ( NULL, NULL, m_strLink, NULL, NULL, SW_NORMAL );
CButton::OnLButtonDown(nFlags, point);
}
void CLinkButton::SetLink(CString link)
{
m_strLink = link;
}
void CLinkButton::AdjustPos()
{
//修改按钮大小
CString str;
GetWindowText(str);
CDC *pDC = GetDC();
CSize szExtent = pDC->GetTextExtent(str, lstrlen(str));
ReleaseDC(pDC);
CRect rect;
GetWindowRect(&rect);
CWnd* parent = GetParent();
if (parent)
{
parent->ScreenToClient(&rect);
}
SetWindowPos(NULL, rect.left, rect.top, szExtent.cx, szExtent.cy, SWP_NOZORDER);
}