QButton

Button.h

#pragma once


// QButton

class QButton : public CButton
{
    DECLARE_DYNAMIC(QButton)
// Construction
public:
    QButton();
    
//Attributes
protected:
    //The outside border of the button
    CPen m_penBoundry;

    //When the mouse pointer above button button inside the frame
    CPen m_penInsideBoundryLeft;
    CPen m_penInsideBoundryRight;
    CPen m_penInsideBoundryTop;
    CPen m_penInsideBoundryBottom;

    //Button gains focus when the button on the borders
    CPen m_penInsideBoundryLeftSel;
    CPen m_penInsideBoundryRightSel;
    CPen m_penInsideBoundryTopSel;
    CPen m_penInsideBoundryBottomSel;

    //Button's background, including two kinds of valid and invalid
    CBrush m_brFillActive;
    CBrush m_brFillInactive;

    //Button status
    BOOL m_bOver; //This value to true when the mouse in the button above, instead of flase
    BOOL m_bTracking;//In the mouse press not to release the value to true
    BOOL m_bSelected;//Button is pressed the value is true
    BOOL m_bFocus;//Button for the current focus when the value is true

    CFont m_font;

//Overridables
public:
    virtual void PreSubclassWindow();
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual ~QButton();

// Implementation
protected:
    virtual void DoGradientFill(CDC *pDC, CRect* rect);
    virtual void DrawInsideBorder(CDC *pDC, CRect* rect);
    

// Generated message map functions
protected:
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);

    DECLARE_MESSAGE_MAP()
    
};

Button.cpp

// Button.cpp
//

#include "stdafx.h"
#include "Button.h"


// QButton

IMPLEMENT_DYNAMIC(QButton, CButton)

QButton::QButton()
{
    //The outside border of the button
    m_penBoundry.CreatePen(PS_INSIDEFRAME | PS_SOLID, 1, RGB(165,166,168));
    
    //When the mouse pointer above button button inside the frame
    m_penInsideBoundryLeft.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(250, 196, 88));
    m_penInsideBoundryRight.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(251, 202, 106));
    m_penInsideBoundryTop.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(252, 210, 121));
    m_penInsideBoundryBottom.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(229, 151, 0));

    //Button gains focus when the button on the borders
    m_penInsideBoundryLeftSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(153, 198, 252));
    m_penInsideBoundryRightSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(162, 201, 255));
    m_penInsideBoundryTopSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(162, 189, 252));
    m_penInsideBoundryBottomSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(162, 201, 255));

    //Button's background, including two kinds of valid and invalid
    m_brFillActive.CreateSolidBrush(RGB(223, 222, 236));
    m_brFillInactive.CreateSolidBrush(RGB(222, 223, 236));

    //Button status
    m_bOver = m_bSelected = m_bTracking = m_bFocus = FALSE;

    m_font.CreateFont(
        15,                        // nHeight
        0,                         // nWidth
        0,                         // nEscapement
        0,                         // nOrientation
        FW_MEDIUM,                   // nWeight
        FALSE,                     // bItalic
        FALSE,                     // 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
}

QButton::~QButton()
{
    m_penBoundry.DeleteObject();

    m_penInsideBoundryLeft.DeleteObject();
    m_penInsideBoundryRight.DeleteObject();
    m_penInsideBoundryTop.DeleteObject();
    m_penInsideBoundryBottom.DeleteObject();

    m_penInsideBoundryLeftSel.DeleteObject();
    m_penInsideBoundryRightSel.DeleteObject();
    m_penInsideBoundryTopSel.DeleteObject();
    m_penInsideBoundryBottomSel.DeleteObject();

    m_brFillActive.DeleteObject();
    m_brFillInactive.DeleteObject();

    m_font.DeleteObject();
}

BEGIN_MESSAGE_MAP(QButton, CButton)
    ON_WM_MOUSEMOVE()
    ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
    ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
END_MESSAGE_MAP()

void QButton::OnMouseMove(UINT nFlags, CPoint point)
{
    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);
}

LRESULT QButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
    m_bOver = FALSE;
    m_bTracking = FALSE;
    InvalidateRect(NULL, FALSE); //parm2:FALSE Heavy paintings don't erase the background
    //InvalidateRect to call DrawItem
    return 0;
}

LRESULT QButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
    m_bOver = TRUE;
    InvalidateRect(NULL); //parm2:TRUE Again when will erase the background.
    return 0;
}

//Add the Owner Draw properties
void QButton::PreSubclassWindow()
{
    CButton::PreSubclassWindow();
    ModifyStyle(0, BS_OWNERDRAW);
}

void QButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    //Information obtained from lpDrawItemStruct controls
    CRect rect =  lpDrawItemStruct->rcItem;
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    int nSaveDC = pDC->SaveDC();
    UINT state = lpDrawItemStruct->itemState;
    POINT pt ;
    TCHAR strText[MAX_PATH + 1];
    ::GetWindowText(m_hWnd, strText, MAX_PATH);

    //The outside border of the draw button, it is a radius of 5 of the rounded rectangle
    pt.x = 8;
    pt.y = 8;
    CPen* hOldPen = pDC->SelectObject(&m_penBoundry);
    pDC->RoundRect(&rect, pt);

    //Access to state of the button
    if (state & ODS_FOCUS)
    {
        m_bFocus = TRUE;
        m_bSelected = TRUE;
    }
    else
    {
        m_bFocus = FALSE;
        m_bSelected = FALSE;
    }

    if (state & ODS_SELECTED || state & ODS_DEFAULT)
    {
        m_bFocus = TRUE;
    }

    pDC->SelectObject(hOldPen);

    rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));

    //According to the status of button fill color button
    CBrush* pOldBrush;
    if (m_bOver)
    {
        pOldBrush = pDC->SelectObject(&m_brFillActive);
        DoGradientFill(pDC, &rect);
    }
    else
    {
        pOldBrush = pDC->SelectObject(&m_brFillInactive);
        DoGradientFill(pDC, &rect);
    }
    
    //According to the button state to plot borders
    if (m_bOver || m_bSelected)
        DrawInsideBorder(pDC, &rect);
    
    pDC->SelectObject(pOldBrush);

    //Displays the text button
    if (strText!=NULL)
    {
        //CFont* hFont = GetFont();
        CFont* hOldFont = pDC->SelectObject(&m_font);
        CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
        CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
        if (state & ODS_SELECTED) 
            pt.Offset(1, 1);
        int nMode = pDC->SetBkMode(TRANSPARENT);
        if (state & ODS_DISABLED)
            pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
        else
            pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
        pDC->SelectObject(hOldFont);
        pDC->SetBkMode(nMode);
    }

    pDC->RestoreDC(nSaveDC);
}

//The color of button
void QButton::DoGradientFill(CDC *pDC, CRect* rect)
{
    #define QBUTTON_WIDTH 64
    CBrush brBk[QBUTTON_WIDTH];
    int nWidth = rect->Width();
    int nHeight = rect->Height();
    CRect rct;
    int i;
    for(i = 0; i < QBUTTON_WIDTH; i++)
    {
        if(m_bOver)
        {
            if(m_bFocus)
                brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 3)));
            else
                brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 5)));
        }
        else
        {
            if(m_bFocus)
                brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 4)));
            else
                brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 5)));
        }
    }

    for(i = rect->top; i <= nHeight + 2; i++)
    {
        rct.SetRect(rect->left, i, nWidth + 2, i + 1);
        pDC->FillRect(&rct, &brBk[((i * 63) / nHeight)]);
    }

    for(i = 0; i < QBUTTON_WIDTH; i++)
        brBk[i].DeleteObject();
}

//Within the draw button on the borders
void QButton::DrawInsideBorder(CDC *pDC, CRect* rect)
{
    CPen *pLeft, *pRight, *pTop, *pBottom;

    if(m_bSelected && !m_bOver)
    {
        pLeft = &m_penInsideBoundryLeftSel;
        pRight = &m_penInsideBoundryRightSel;
        pTop = &m_penInsideBoundryTopSel;
        pBottom = &m_penInsideBoundryBottomSel;
    }
    else
    {
        pLeft = &m_penInsideBoundryLeft;
        pRight = &m_penInsideBoundryRight;
        pTop = &m_penInsideBoundryTop;
        pBottom = &m_penInsideBoundryBottom;
    }

    CPoint oldPoint = pDC->MoveTo(rect->left, rect->bottom - 1);
    CPen *pOldPen = pDC->SelectObject(pLeft);
    pDC->LineTo(rect->left, rect->top + 1);
    pDC->SelectObject(pRight);
    pDC->MoveTo(rect->right - 1, rect->bottom - 1);
    pDC->LineTo(rect->right - 1, rect->top);
    pDC->SelectObject(pTop);
    pDC->MoveTo(rect->left - 1, rect->top);
    pDC->LineTo(rect->right - 1, rect->top);
    pDC->SelectObject(pBottom);
    pDC->MoveTo(rect->left, rect->bottom);
    pDC->LineTo(rect->right - 1, rect->bottom);
    pDC->SelectObject(pOldPen);
    pDC->MoveTo(oldPoint);

    if(m_bSelected && !m_bOver)
        ::DrawFocusRect(pDC->m_hDC, rect);
}
原文地址:https://www.cnblogs.com/qintangtao/p/3198442.html