mybutton.cpp
来自「用c++ 开发的中国象棋」· C++ 代码 · 共 157 行
CPP
157 行
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "chinesechess.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_MouseFlag = FALSE;
lf = new LOGFONT;
}
CMyButton::~CMyButton()
{
delete lf;
m_Font->DeleteObject();
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CString m_Caption;
GetWindowText(m_Caption);
//重新设置按钮的字体
m_Font = GetFont();
m_Font->GetLogFont(lf);
lf->lfWeight = 540; //改边字体的粗细
CFont * NewFont = new CFont;
NewFont->CreateFontIndirect(lf);
CDC * hdc = CDC::FromHandle(lpDrawItemStruct->hDC);
CBrush * m_brush, * m_Oldbrush, * Broderline;
CPen * m_Pen, * m_OldPen;
COLORREF m_PenColor, m_BrushColor; //按钮的钢笔、笔刷的颜色
CRect m_ButtonRect(lpDrawItemStruct->rcItem); //取得按钮的大小
CRect m_Borderline(m_ButtonRect);
CFont * m_OldFont = (CFont * ) hdc->SelectObject(NewFont);
//绘制各种状态下的按钮
if ( (lpDrawItemStruct->itemState & ODS_FOCUS) )
{
m_ButtonRect.DeflateRect(1, 1);
}
if ((lpDrawItemStruct->itemState & ODS_DISABLED) ||
(!m_MouseFlag && !(lpDrawItemStruct->itemState & ODS_SELECTED)))
{
m_PenColor = ::GetSysColor(COLOR_3DSHADOW);
m_BrushColor = ::GetSysColor(COLOR_3DFACE);
}
else
{
COLORREF BorderColor = ::GetSysColor(COLOR_HIGHLIGHT);
m_PenColor = BorderColor;
if ((lpDrawItemStruct->itemState & ODS_SELECTED))
{
m_BrushColor = RGB(100, 100, 205);
hdc->SetTextColor(RGB(250, 0, 100));
}
else
{
m_BrushColor = RGB(170, 213, 255);
hdc->SetTextColor(::GetSysColor(COLOR_BTNTEXT));
}
}
if (lpDrawItemStruct->itemState & ODS_DISABLED)
{
hdc->SetTextColor(::GetSysColor(COLOR_BTNTEXT));
}
m_Pen = new CPen;
m_brush = new CBrush;
m_Pen->CreatePen(PS_SOLID, 1, m_PenColor);
m_brush->CreateSolidBrush(m_BrushColor);
m_OldPen = (CPen * )hdc->SelectObject(m_Pen);
m_Oldbrush = (CBrush *)hdc->SelectObject(m_brush);
hdc->Rectangle(m_ButtonRect);
hdc->SetBkMode(TRANSPARENT);
hdc->DrawText(m_Caption, m_ButtonRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
if ((lpDrawItemStruct->itemState & ODS_FOCUS))
{
m_ButtonRect.DeflateRect(3, 3);
hdc->DrawFocusRect(m_ButtonRect);
Broderline = new CBrush;
Broderline->CreateSolidBrush(RGB(0, 0, 0));
hdc->FrameRect(m_Borderline, Broderline); //绘制按钮的黑边框
delete Broderline;
}
delete NewFont;
delete m_Pen;
delete m_brush;
hdc->SelectObject(m_OldFont);
hdc->SelectObject(m_OldPen);
hdc->SelectObject(m_Oldbrush);
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd* pCaptureWnd=GetCapture();
if (pCaptureWnd != this)
{
m_MouseFlag = TRUE;
SetCapture(); //加上该句可以防止当鼠标指向时发生闪烁
// Invalidate();
}
else
{
POINT p2 = point;
ClientToScreen(&p2);
//返回当前鼠标下的窗口句柄,判断该句柄是否为按钮的句柄
CWnd* wndUnderMouse = WindowFromPoint(p2);
if (wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd){
// Redraw only if mouse goes out
if (m_MouseFlag == TRUE){
m_MouseFlag = FALSE;
// Invalidate();
}
// If user is NOT pressing left button then release capture!
if (!(nFlags & MK_LBUTTON))
ReleaseCapture();
}
}
CButton::OnMouseMove(nFlags, point);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?