www.pudn.com > ButtonDemo.rar > XPButton.cpp
#include#include #include "stdafx.h" #include "XPButton.h" //#if(WINVER >= 0x0501) #ifdef THEMEAPI #include #else #define NO_THEMEAPI_FOUND #define BP_PUSHBUTTON 0x00000001 #define PBS_NORMAL 0x00000001 #define PBS_HOT 0x00000002 #define PBS_PRESSED 0x00000003 #define PBS_DISABLED 0x00000004 #define PBS_DEFAULTED 0x00000005 #endif #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CXPButton::CXPButton() { m_bIsXPStyle = TRUE; } CXPButton::~CXPButton() { } // This function is called every time the button border needs to be painted. // If the button is in standard (not flat) mode this function will NOT be called. // This is a virtual function that can be rewritten in CMyButton-derived classes // to produce a whole range of buttons not available by default. // // Parameters: // [IN] pDC // Pointer to a CDC object that indicates the device context. // [IN] pRect // Pointer to a CRect object that indicates the bounds of the // area to be painted. // // Return value: // BTNST_OK // Function executed successfully. // DWORD CXPButton::OnDrawBorder(CDC* /*pDC*/, LPCRECT /*pRect*/) { return BTNST_OK; } // End of OnDrawBorder // This function is called every time the button background needs to be painted. // If the button is in transparent mode this function will NOT be called. // This is a virtual function that can be rewritten in CMyButton-derived classes // to produce a whole range of buttons not available by default. // // Parameters: // [IN] pDC // Pointer to a CDC object that indicates the device context. // [IN] pRect // Pointer to a CRect object that indicates the bounds of the // area to be painted. // // Return value: // BTNST_OK // Function executed successfully. // DWORD CXPButton::OnDrawBackground(CDC* pDC, LPCRECT pRect) { BOOL bDefaultDraw = FALSE; CMemDC dc( pDC ); PaintBk(&dc); // No theme helper passed if (m_pTheme == NULL || m_pTheme->IsAppThemed() == FALSE) { bDefaultDraw = TRUE; } // if else { HTHEME hTheme = NULL; int iStateId = 0; hTheme = m_pTheme->OpenThemeData(GetSafeHwnd(), L"BUTTON"); if (hTheme) { iStateId = PBS_NORMAL; // Normal if (m_bIsDefault) iStateId = PBS_DEFAULTED; // Default button if (m_bMouseOnButton) iStateId = PBS_HOT; // Hot if (m_bIsPressed) iStateId = PBS_PRESSED; // Pressed if (m_bIsDisabled) iStateId = PBS_DISABLED; // Disabled m_pTheme->DrawThemeBackground(hTheme, dc.GetSafeHdc(), BP_PUSHBUTTON, iStateId, pRect, NULL); if ( m_bIsFocused ) { CRect rc = pRect; rc.DeflateRect( 3, 3 ); dc.DrawFocusRect( rc ); } m_pTheme->CloseThemeData(hTheme); } // if else { bDefaultDraw = TRUE; } // else } // else if ( bDefaultDraw ) { CRect rc( pRect ); CRect rect = rc; CPen pen1( PS_SOLID, 0, RGB(226, 222, 203) ); // 最左边的一条竖线 CPen pen2( PS_SOLID, 0, RGB(248, 247, 242) ); // 最底部的一条横线 CPen pen3, pen4; if ( m_bIsDisabled ) { pen3.CreatePen( PS_SOLID, 0, RGB(216, 213, 199) ); // 圆角矩形(4,4) pen4.CreatePen( PS_SOLID, 0, RGB(201, 199, 186) ); // 圆角矩形(6,6) } else { pen3.CreatePen( PS_SOLID, 0, RGB(122, 149, 168) ); // 圆角矩形(4,4) pen4.CreatePen( PS_SOLID, 0, RGB(0, 60, 116) ); // 圆角矩形(6,6) } COLORREF bkColorStart = RGB( 220, 214, 194 ); COLORREF bkColorEnd = RGB( 245, 244, 235 ); COLORREF FaceColorStart = RGB( 255, 255, 255 ); COLORREF FaceColorEnd = RGB( 226, 223, 214 ); COLORREF TextColorStart = RGB( 252, 252, 251 ); COLORREF TextColorEnd = RGB( 236, 235, 230 ); COLORREF bkColor = pDC->GetPixel( rc.right-1, rc.top ); int nSaveDC = dc.SaveDC(); dc.SelectStockObject( NULL_BRUSH ); // 画最左边的一条竖线和最底部的一条横线 if ( !m_bIsDisabled ) { dc.SelectObject( &pen1 ); dc.MoveTo( rc.left, rc.top+1 ); dc.LineTo( rc.left, rc.bottom-1 ); dc.SelectObject( &pen2 ); dc.MoveTo( rc.left+2, rc.bottom-1 ); dc.LineTo( rc.right-1, rc.bottom-1 ); } // 画最底层背景 TRIVERTEX vert[2] ; GRADIENT_RECT gRect; vert [0].y = rect.top; vert [0].x = rect.left+1; vert [0].Red = COLOR16( COLOR16( GetRValue( bkColorStart ) ) << 8); vert [0].Green = COLOR16( COLOR16( GetGValue( bkColorStart ) ) << 8); vert [0].Blue = COLOR16( COLOR16( GetBValue( bkColorStart ) ) << 8); vert [0].Alpha = 0x0000; vert [1].y = rect.bottom-1; vert [1].x = rect.right; vert [1].Red = COLOR16( COLOR16( GetRValue( bkColorEnd ) ) << 8); vert [1].Green = COLOR16( COLOR16( GetGValue( bkColorEnd ) ) << 8); vert [1].Blue = COLOR16( COLOR16( GetBValue( bkColorEnd ) ) << 8); vert [1].Alpha = 0xFF00; gRect.UpperLeft = 0; gRect.LowerRight = 1; if ( !m_bIsDisabled ) GradientFill( dc.GetSafeHdc(), vert, 2, &gRect, 1, GRADIENT_FILL_RECT_V ); // 如果有焦点 if ( ( m_bIsFocused ) || ( m_bIsDefault ) ) { FaceColorStart = RGB( 206, 231, 255 ); FaceColorEnd = RGB( 105, 130, 238 ); rect = rc; rect.DeflateRect( 2, 0, 0, 0 ); } // 如果是高亮 if ( m_bMouseOnButton ) { FaceColorStart = RGB( 255, 240, 207 ); FaceColorEnd = RGB( 229, 151, 0 ); rect = rc; rect.DeflateRect( 2, 0, 0, 0 ); } // 如果被按下 if ( m_bIsPressed ) { FaceColorStart = RGB( 209, 204, 193 ); FaceColorEnd = RGB( 242, 241, 238 ); TextColorStart = RGB( 229, 228, 221 ); TextColorEnd = RGB( 226, 226, 218 ); rect = rc; rect.DeflateRect( 2, 0, 0, 0 ); } // 如果被按下 if ( m_bIsDisabled ) { FaceColorStart = RGB( 245, 244, 234 ); FaceColorEnd = FaceColorStart; TextColorStart = FaceColorStart; TextColorEnd = FaceColorStart; rect = rc; rect.DeflateRect( 2, 0, 0, 0 ); } // 画 BUTTON 内部所有区域背景 vert [0].y = rc.top + 2; vert [0].x = rc.left + 2; vert [0].Red = COLOR16( COLOR16( GetRValue( FaceColorStart ) ) << 8); vert [0].Green = COLOR16( COLOR16( GetGValue( FaceColorStart ) ) << 8 ); vert [0].Blue = COLOR16( COLOR16( GetBValue( FaceColorStart ) ) << 8 ); vert [1].y = rc.bottom - 2 ; vert [1].x = rc.right - 2; vert [1].Red = COLOR16( COLOR16( GetRValue( FaceColorEnd ) ) << 8 ); vert [1].Green = COLOR16( COLOR16( GetGValue( FaceColorEnd ) ) << 8 ); vert [1].Blue = COLOR16( COLOR16( GetBValue( FaceColorEnd ) ) << 8 ); GradientFill( dc.GetSafeHdc(), vert, 2, &gRect, 1, GRADIENT_FILL_RECT_V ); // 画 BUTTON 内部有效区域背景 vert [0].y = rect.top + 4; vert [0].x = rect.left + 2; vert [0].Red = COLOR16( COLOR16( GetRValue( TextColorStart ) ) << 8); vert [0].Green = COLOR16( COLOR16( GetGValue( TextColorStart ) ) << 8 ); vert [0].Blue = COLOR16( COLOR16( GetBValue( TextColorStart ) ) << 8 ); vert [1].y = rect.bottom - 4; vert [1].x = rect.right - 4; vert [1].Red = COLOR16( COLOR16( GetRValue( TextColorEnd ) ) << 8 ); vert [1].Green = COLOR16( COLOR16( GetGValue( TextColorEnd ) ) << 8 ); vert [1].Blue = COLOR16( COLOR16( GetBValue( TextColorEnd ) ) << 8 ); GradientFill( dc.GetSafeHdc(), vert, 2, &gRect, 1, GRADIENT_FILL_RECT_V ); // 如果有焦点 if ( m_bIsFocused ) { rect = rc; rect.DeflateRect( 3, 3 ); dc.DrawFocusRect( rect ); } // 画椭圆形外框 rect = rc; rect.DeflateRect( 1, 1 ); dc.SelectObject( &pen3 ); dc.RoundRect( rect, CPoint( 4, 4 ) ); dc.SelectObject( &pen4 ); dc.RoundRect( rect, CPoint( 6, 6 ) ); // 去除右上角的多余点 dc.SetPixel( rc.right-1, rc.top, bkColor ); dc.RestoreDC( nSaveDC ); } // if return BTNST_OK; } // End of OnDrawBackground #ifdef NO_THEMEAPI_FOUND #undef NO_THEMEAPI_FOUND #undef BP_PUSHBUTTON #undef PBS_NORMAL #undef PBS_HOT #undef PBS_PRESSED #undef PBS_DISABLED #undef PBS_DEFAULTED #endif