图片按钮
// MyButton.cpp : 实现文件 // #include "stdafx.h" #include "03 图片按钮.h" #include "MyButton.h" // CMyButton IMPLEMENT_DYNAMIC(CMyButton, CButton) CMyButton::CMyButton() { } CMyButton::~CMyButton() { } BEGIN_MESSAGE_MAP(CMyButton, CButton) ON_WM_LBUTTONUP() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_MOUSEHOVER() ON_WM_MOUSELEAVE() END_MESSAGE_MAP() // CMyButton 消息处理程序 void CMyButton::LoadPicture( int nId, CRect rtLeave, CRect rtHover, CRect rtPush) { m_BitMap.LoadBitmap(nId); m_rtLeave = rtLeave; m_rtHover = rtHover; m_rtPush = rtPush; } void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { //FromHandle可以把一个句柄转化为一个对象 CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC); //dc->TextOut(0,0,TEXT("我是按钮")); m_MemDc.CreateCompatibleDC(dc); m_MemDc.SelectObject(m_BitMap); CRect ButtonRt; GetClientRect(ButtonRt); if (bIsHover) { //鼠标按下 if (bIsPush) { //dc->TextOut(0, 0, TEXT("鼠标按下")); dc->StretchBlt(ButtonRt.left, ButtonRt.top, ButtonRt.right, ButtonRt.bottom, &m_MemDc, m_rtPush.left, m_rtPush.top, m_rtPush.Width(), m_rtPush.Height(), SRCCOPY); } //鼠标弹起 else { //dc->TextOut(0, 0, TEXT("鼠标弹起")); dc->StretchBlt(ButtonRt.left, ButtonRt.top, ButtonRt.right, ButtonRt.bottom, &m_MemDc, m_rtHover.left, m_rtHover.top, m_rtHover.Width(),m_rtHover.Height(), SRCCOPY); } } else { //鼠标没有在按钮上 //dc->TextOut(0, 0, TEXT("鼠标离开")); dc->StretchBlt(ButtonRt.left, ButtonRt.top, ButtonRt.right, ButtonRt.bottom, &m_MemDc, m_rtLeave.left, m_rtLeave.top, m_rtLeave.Width(), m_rtLeave.Height(), SRCCOPY); } m_MemDc.DeleteDC(); // TODO: 添加您的代码以绘制指定项 } void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 bIsPush = FALSE; InvalidateRect(NULL, TRUE); CButton::OnLButtonUp(nFlags, point); } void CMyButton::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 bIsPush = TRUE; InvalidateRect(NULL, TRUE); CButton::OnLButtonDown(nFlags, point); } void CMyButton::OnMouseMove(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 //捕获鼠标停留以及鼠标离开的消息 TRACKMOUSEEVENT tms = {}; tms.cbSize = sizeof(TRACKMOUSEEVENT); tms.dwFlags = TME_HOVER | TME_LEAVE; tms.dwHoverTime = 1; tms.hwndTrack = this->m_hWnd; TrackMouseEvent(&tms); CButton::OnMouseMove(nFlags, point); } void CMyButton::OnMouseHover(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 bIsHover = TRUE; InvalidateRect(NULL, TRUE); CButton::OnMouseHover(nFlags, point); } void CMyButton::OnMouseLeave() { // TODO: 在此添加消息处理程序代码和/或调用默认值 bIsHover = FALSE; InvalidateRect(NULL, TRUE); CButton::OnMouseLeave(); }
让数据变得更安全!