📄 mybutton.cpp
字号:
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
#define MOVE_IN 10
#define MOVE_DOWN 11
#define MOVE_UP 12
#define MOVE_OUT 13
CMyButton::CMyButton()
{i=false;
j=false;
m_focus=false;
m_move=true;
isflat=false;
select=false;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lps)
{
// TODO: Add your code to draw the specified item
CDC *dc=CDC::FromHandle(lps->hDC);
UINT state=lps->itemState;
focusrect.CopyRect(&lps->rcItem);
focusrect.DeflateRect(4,4,4,4);
btnrect.CopyRect(&lps->rcItem);
if(state&ODS_FOCUS)
{
m_focus=true;
}
if(!(state&ODS_FOCUS))
m_focus=false;
if(state&ODS_SELECTED)
select=true;
if(!state&ODS_SELECTED)
select=false;
Draw();
}
void CMyButton::fresh()
{
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.BitBlt(0,0,rect.Width(),rect.Height(),&memdc,0,0,SRCCOPY);
}
BOOL CMyButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
if(!j&&isflat)
{
CBitmap bitmap;
CRect rect;
GetClientRect(&rect);
memdc.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height ());
memdc.SelectObject(&bitmap);
memdc.BitBlt(0,0,rect.Width(),rect.Height(),pDC,0,0,SRCCOPY);
j=true;
}
return TRUE;
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_move)
{
m_move=false;
m_state=MOVE_IN;
Draw();
SetTimer(1,10,NULL);
}
CButton::OnMouseMove(nFlags, point);
}
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_state=MOVE_DOWN;
Draw();
CButton::OnLButtonDown(nFlags, point);
}
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_state=MOVE_UP;
Draw();
CButton::OnLButtonUp(nFlags, point);
}
void CMyButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CPoint pt;
GetCursorPos(&pt);
CRect rect;
GetWindowRect(&rect);
if(!rect.PtInRect(pt)&&!m_move)
{
KillTimer(1);
m_move=true;
m_state=MOVE_OUT;
Draw();
}
CButton::OnTimer(nIDEvent);
}
void CMyButton::Draw()
{
CClientDC dc(this);
if(!isflat)
{
CString st=_T("");
GetWindowText(st);
CBrush br(RGB(196,112,64));
CBrush focusbr(RGB(255,255,255));
dc.FillRect(&btnrect,&br);
dc.SetTextColor(RGB(0,0,0));
dc.SetBkColor(RGB(196,112,64));
dc.DrawText(st,&btnrect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
if(m_focus)
{
dc.DrawFocusRect(focusrect);
dc.Draw3dRect(&btnrect,RGB(255,255,255),RGB(0,0,0));
if(select&&m_state==MOVE_DOWN)
{
dc.Draw3dRect(&btnrect,RGB(0,0,0),RGB(255,255,255));
}
}
else
dc.Draw3dRect(&btnrect,RGB(255,255,255),RGB(0,0,0));
}
else{
CRect rect;
GetClientRect(&rect);
fresh();
switch(m_state)
{
case MOVE_UP:
if(!m_move)
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
break;
case MOVE_IN:
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
break;
case MOVE_DOWN:
dc.Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255));
break;
default:
break;
}
CString st=_T("");
GetWindowText(st);
CFont font;
LOGFONT ft;
memset(&ft,0,sizeof(LOGFONT));
ft.lfHeight=20;
strcpy(ft.lfFaceName,"宋体");
font.CreateFontIndirect(&ft);
CFont *oldfont=dc.SelectObject(&font);
dc.SetTextColor(RGB(0,0,0));
dc.SetBkMode(TRANSPARENT);
CSize sz=dc.GetTextExtent(st);
int rectwidth=rect.Width();
int recthight=rect.Height();
int fontwidth=sz.cx;
int fonthight=sz.cy;
int x=(rectwidth-fontwidth)/2;
int y=(recthight-fonthight)/2+1;
if(m_state==MOVE_DOWN)
{
x++;
y++;
}
dc.TextOut(x,y,st);
dc.SelectObject(oldfont);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -