📄 bitmapbtn.cpp
字号:
// BitmapBtn.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "BitmapBtn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBitmapBtn
CBitmapBtn::CBitmapBtn()
{
// m_DrawMode=1; // normal drawing mode
m_FocusRectMargin=0; // disable focus dotted rect
hClipRgn=NULL; // no clipping region
//m_TextColor=GetSysColor(COLOR_BTNTEXT); // default button text color
m_button_down = m_tracking = m_Checked = false;
}
/////////////////////////////////////////////////////////////////////////////
CBitmapBtn::~CBitmapBtn()
{
if (hClipRgn) DeleteObject(hClipRgn); // free clip region
}
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CBitmapBtn, CButton)
//{{AFX_MSG_MAP(CBitmapBtn)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_KILLFOCUS()
//ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
// ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_CXSHADE_RADIO , OnRadioInfo)
ON_MESSAGE(BM_SETCHECK , OnBMSetCheck)
ON_MESSAGE(BM_GETCHECK , OnBMGetCheck)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBitmapBtn message handlers
/////////////////////////////////////////////////////////////////////////////
void CBitmapBtn::PreSubclassWindow()
{
m_Style=GetButtonStyle(); ///get specific BS_ styles
if ((m_Style & BS_AUTOCHECKBOX)==BS_AUTOCHECKBOX)
// ||((m_Style & BS_CHECKBOX)==BS_CHECKBOX))
m_Style=BS_CHECKBOX;
else if ((m_Style & BS_AUTORADIOBUTTON)==BS_AUTORADIOBUTTON)
// ||((m_Style & BS_RADIOBUTTON)==BS_RADIOBUTTON))
m_Style=BS_RADIOBUTTON;
else { m_Style=BS_PUSHBUTTON; }
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}
/////////////////////////////////////////////////////////////////////////////
BOOL CBitmapBtn::OnEraseBkgnd(CDC* pDC)
{ return 1; }
/////////////////////////////////////////////////////////////////////////////
void CBitmapBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT (lpDrawItemStruct);
//TRACE("* Captured: %08X\n", ::GetCapture());
//Check if the button state in not in inconsistent mode...
POINT mouse_position;
if ((m_button_down) && (::GetCapture() == m_hWnd) && (::GetCursorPos(&mouse_position))){
if (::WindowFromPoint(mouse_position) == m_hWnd){
if ((GetState() & BST_PUSHED) != BST_PUSHED) {
//TRACE("* Inconsistency up detected! Fixing.\n");
SetState(TRUE);
return;
}
} else {
if ((GetState() & BST_PUSHED) == BST_PUSHED) {
//TRACE("* Inconsistency up detected! Fixing.\n");
SetState(FALSE);
return;
}
}
}
//TRACE("* Drawing: %08x\n", lpDrawItemStruct->itemState);
CString sCaption;
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); // get device context
RECT r=lpDrawItemStruct->rcItem; // context rectangle
int cx = r.right - r.left ; // get width
int cy = r.bottom - r.top ; // get height
// get text box position
RECT tr={r.left+m_FocusRectMargin+2,r.top-2,r.right-m_FocusRectMargin-2,r.bottom};
GetWindowText(sCaption); // get button text
pDC->SetBkMode(TRANSPARENT);
// Select the correct skin
if (lpDrawItemStruct->itemState & ODS_DISABLED){ // DISABLED BUTTON
if(m_bDisabled.m_hObject==NULL)
// no skin selected for disabled state -> standard button
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE));
else // paint the skin
DrawBitmap(pDC,(HBITMAP)m_bDisabled,r,m_DrawMode);
// if needed, draw the standard 3D rectangular border
if (m_Border) pDC->DrawEdge(&r,EDGE_RAISED,BF_RECT);
// paint the etched button text
pDC->SetTextColor(GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(sCaption,&tr,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
OffsetRect(&tr,-1,-1);
pDC->DrawText(sCaption,&tr,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
} else { // SELECTED (DOWN) BUTTON
if ((lpDrawItemStruct->itemState & ODS_SELECTED)||m_Checked)
{
if(m_bDown.m_hObject==NULL)
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE)); // no skin selected for selected state -> standard button
else
{
// AfxMessageBox(L"4");
DrawBitmap(pDC,(HBITMAP)m_bDown,r,m_DrawMode); // paint the skin
}
OffsetRect(&tr,1,1); //shift text
// if needed, draw the standard 3D rectangular border
if (m_Border) pDC->DrawEdge(&r,EDGE_SUNKEN,BF_RECT);
}
else
{ // DEFAULT BUTTON
if(m_bNormal.m_hObject==NULL)
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE));// no skin selected for normal state -> standard button
else if ((m_tracking)&&(m_bOver.m_hObject!=NULL))
{
// AfxMessageBox(L"1");
DrawBitmap(pDC,(HBITMAP)m_bOver,r,m_DrawMode);
}
else
{
if ((lpDrawItemStruct->itemState & ODS_FOCUS)&&(m_bFocus.m_hObject!=NULL))
{
// AfxMessageBox(L"2");
DrawBitmap(pDC,(HBITMAP)m_bFocus,r,m_DrawMode);
}
else
{
// AfxMessageBox(L"3");
DrawBitmap(pDC,(HBITMAP)m_bNormal,r,m_DrawMode);
}
}
if (m_Border) pDC->DrawEdge(&r,EDGE_RAISED,BF_RECT);// if needed, draw the standard 3D rectangular border
}
// paint the focus rect
if ((lpDrawItemStruct->itemState & ODS_FOCUS)&&(m_FocusRectMargin>0)){
r.left += m_FocusRectMargin ;
r.top += m_FocusRectMargin ;
r.right -= m_FocusRectMargin ;
r.bottom -= m_FocusRectMargin ;
DrawFocusRect (lpDrawItemStruct->hDC, &r) ;
}
// paint the enabled button text
pDC->SetTextColor(1);
pDC->DrawText(sCaption,&tr,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
}
/////////////////////////////////////////////////////////////////////////////
int CBitmapBtn::GetBitmapWidth (HBITMAP hBitmap)
{ BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),(PSTR)&bm); return bm.bmWidth;}
/////////////////////////////////////////////////////////////////////////////
int CBitmapBtn::GetBitmapHeight (HBITMAP hBitmap)
{ BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),(PSTR)&bm); return bm.bmHeight;}
/////////////////////////////////////////////////////////////////////////////
void CBitmapBtn::DrawBitmap(CDC* dc, HBITMAP hbmp, RECT r, int DrawMode)
{
CPoint pt(0,0);
ClientToScreen(&pt);
this->GetParent()->ScreenToClient(&pt);
if(DrawMode==1) //用于按钮(缩放,不透明)
{
LoadBitMapWH(dc->m_hDC,0,0,pt.x,pt.y,r.right-r.left,r.bottom-r.top,hbmp,NULL,1);
}
else if(DrawMode==0)//用于标签(不缩放)
{
RECT RC1;
GetWindowRect(&RC1);
::LoadBitMapWH(dc->m_hDC,0,0,pt.x,pt.y,r.right-r.left,r.bottom-r.top,(HBITMAP)m_backm,NULL,0);
}
else if(DrawMode==2) //用于图片(缩放,透明)
{
RECT RC1;
GetWindowRect(&RC1);
::LoadBitMapWH(dc->m_hDC,0,0,pt.x,pt.y,r.right-r.left,r.bottom-r.top,hbmp,(HBITMAP)m_backm,2);
}
}
/////////////////////////////////////////////////////////////////////////////
void CBitmapBtn::FillWithBitmap(CDC* dc, HBITMAP hbmp, RECT r)
{
if(!hbmp) return;
CDC memdc;
memdc.CreateCompatibleDC(dc);
memdc.SelectObject(hbmp);
int w = r.right - r.left;
int h = r.bottom - r.top;
int x,y,z;
int bx=GetBitmapWidth(hbmp);
int by=GetBitmapHeight(hbmp);
for (y = r.top ; y < h ; y += by){
if ((y+by)>h) by=h-y;
z=bx;
for (x = r.left ; x < w ; x += z){
if ((x+z)>w) z=w-x;
dc->BitBlt(x, y, z, by, &memdc, 0, 0, SRCCOPY);
}
}
DeleteDC(memdc);
}
/////////////////////////////////////////////////////////////////////////////
void CBitmapBtn::SetSkin(UINT normal,UINT down,UINT over,UINT disabled, UINT focus,UINT mask,
UINT backm,short drawmode, short border, short margin)
{
m_bNormal.DeleteObject(); //free previous allocated bitmap
m_bDown.DeleteObject();
m_bOver.DeleteObject();
m_bDisabled.DeleteObject();
m_bMask.DeleteObject();
m_bFocus.DeleteObject();
m_backm.DeleteObject();
if (normal>0) m_bNormal.LoadBitmap(normal);
if (down>0) m_bDown.LoadBitmap(down);
if (over>0) m_bOver.LoadBitmap(over);
if (focus>0) m_bFocus.LoadBitmap(focus);
if (backm>0) m_backm.LoadBitmap(backm);
if (disabled>0) m_bDisabled.LoadBitmap(disabled);
else if (normal>0) m_bDisabled.LoadBitmap(normal);
m_DrawMode =drawmode;
m_Border=border;
m_FocusRectMargin=max(0,margin);
}
/////////////////////////////////////////////////////////////////////////////
HRGN CBitmapBtn::CreateRgnFromBitmap(HBITMAP hBmp, COLORREF color)
{
if (!hBmp) return NULL;
BITMAP bm;
GetObject( hBmp, sizeof(BITMAP), &bm ); // get bitmap attributes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -