📄 xpradio.cpp
字号:
#include <Windows.h>
#include <Winuser.h>
#include "stdafx.h"
#include "XPRadio.h"
#include "memDC.h"
#include "Resource.h"
//#if(WINVER >= 0x0501)
#ifdef THEMEAPI
#include <tmschema.h>
#else
#define NO_THEMEAPI_FOUND
#define BP_RADIOBUTTON 0x00000002
#define RBS_UNCHECKEDNORMAL 0x00000001
#define RBS_UNCHECKEDHOT 0x00000002
#define RBS_UNCHECKEDPRESSED 0x00000003
#define RBS_UNCHECKEDDISABLED 0x00000004
#define RBS_CHECKEDNORMAL 0x00000005
#define RBS_CHECKEDHOT 0x00000006
#define RBS_CHECKEDPRESSED 0x00000007
#define RBS_CHECKEDDISABLED 0x00000008
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CXPRadio::CXPRadio()
{
m_bIsXPStyle = TRUE;
}
CXPRadio::~CXPRadio()
{
}
// 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 CXPRadio::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 CXPRadio::OnDrawBackground(CDC* pDC, LPCRECT pRect)
{
BOOL bDefaultDraw = FALSE;
int iStateId = 0;
CMemDC dc( pDC );
PaintBk(&dc);
if ( GetCheck() == BST_CHECKED )
{
iStateId = RBS_CHECKEDNORMAL; // Normal
if (m_bMouseOnButton) iStateId = RBS_CHECKEDHOT; // Hot
if (m_bIsPressed) iStateId = RBS_CHECKEDPRESSED; // Pressed
if (m_bIsDisabled) iStateId = RBS_CHECKEDDISABLED; // Disabled
}
else
{
iStateId = RBS_UNCHECKEDNORMAL; // Normal
if (m_bMouseOnButton) iStateId = RBS_UNCHECKEDHOT; // Hot
if (m_bIsPressed) iStateId = RBS_UNCHECKEDPRESSED; // Pressed
if (m_bIsDisabled) iStateId = RBS_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_RADIOBUTTON, 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_RADIO ); // 取出位图资源
pOldBitmap = BitmapDC.SelectObject( &bitmap ); // 将位图选入内存场境
TransparentBlt( dc.m_hDC, rc.left, rc.top, 13, 13,
BitmapDC.m_hDC, (iStateId-1)*13, 0, 13, 13, RGB(0,0,0) );
BitmapDC.SelectObject(pOldBitmap);
}
return BTNST_OK;
} // End of OnDrawBackground
void CXPRadio::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
BOOL CXPRadio::OnClicked()
{
CWnd * pParent = GetParent();
CWnd * pLast;
pLast = this;
pLast->SendMessage( BM_SETCHECK, BST_CHECKED );
while ( TRUE )
{
pLast = pParent->GetNextDlgGroupItem( pLast, TRUE );
if ( pLast == this )
break;
if ( (pLast->SendMessage(WM_GETDLGCODE, 0, 0L)) & DLGC_RADIOBUTTON )
pLast->SendMessage( BM_SETCHECK, BST_UNCHECKED );
}
return FALSE;
}
DWORD CXPRadio::SetCheck(int nCheck, BOOL bRepaint)
{
if (nCheck == 0)
m_nCheck = 0;
else
m_nCheck = 1;
if (bRepaint)
Invalidate();
return BTNST_OK;
} // End of SetCheck
BEGIN_MESSAGE_MAP(CXPRadio, CMyButton)
ON_WM_GETDLGCODE()
ON_MESSAGE(BM_SETCHECK, OnSetCheck)
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
END_MESSAGE_MAP()
UINT CXPRadio::OnGetDlgCode()
{
UINT nCode = CButton::OnGetDlgCode();
// Tell the system if we want default state handling
// (losing default state always allowed)
nCode |= DLGC_RADIOBUTTON;
return nCode;
}
LRESULT CXPRadio::OnSetCheck(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case BST_CHECKED:
case BST_INDETERMINATE: // Indeterminate state is handled like checked state
SetCheck(1);
ModifyStyle( 0, WS_TABSTOP );
break;
default:
ModifyStyle( WS_TABSTOP, 0 );
SetCheck(0);
break;
} // switch
return 0;
} // End of OnSetCheck
void CXPRadio::OnKillFocus(CWnd* pNewWnd)
{
CMyButton::OnKillFocus(pNewWnd);
CWnd * pParent = GetParent();
CWnd * pLast = this;
BOOL bIsGroup = FALSE;
while ( TRUE )
{
if ( pLast == pNewWnd )
{
bIsGroup = TRUE;
break;
}
pLast = pParent->GetNextDlgGroupItem( pLast, TRUE );
if ( pLast == this )
break;
}
if ( bIsGroup )
{
if ( (pLast->SendMessage(WM_GETDLGCODE, 0, 0L)) & DLGC_RADIOBUTTON )
{
pLast->SendMessage( WM_COMMAND, BN_CLICKED, (LPARAM)( pLast->GetSafeHwnd() ) );
}
}
}
void CXPRadio::OnSetFocus(CWnd* pOldWnd)
{
CMyButton::OnSetFocus(pOldWnd);
CWnd * pParent = GetParent();
CWnd * pLast = this;
BOOL bIsGroup = FALSE;
while ( TRUE )
{
if ( pLast == pOldWnd )
{
bIsGroup = TRUE;
break;
}
pLast = pParent->GetNextDlgGroupItem( pLast, TRUE );
if ( pLast == this )
break;
}
if ( bIsGroup )
SendMessage( WM_COMMAND, BN_CLICKED, (LPARAM)( GetSafeHwnd() ) );
}
#ifdef NO_THEMEAPI_FOUND
#undef NO_THEMEAPI_FOUND
#undef BP_RADIOBUTTON
#undef RBS_UNCHECKEDNORMAL
#undef RBS_UNCHECKEDHOT
#undef RBS_UNCHECKEDPRESSED
#undef RBS_UNCHECKEDDISABLED
#undef RBS_CHECKEDNORMAL
#undef RBS_CHECKEDHOT
#undef RBS_CHECKEDPRESSED
#undef RBS_CHECKEDISABLED
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -