www.gusucode.com > VC++实现Vista玻璃窗体程序 > VC++实现Vista玻璃窗体程序/gusucode/Vista玻璃窗体程序文件/PngButton.cpp

    // PngButton.cpp : implementation file
//

#include "stdafx.h"
//#include "testbutton.h"
#include "PngButton.h"
//#include ".pngbutton.h"


// PngButton

IMPLEMENT_DYNAMIC(PngButton, CButton)
PngButton::PngButton()
{
    m_bDisable            = FALSE;
    m_bCursorOnButton    = FALSE;
    m_bPress            = FALSE;
}

PngButton::~PngButton()
{
}


BEGIN_MESSAGE_MAP(PngButton, CButton)
    ON_WM_MOUSEMOVE()
    ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover)
    ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()



// PngButton message handlers


void PngButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct -> hDC);

    Graphics graphics(pDC -> m_hDC);

    if( m_bDisable == TRUE )    
    {
        ;
        graphics.DrawImage(m_btnImage,Rect(0,0,m_nSliceWidth,m_nHeight),
            BUTTON_DISABLE * m_nSliceWidth,0,m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL);
    }

    else
    {
        // click state
        if( lpDrawItemStruct -> itemState & ODS_SELECTED )
        {
            
            
            graphics.DrawImage (m_btnImage,Rect( 0  ,0 , m_nSliceWidth , m_nHeight),
                BUTTON_CLICK * m_nSliceWidth , 0  , m_nSliceWidth , m_nHeight ,UnitPixel,NULL,NULL,NULL);
        }

        // hover state
        else if ( m_bPress)
        {
            
            graphics.DrawImage(m_btnImage, Rect( 0, 0, m_nSliceWidth, m_nHeight),
                BUTTON_HOVER * m_nSliceWidth,0, m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL);

            
        }

        // enable state
        else
        {
            
            graphics.DrawImage(m_btnImage,Rect( 0, 0,m_nSliceWidth,m_nHeight),
                BUTTON_ENABLE*m_nSliceWidth,0, m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL);
        }
    }


    // TODO:  Add your code to draw the specified item
}

void PngButton::SetButtonImage(WCHAR* str)
{
    
    m_btnImage = new Bitmap(str);//??BITMAP??
    

    m_nWidth = m_btnImage -> GetWidth();
    m_nHeight = m_btnImage -> GetHeight();

    m_nSliceWidth = m_nWidth/4;   //??????

    CWnd *pWnd = this -> GetParent();
    GetWindowRect( &m_rectButton );
    pWnd -> ScreenToClient(m_rectButton);
    m_rectButton.right    = m_rectButton.left + m_nSliceWidth;
    m_rectButton.bottom    = m_rectButton.top  + m_nHeight;
    MoveWindow(m_rectButton);      //???????????
}

void PngButton::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    if( m_bCursorOnButton == FALSE )
    {
        TRACKMOUSEEVENT tme;
        ZeroMemory(&tme,sizeof(TRACKMOUSEEVENT));
        tme.cbSize = sizeof(tme);
        tme.hwndTrack = m_hWnd;
        tme.dwFlags = TME_LEAVE|TME_HOVER;
        tme.dwHoverTime = 1;
        m_bCursorOnButton = _TrackMouseEvent(&tme);
    }

    CButton::OnMouseMove(nFlags, point);
}

LRESULT PngButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
    m_bCursorOnButton    = FALSE;
    m_bPress    = FALSE;

    Invalidate();
    return 0L;
}

LRESULT PngButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
{
    m_bPress = TRUE;

    Invalidate();
    return 0L;
}


void PngButton::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    Invalidate();

    CButton::OnLButtonDown(nFlags, point);
}

void PngButton::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    Invalidate();
    CButton::OnLButtonUp(nFlags, point);
}

LRESULT PngButton::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    // TODO: Add your specialized code here and/or call the base class
    if (message == WM_LBUTTONDBLCLK)
    {
        message = WM_LBUTTONDOWN;
    }
    return CButton::DefWindowProc(message, wParam, lParam);//???,???????
}

BOOL PngButton::OnEraseBkgnd(CDC* pDC)
{
    // TODO: Add your message handler code here and/or call default

    //return CButton::OnEraseBkgnd(pDC);
    return true;
}