zoukankan      html  css  js  c++  java
  • 字体下划线按钮

    #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);
    }

    下载地址

  • 相关阅读:
    SQL Server 添加数据库没有权限等
    网站图片优化的重要性与技巧方案
    5年前端经验小伙伴教你纯css3实现饼状图
    css3 斜切角/斜边的实现方式来自BAT大神的出品
    Validate表单验证插件之常用参数介绍
    html实现邮箱发送邮件_js发送邮件至指定邮箱功能
    css重设样式_清除浏览器的默认样式
    大厂前端工程师教你如何使用css3绘制任意角度扇形+动画
    WordPress教程之如何批量删除未引用(无用)的TAG标签
    css引入的方式有哪些_四种css的引入方式与特点
  • 原文地址:https://www.cnblogs.com/ye-ming/p/8796408.html
Copyright © 2011-2022 走看看