字体下划线按钮
#include "stdafx.h" #include "UnderLineTextButton.h" IMPLEMENT_DYNAMIC(CUnderLineTextButton, CButton) CUnderLineTextButton::CUnderLineTextButton() { m_bgColor = RGB(13, 17, 23); m_fontColor = RGB(255, 255, 255); m_hoverFontColor = RGB(255, 255, 255); m_disableFontColor = RGB(170, 170, 170); m_pressDownFontColor = RGB(34, 186, 251); m_ButtonState = UnderLineButtonState::State_Normal; m_bTracking = FALSE; } CUnderLineTextButton::~CUnderLineTextButton() { } BEGIN_MESSAGE_MAP(CUnderLineTextButton, CButton) ON_WM_MOUSEMOVE() ON_WM_MOUSELEAVE() ON_WM_MOUSEHOVER() ON_WM_ENABLE() ON_WM_SETCURSOR() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() END_MESSAGE_MAP() void CUnderLineTextButton::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;//光标停在按钮上,改变状态的时间,以1毫秒为单位 m_bTracking = _TrackMouseEvent(&tme); } CButton::OnMouseMove(nFlags, point); } void CUnderLineTextButton::OnMouseLeave() { m_ButtonState = UnderLineButtonState::State_Normal; m_bTracking = FALSE; CButton::OnMouseLeave(); Invalidate(); } void CUnderLineTextButton::OnMouseHover(UINT nFlags, CPoint point) { m_ButtonState = UnderLineButtonState::State_Hover; m_bTracking = FALSE; CButton::OnMouseHover(nFlags, point); Invalidate(); } void CUnderLineTextButton::PreSubclassWindow() { // TODO: 在此添加专用代码和/或调用基类 /* CButton::PreSubclassWindow();*/ ModifyStyle(0, BS_OWNERDRAW); CFont* pFont = GetFont(); LOGFONT logFont; pFont->GetLogFont(&logFont); logFont.lfHeight = 24; m_normalFont.CreateFontIndirect(&logFont); //创建下划线字体 logFont.lfUnderline = TRUE; m_underlineFont.CreateFontIndirect(&logFont); CButton::PreSubclassWindow(); } void CUnderLineTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); GetWindowText(m_strText); int nSavedDC = pDC->SaveDC(); CRect buttonRect = lpDrawItemStruct->rcItem; CBrush Brush; Brush.CreateSolidBrush(m_bgColor); pDC->FillRect(buttonRect, &Brush); switch (m_ButtonState) { case UnderLineButtonState::State_Hover: pDC->SelectObject(&m_underlineFont); pDC->SetTextColor(m_hoverFontColor); break; case UnderLineButtonState::State_Disable: pDC->SelectObject(&m_normalFont); pDC->SetTextColor(m_disableFontColor); break; case UnderLineButtonState::State_PressDown: pDC->SelectObject(&m_underlineFont); pDC->SetTextColor(m_pressDownFontColor); break; default: pDC->SelectObject(&m_normalFont); pDC->SetTextColor(m_fontColor); break; } pDC->SetBkMode(TRANSPARENT); pDC->DrawText(m_strText, buttonRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); pDC->RestoreDC(nSavedDC); } void CUnderLineTextButton::OnEnable(BOOL bEnable) { if (!bEnable) { m_ButtonState = UnderLineButtonState::State_Disable; } else { m_ButtonState = UnderLineButtonState::State_Normal; } CButton::OnEnable(bEnable); Invalidate(); } BOOL CUnderLineTextButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { ::SetCursor(::LoadCursor(NULL, IDC_HAND)); return TRUE; } void CUnderLineTextButton::OnLButtonDown(UINT nFlags, CPoint point) { m_ButtonState = UnderLineButtonState::State_PressDown; CButton::OnLButtonDown(nFlags, point); } void CUnderLineTextButton::OnLButtonUp(UINT nFlags, CPoint point) { CButton::OnLButtonUp(nFlags, point); }
赌上我的人生为梦想,即使是臭名远扬,也要我的名字响彻天堂
浙公网安备 33010602011771号