MFC下的链接按钮呀

来源:互联网 发布:二叉树反转python 编辑:程序博客网 时间:2024/06/05 05:16

用来点击可以跳转连接的按钮,大家可以参考参考!

#pragma once// CLinkButtonclass CLinkButton : public CButton{private://各种颜色COLORREF m_normalTextColor;//按钮文本颜色COLORREF m_overTextColor;//鼠标位于按钮之上的文本颜色CFont m_textFont;//按钮文本字体CString m_strLink;//链接地址//按钮的状态bool m_bOver;//鼠标位于按钮之上时该值为true,反之为flasebool m_bTracking;//在鼠标按下没有释放时该值为trueDECLARE_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"// CNormalButtonIMPLEMENT_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);}



0 0
原创粉丝点击