📄 mybutton.cpp
字号:
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "DialogTest7.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_bIsMouseMove = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( !m_bIsMouseMove )
{
m_bIsMouseMove = TRUE;
Invalidate();
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE ;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent);
}
CButton::OnMouseMove(nFlags, point);
}
#define WM_MOUSEOFFBUTTON WM_USER+101
LONG CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bIsMouseMove = FALSE;
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEOFFBUTTON, 0, 0);
Invalidate();
return 0;
}
#define WM_MOUSEONBUTTON WM_USER+100
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
DWORD nState = lpDrawItemStruct->itemState;
DWORD nAction = lpDrawItemStruct->itemAction;
CRect rc = lpDrawItemStruct->rcItem;
UINT uStyle = DFCS_BUTTONPUSH;
pDC->SetBkMode(TRANSPARENT);
CString strText;
GetWindowText(strText);
if (nState & ODS_SELECTED)//select
{
m_bIsMouseMove = FALSE;
DrawSelectedState(rc, pDC);
}
else //Normal
{
DrawNormalState(rc, pDC);
}
if (m_bIsMouseMove)//hover on the button
{
DrawFocusState(rc, pDC);
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEONBUTTON, 0, 0);
}
pDC->DrawText(strText, strText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
void CMyButton::DrawNormalState(CRect rc, CDC *pDC)
{
//DrawFrame(rc, pDC);
// DrawFace(rc, RGB(223, 233, 255), RGB(0, 128, 255), pDC);
// DrawFrame(rc, RGB(230, 230, 230), RGB(128, 128, 128), pDC);
// pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmNormal, DSS_NORMAL);
}
void CMyButton::DrawSelectedState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(0, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 128), RGB(0, 255, 255), pDC);
// pDC->SetTextColor(RGB(0, 128, 128));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmSelect, DSS_NORMAL);
}
void CMyButton::DrawFocusState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// pDC->SetTextColor(RGB(0, 128, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmFocuse, DSS_NORMAL);
}
void CMyButton::DrawFace(CRect &rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CPen Pen;
CPen* pOldPen = pDC->SelectObject(&Pen);
int R,G, B;
R = (GetRValue(clrTopRight) - GetRValue(clrBottomLeft)) / rc.Height();
G = (GetGValue(clrTopRight) - GetGValue(clrBottomLeft)) / rc.Height();
B = (GetBValue(clrTopRight) - GetBValue(clrBottomLeft)) / rc.Height();
int nColR = GetRValue(clrTopRight);
int nColG = GetGValue(clrTopRight);
int nColB = GetBValue(clrTopRight);
COLORREF clrColMax = clrTopRight > clrBottomLeft ? clrTopRight : clrBottomLeft;
COLORREF clrColMin = clrTopRight > clrBottomLeft ? clrBottomLeft: clrTopRight;
for(int i=0; i<rc.Height(); i++)
{
nColR -= R;
nColG -= G;
nColB -= B;
Pen.CreatePen(PS_SOLID, 1, RGB(nColR, nColG, nColB));
pDC->SelectObject(&Pen);
pDC->MoveTo(rc.left, rc.top+i);
pDC->LineTo(rc.right, rc.top+i);
Pen.DeleteObject();
}
pDC->SelectObject(pOldPen);
}
void CMyButton::DrawFrame(CRect& rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CBrush NullBrush;
NullBrush.CreateStockObject(NULL_BRUSH);
CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(0, 64, 128));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->RoundRect(rc, CPoint(3, 3) );
rc.DeflateRect(1, 1, 1, 1);
pDC->Draw3dRect(rc, clrTopRight, clrBottomLeft);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CMyButton::SetBitmaps(UINT nNormal, UINT nSelect, UINT nFocuse)
{
m_bmNormal.LoadBitmap(nNormal);
m_bmSelect.LoadBitmap(nSelect);
m_bmFocuse.LoadBitmap(nFocuse);
}
void CMyButton::OnClicked()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -