📄 custombutton.cpp
字号:
// CustomButton.cpp : implementation file
//
#include "stdafx.h"
#include "Examle1.h"
#include "CustomButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCustomButton
CCustomButton::CCustomButton()
{
m_bitmapID = 0;
isPressed = false;
}
CCustomButton::~CCustomButton()
{
}
BEGIN_MESSAGE_MAP(CCustomButton, CButton)
//{{AFX_MSG_MAP(CCustomButton)
ON_WM_MBUTTONDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCustomButton message handlers
void CCustomButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (lpDrawItemStruct->CtlType==ODT_BUTTON)
{
//按钮ID
UINT bID = lpDrawItemStruct->itemID;
//获取按钮文本
CString text;
CWnd* m_btn = GetParent()->GetDlgItem(lpDrawItemStruct->CtlID);
CRect m_rect;
if (m_btn!= NULL)
{
m_btn->GetWindowText(text);
m_btn->GetClientRect(m_rect);
CBitmap bitmap;
bitmap.LoadBitmap(m_bitmapID);
BITMAPINFO pbit;
bitmap.GetObject(sizeof(pbit),&pbit);
CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);
dc->SetBkMode(TRANSPARENT);
CBrush m_brush;
m_brush.CreateStockObject(LTGRAY_BRUSH);
dc->FillRect(m_rect,&m_brush);
dc->DrawEdge(m_rect,BDR_RAISEDOUTER|BDR_RAISEDINNER,BF_RECT);
if (isPressed)
{
dc->DrawEdge(m_rect,BDR_RAISEDOUTER|BDR_SUNKENINNER ,BF_RECT);
CRect temp(m_rect);
temp.DeflateRect(2,2,2,2);
dc->DrawFocusRect(temp);
// dc->Draw3dRect(m_rect,RGB(0,0,0),RGB(0,0,0));
}
else
{
dc->FillRect(m_rect,&m_brush);
dc->DrawEdge(m_rect,BDR_RAISEDOUTER|BDR_RAISEDINNER,BF_RECT);
}
switch (lpDrawItemStruct->itemState)
{
case ODS_FOCUS:
{
CRect temp(m_rect);
temp.DeflateRect(2,2,2,2);
dc->DrawFocusRect(temp);
dc->Draw3dRect(m_rect,RGB(0,0,0),RGB(0,0,0));
break;
}
}
CDC m_memdc;
m_memdc.CreateCompatibleDC(dc);
m_memdc.SelectObject(&bitmap);
int y;
y = (m_rect.Height()-pbit.bmiHeader.biHeight)/2; //图像居中
dc->StretchBlt(5,y,pbit.bmiHeader.biWidth,pbit.bmiHeader.biHeight,&m_memdc,0,0,pbit.bmiHeader.biWidth,pbit.bmiHeader.biHeight,SRCCOPY);
m_rect.DeflateRect(pbit.bmiHeader.biWidth,0,0,0);
dc->DrawText(text,m_rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
bitmap.DeleteObject();
m_memdc.DeleteDC();
}
}
}
void CCustomButton::OnMButtonDown(UINT nFlags, CPoint point)
{
CButton::OnMButtonDown(nFlags, point);
}
void CCustomButton::OnLButtonDown(UINT nFlags, CPoint point)
{
isPressed = true;
CButton::OnLButtonDown(nFlags, point);
}
void CCustomButton::OnLButtonUp(UINT nFlags, CPoint point)
{
isPressed = false;
CButton::OnLButtonUp(nFlags, point);
}
int CCustomButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CCustomButton::LoadButtonResource(UINT ID, CWnd *pParent, UINT BID)
{
SubclassDlgItem(ID,pParent);
m_bitmapID = BID;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -