⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpcheck.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
#include <Windows.h>
#include <Winuser.h>

#include "stdafx.h"
#include "XPCheck.h"
#include "memDC.h"
#include "Resource.h"

//#if(WINVER >= 0x0501)
#ifdef	THEMEAPI
#include <tmschema.h>
#else
#define	NO_THEMEAPI_FOUND

#define	BP_CHECKBOX				0x00000003
#define	CBS_UNCHECKEDNORMAL		0x00000001
#define	CBS_UNCHECKEDHOT		0x00000002
#define CBS_UNCHECKEDPRESSED	0x00000003
#define	CBS_UNCHECKEDDISABLED	0x00000004

#define	CBS_CHECKEDNORMAL		0x00000005
#define	CBS_CHECKEDHOT			0x00000006
#define CBS_CHECKEDPRESSED		0x00000007
#define CBS_CHECKEDDISABLED		0x00000008
#endif

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CXPCheck::CXPCheck()
{
	m_bIsXPStyle = TRUE;
}

CXPCheck::~CXPCheck()
{
}

// 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 CXPCheck::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 CXPCheck::OnDrawBackground(CDC* pDC, LPCRECT pRect)
{
	BOOL	bDefaultDraw = FALSE;
	int		iStateId = 0;

	CMemDC dc( pDC );
	PaintBk(&dc);

	if ( GetCheck() == BST_CHECKED )
	{
		iStateId = CBS_CHECKEDNORMAL;								// Normal
        if (m_bMouseOnButton)	iStateId = CBS_CHECKEDHOT;			// Hot
		if (m_bIsPressed)		iStateId = CBS_CHECKEDPRESSED;		// Pressed
		if (m_bIsDisabled)		iStateId = CBS_CHECKEDDISABLED;		// Disabled
	}
	else
	{
		iStateId = CBS_UNCHECKEDNORMAL;								// Normal
        if (m_bMouseOnButton)	iStateId = CBS_UNCHECKEDHOT;		// Hot
		if (m_bIsPressed)		iStateId = CBS_UNCHECKEDPRESSED;	// Pressed
		if (m_bIsDisabled)		iStateId = CBS_UNCHECKEDDISABLED;	// Disabled
	}

	// No theme helper passed
	if (m_pTheme == NULL || m_pTheme->IsAppThemed() == FALSE)
	{
		bDefaultDraw = TRUE;
	} // if
	else
	{
		HTHEME	hTheme = NULL;

		hTheme = m_pTheme->OpenThemeData(GetSafeHwnd(), L"BUTTON");
		if (hTheme)
		{
            CRect rc = pRect;
			if ( GetButtonStyle() & BS_LEFTTEXT )
			{
				rc.left = rc.right - 13;
			}
			else
			{
                rc.right = rc.left + 13;
			}

			m_pTheme->DrawThemeBackground(hTheme, dc.GetSafeHdc(), BP_CHECKBOX, iStateId, rc, NULL);

			m_pTheme->CloseThemeData(hTheme);
		} // if
		else
		{
			bDefaultDraw = TRUE;
		} // else
	} // else

	if ( bDefaultDraw )
	{
		CRect rc = pRect;
		if ( GetButtonStyle() & BS_LEFTTEXT )
			rc.left = rc.right - 13;
		rc.top = rc.CenterPoint().y - 6;

		CBitmap bitmap, * pOldBitmap;

		CDC BitmapDC;
		BitmapDC.CreateCompatibleDC( &dc );				// 建立与显示设备兼容的内存设备场境
		bitmap.LoadBitmap( IDB_CHECKBOX );				// 取出位图资源
		pOldBitmap = BitmapDC.SelectObject( &bitmap );	// 将位图选入内存场境

		dc.BitBlt( rc.left, rc.top, 13, 13, &BitmapDC, (iStateId-1)*13, 0, SRCCOPY);	///显示它
		BitmapDC.SelectObject(pOldBitmap);
	}
	
	return BTNST_OK;
} // End of OnDrawBackground



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

	m_bIsPressed  = (lpDIS->itemState & ODS_SELECTED);
	m_bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
	m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);

	CRect itemRect = lpDIS->rcItem;

	pDC->SetBkMode(TRANSPARENT);

	OnDrawBackground(pDC, &itemRect);

	// Read the button's title
	CString sTitle;
	GetWindowText(sTitle);

	CRect captionRect = lpDIS->rcItem;
	UINT nStyle = GetWindowLong( m_hWnd, GWL_STYLE);
	if ( nStyle & BS_LEFTTEXT )
	{
		captionRect.right -= 15;
	}
	else
	{
		captionRect.left += 15;
	}

	captionRect.DeflateRect(1, 1);

	// Write the button title (if any)
	if (sTitle.IsEmpty() == FALSE)
	{
		UINT nTextStyle = DT_SINGLELINE|DT_VCENTER|DT_LEFT;
		
		CRect rc( captionRect );
		pDC->DrawText(sTitle, -1, captionRect, nTextStyle | DT_CALCRECT);
		captionRect.OffsetRect(0, (rc.bottom - captionRect.bottom) / 2 );

		if ( (nStyle & BS_CENTER) == BS_CENTER )
		{
			captionRect.OffsetRect( (rc.right - captionRect.right) / 2, 0 );
		}
		else if ( (nStyle & BS_RIGHT) == BS_RIGHT )
		{
			captionRect.OffsetRect( rc.right - captionRect.right, 0 );
		}
		
		if (m_bIsDisabled)
		{
			captionRect.OffsetRect(1, 1);
			pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
			pDC->DrawText(sTitle, -1, captionRect, nTextStyle );
			captionRect.OffsetRect(-1, -1);
			pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
			pDC->DrawText(sTitle, -1, captionRect, nTextStyle );
		} // if
		else
		{
			pDC->DrawText(sTitle, -1, captionRect, nTextStyle );

			captionRect.InflateRect( 1, 0 );
			if ( m_bIsFocused )
			{
				pDC->DrawFocusRect( captionRect );
			}
		} // if
	} // if

} // End of DrawItem


#ifdef	NO_THEMEAPI_FOUND
#undef	NO_THEMEAPI_FOUND
#undef	BP_CHECKBOX				
#undef	CBS_UNCHECKEDNORMAL		
#undef	CBS_UNCHECKEDHOT		
#undef	CBS_UNCHECKEDPRESSED		
#undef	CBS_UNCHECKEDDISABLED	

#undef	CBS_CHECKEDNORMAL		
#undef	CBS_CHECKEDHOT			
#undef	CBS_CHECKEDPRESSED		
#undef	CBS_CHECKEDISABLED		
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -