给之前的CSWButton添加一个背景色(支持半透明),效果图如下:
头文件声明(CSWButton.h):
#pragma once
#include "afxwin.h"
#include
{DECLARE_DYNAMIC(CSWButton)public:enum BUTTON_STATUS//按钮状态{BTNST_NULL, // 正常BTNST_STAY, // 鼠标停留BTNST_DISB, // 失效};enum ICON_ALIGN//图片对齐方式{ICOALIGN_CENTER,ICOALIGN_LEFT,ICOALIGN_RIGHT,ICOALIGN_TOP,ICOALIGN_BOTTOM,};public:CSWButton();~CSWButton();void SetToolTipText(LPCSTR lpszToolTipText, BOOL bActivate = TRUE);void ActivateTooltip(BOOL bActivate = TRUE);void NoticeBkgrRefresh();private:void CommonConstruct(); // 初始化void InitToolTip();void DrawAlphaFrame(Gdiplus::Graphics& g, BOOL bFocused = FALSE, BYTE byAlpha = 255);void DrawAlphaEdge(Gdiplus::Graphics& g, BYTE byAlpha = 255);void DrawWindowText(Gdiplus::Graphics& g, BOOL bFocused = FALSE, BYTE byAlpha = 255);private:std::string m_sFontName; // 字体名称int m_nFontSize; // 字体大小Gdiplus::Color m_clrFont; // 字体颜色int m_nIcoAlign; // 图标对齐方式CToolTipCtrl m_ToolTip; // 鼠标停留提醒RECT m_rcImageWindow; // 图片裁剪坐标Gdiplus::Bitmap* m_pBkgrBitmap; // 背景图片CSize m_sizeBkgrBitmap; // 背景图片尺寸CBrush m_brBkgr; // 背景颜色画刷CPen m_penEdge; // 边框颜色画笔CDC m_dcBkgr; // 背景DC(保持了父窗体背景图像)BOOL m_bTracking; // 鼠标停留状态BOOL m_bTabStop; // 是否响应Tab焦点BOOL m_bMuliStatus; // 背景图像是否包含多种形态(正常/离开-停留/按下/选中-禁用)BOOL m_bTransParent; // 背景是否透明HCURSOR m_hCursor; // 鼠标手势protected:virtual BOOL PreTranslateMessage(MSG* pMsg);virtual void PreSubclassWindow();virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);protected:afx_msg void OnKillFocus(CWnd* pNewWnd);afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg void OnCaptureChanged(CWnd *pWnd);afx_msg BOOL OnEraseBkgnd(CDC* pDC);afx_msg void OnMouseLeave();afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);afx_msg LRESULT OnSetText(WPARAM wParam, LPARAM lParam);DECLARE_MESSAGE_MAP()public:// 设置窗口背景图片void SetBackgroundColor(COLORREF crBkgr, COLORREF cdEdge, BOOL bRepaint = TRUE);/*lpszImageName: 背景图片句柄rcWindow: 窗口区域rcClient:客户区区域*/void SetBackgroundImage(HBITMAP hBitmap, BOOL bMuliStatus = FALSE, RECT* rcWindow = NULL, BOOL bRepaint = TRUE);/*lpszImageName: 背景图片资源IDrcWindow: 窗口区域rcClient:客户区区域*/void SetBackgroundImage(UINT uiBmpResId, BOOL bMuliStatus = FALSE, RECT* rcWindow = NULL, BOOL bRepaint = TRUE);/*lpszImageName: 背景图片名称rcWindow: 窗口区域rcClient:客户区区域*/void SetBackgroundImage(LPCTSTR lpszImageName, BOOL bMuliStatus = FALSE, RECT* rcWindow = NULL, BOOL bRepaint = TRUE);// 图标对齐方式void SetIcoAlign(int nAlign = ICOALIGN_CENTER);// 字体设置void SetFont(LPCTSTR lpszFontName, int nFontSize, COLORREF crFont);// 设置鼠标void SetCursor(HCURSOR hCursor = NULL);
};
源码实现(CSWButton.cpp) :
#include "StdAfx.h"
#include "CSWButton.h"CSWButton::CSWButton()
{CommonConstruct();
}CSWButton::~CSWButton(void)
{m_penEdge.DeleteObject();m_brBkgr.DeleteObject();
}IMPLEMENT_DYNAMIC(CSWButton, CButton)
BEGIN_MESSAGE_MAP(CSWButton, CButton)ON_WM_DRAWITEM()ON_WM_KILLFOCUS()ON_WM_MOUSEMOVE()ON_WM_CAPTURECHANGED()ON_WM_ERASEBKGND()ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_RBUTTONDOWN()ON_WM_RBUTTONUP()ON_WM_MOUSELEAVE()ON_WM_SETCURSOR()ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()void CSWButton::CommonConstruct()
{m_ToolTip.m_hWnd = NULL;m_nIcoAlign = ICOALIGN_CENTER;m_sFontName = _T("宋体");m_nFontSize = 9;COLORREF cr = ::GetSysColor(COLOR_WINDOWTEXT);m_clrFont = Gdiplus::Color(255, GetRValue(cr), GetGValue(cr), GetBValue(cr));m_brBkgr.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));m_bTabStop = FALSE;m_bTracking = FALSE;m_bTransParent = FALSE;m_penEdge.CreatePen(PS_DOT, 1, RGB(128, 128, 128));
}void CSWButton::InitToolTip()
{if (m_ToolTip.m_hWnd == NULL){m_ToolTip.Create(this);m_ToolTip.Activate(FALSE);}
}void CSWButton::SetToolTipText(LPCSTR lpszToolTipText, BOOL bActivate/* = TRUE*/)
{if (lpszToolTipText == NULL)return;InitToolTip();if (m_ToolTip.GetToolCount() == 0){CRect rcClient;GetClientRect(rcClient);m_ToolTip.AddTool(this, (LPCTSTR)lpszToolTipText, rcClient, 1);}m_ToolTip.UpdateTipText((LPCTSTR)lpszToolTipText, this, 1);m_ToolTip.Activate(bActivate);
}void CSWButton::ActivateTooltip(BOOL bActivate/* = TRUE*/)
{if (m_ToolTip.GetToolCount() == 0)return;m_ToolTip.Activate(bActivate);
}BOOL CSWButton::PreTranslateMessage(MSG* pMsg)
{// TODO: 在此添加专用代码和/或调用基类InitToolTip();m_ToolTip.RelayEvent(pMsg);return CButton::PreTranslateMessage(pMsg);
}void CSWButton::SetBackgroundColor(COLORREF crBkgr, COLORREF cdEdge, BOOL bRepaint)
{if (m_brBkgr.GetSafeHandle() != NULL){m_brBkgr.DeleteObject();}if (crBkgr != (COLORREF)-1){m_brBkgr.CreateSolidBrush(crBkgr);}if (m_penEdge.GetSafeHandle() != NULL){m_penEdge.DeleteObject();}if (cdEdge != (COLORREF)-1){m_penEdge.CreatePen(PS_DOT, 2, cdEdge);}else{m_penEdge.CreatePen(PS_DOT, 2, RGB(255, 255, 255));}if (!m_bTransParent && bRepaint && GetSafeHwnd() != NULL){Invalidate();UpdateWindow();}
}void CSWButton::SetBackgroundImage(HBITMAP hBitmap, BOOL bMuliStatus, RECT* rcWindow, BOOL bRepaint)
{m_pBkgrBitmap = CImageFactory::getSingletonPtr()->GetSkinItemImage(hBitmap);m_bMuliStatus = bMuliStatus;if (m_pBkgrBitmap){if (rcWindow == NULL)m_rcImageWindow = CRect(0, 0, m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());elsem_rcImageWindow = *rcWindow;if (m_bMuliStatus)m_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth() / 3, m_pBkgrBitmap->GetHeight());elsem_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());if (Gdiplus::IsAlphaPixelFormat((Gdiplus::PixelFormat)m_pBkgrBitmap->GetPixelFormat()))m_bTransParent = TRUE;if (bRepaint && GetSafeHwnd() != NULL){Invalidate();UpdateWindow();}}
}void CSWButton::SetBackgroundImage(UINT uiBmpResId, BOOL bMuliStatus, RECT* rcWindow, BOOL bRepaint)
{m_bMuliStatus = bMuliStatus;m_pBkgrBitmap = CImageFactory::getSingletonPtr()->GetSkinItemImage(uiBmpResId, RT_BITMAP);if (m_pBkgrBitmap){if (rcWindow == NULL)m_rcImageWindow = CRect(0, 0, m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());elsem_rcImageWindow = *rcWindow;if (m_bMuliStatus)m_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth() / 3, m_pBkgrBitmap->GetHeight());elsem_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());if (Gdiplus::IsAlphaPixelFormat((Gdiplus::PixelFormat)m_pBkgrBitmap->GetPixelFormat()))m_bTransParent = TRUE;if (bRepaint && GetSafeHwnd() != NULL){Invalidate();UpdateWindow();}}
}void CSWButton::SetBackgroundImage(LPCTSTR lpszImageName, BOOL bMuliStatus, RECT* rcWindow,BOOL bRepaint)
{m_bMuliStatus = bMuliStatus;m_pBkgrBitmap = CImageFactory::getSingletonPtr()->GetSkinItemImage(lpszImageName);if (m_pBkgrBitmap){if (rcWindow == NULL)m_rcImageWindow = CRect(0, 0, m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());elsem_rcImageWindow = *rcWindow;if (m_bMuliStatus)m_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth() / 3, m_pBkgrBitmap->GetHeight());elsem_sizeBkgrBitmap = CSize(m_pBkgrBitmap->GetWidth(), m_pBkgrBitmap->GetHeight());if (Gdiplus::IsAlphaPixelFormat((Gdiplus::PixelFormat)m_pBkgrBitmap->GetPixelFormat()))m_bTransParent = TRUE;if (bRepaint && GetSafeHwnd() != NULL){Invalidate();UpdateWindow();}}
}void CSWButton::DrawAlphaFrame(Gdiplus::Graphics& g, BOOL bFocused, BYTE byAlpha)
{CRect rcClient; GetClientRect(rcClient);//设置滤波模式为消除锯齿g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);//定义3种参与渐变的色彩Color colors[] ={Color(byAlpha, 255, 128, 0),Color(byAlpha, 0, 128, 255),Color(byAlpha, 0, 0, 255)};float positions[] = { 0.0f, 0.5f, 1.0f };// 色彩分布位置比例LinearGradientBrush linGrBrush(RectF(0, 0, rcClient.Width(), rcClient.Height()), Color::Black, Color::White, LinearGradientModeVertical);linGrBrush.SetInterpolationColors(colors, positions, 3);// 圆角半径int nCornerRadius = 5;GraphicsPath* path = new GraphicsPath;path->StartFigure();path->AddArc((REAL)rcClient.left, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 180.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 270.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 0.0, 90.0);path->AddArc((REAL)rcClient.left, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 90.0, 90.0);path->CloseFigure();g.FillPath(&linGrBrush, path);delete path;path = NULL;
}void CSWButton::DrawAlphaEdge(Gdiplus::Graphics& g, BYTE byAlpha)
{CRect rcClient; GetClientRect(rcClient);LOGPEN logPen;m_penEdge.GetLogPen(&logPen);Gdiplus::Pen pen(Color(byAlpha, GetRValue(logPen.lopnColor), GetGValue(logPen.lopnColor), GetBValue(logPen.lopnColor)), logPen.lopnWidth.x);pen.SetDashStyle(Gdiplus::DashStyle::DashStyleSolid);// 直角矩形//g.DrawRectangle(&pen, rcClient.left+1, rcClient.top+1, rcClient.Width()-2, rcClient.Height()-2);// 圆角矩形int nCornerRadius = 5;GraphicsPath* path = new GraphicsPath;path->StartFigure();path->AddArc((REAL)rcClient.left, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 180.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 270.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 0.0, 90.0);path->AddArc((REAL)rcClient.left, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 90.0, 90.0);path->CloseFigure();g.DrawPath(&pen, path);delete path;path = NULL;
}void CSWButton::DrawWindowText(Gdiplus::Graphics& g, BOOL bFocused, BYTE byAlpha)
{CRect rcShowText; GetClientRect(rcShowText);char szWindowText[_MAX_PATH] = { 0 };GetWindowText(szWindowText, _MAX_PATH);if (!rcShowText.IsRectEmpty() && strlen(szWindowText) > 0){std::wstring wsFont = CStringHelper::Ansi2Unicode(m_sFontName.c_str());Gdiplus::Font font(wsFont.c_str(), m_nFontSize, (bFocused || m_bTracking) ? (FontStyleRegular | FontStyleUnderline) : FontStyleRegular);StringFormat stringFormat;stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);stringFormat.SetAlignment(StringAlignmentCenter);stringFormat.SetLineAlignment(StringAlignmentCenter);std::wstring wsTitle = CStringHelper::Ansi2Unicode(szWindowText);SolidBrush brush(m_clrFont);g.SetTextRenderingHint(TextRenderingHintAntiAlias);g.DrawString(wsTitle.c_str(), wsTitle.size(), &font, RectF(rcShowText.left, rcShowText.top, rcShowText.Width(), rcShowText.Height()), &stringFormat, &brush);}
}void CSWButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{// TODO: 添加您的代码以绘制指定项CRect rcClient;GetClientRect(&rcClient);CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);sw::CMemDC dcMem(*pDC, rcClient);if (m_bTransParent){CRect rcWindow; GetWindowRect(rcWindow);GetParent()->ScreenToClient(rcWindow);if (m_dcBkgr.m_hDC == NULL){// 复制父窗口背景CClientDC dcParentBkgr(GetParent());m_dcBkgr.CreateCompatibleDC(&dcParentBkgr);HBITMAP hBmp = ::CreateCompatibleBitmap(dcParentBkgr.GetSafeHdc(), rcClient.Width(), rcClient.Height());HBITMAP hOldBmp = (HBITMAP)dcParentBkgr.SelectObject(hBmp);m_dcBkgr.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &dcParentBkgr, rcWindow.left, rcWindow.top, SRCCOPY);dcParentBkgr.SelectObject(hOldBmp);DeleteObject(hBmp);}dcMem->BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_dcBkgr, 0, 0, SRCCOPY);}Gdiplus::Graphics g(dcMem); BOOL bIsFocused = m_bTracking || (lpDrawItemStruct->itemState & ODS_FOCUS) || (GetCheck() == BST_CHECKED);BOOL bIsDisabled = (lpDrawItemStruct->itemState & ODS_GRAYED) || (lpDrawItemStruct->itemState & ODS_DISABLED);if (m_pBkgrBitmap == NULL){if (bIsDisabled)
#pragma message("rcClipImage灰度调节未实现")DrawAlphaFrame(g, bIsFocused, 128);else if (bIsFocused)
#pragma message("rcClipImage亮度调节未实现")DrawAlphaFrame(g, bIsFocused, 196);elseDrawAlphaFrame(g, bIsFocused, 128);DrawAlphaEdge(g);DrawWindowText(g);return;}CRect rcClipImage = m_rcImageWindow;if (m_bMuliStatus){DrawAlphaFrame(g, bIsFocused, 128);if (bIsDisabled)rcClipImage.OffsetRect(m_sizeBkgrBitmap.cx * 2, 0);else if (bIsFocused)rcClipImage.OffsetRect(m_sizeBkgrBitmap.cx, 0);}else{if (bIsDisabled)
#pragma message("rcClipImage灰度调节未实现")DrawAlphaFrame(g, bIsFocused, 128);else if (bIsFocused)
#pragma message("rcClipImage亮度调节未实现")DrawAlphaFrame(g, bIsFocused, 196);elseDrawAlphaFrame(g, bIsFocused, 128);}char szWindowText[_MAX_PATH] = { 0 };GetWindowText(szWindowText, _MAX_PATH);CRect rcShowImage = rcClient;CRect rcShowText = rcClient;int nEdge = 5; // 为了保证图标显示不至于靠近边缘,这里设置边缘间距为5if (m_nIcoAlign == ICOALIGN_LEFT){rcShowImage.left += nEdge;rcShowImage.top = (rcClient.Height() - m_sizeBkgrBitmap.cy) / 2;rcShowImage.bottom = rcShowImage.top + m_sizeBkgrBitmap.cy;rcShowImage.right = rcShowImage.left + m_sizeBkgrBitmap.cx;rcShowText.left = rcShowImage.right;}else if (m_nIcoAlign == ICOALIGN_RIGHT){rcShowImage.left = rcClient.right - m_sizeBkgrBitmap.cx - nEdge;rcShowImage.right = rcClient.right - nEdge;rcShowImage.top = rcClient.bottom - m_sizeBkgrBitmap.cy;rcShowImage.bottom = rcShowImage.top + m_sizeBkgrBitmap.cy;rcShowText.right = rcShowImage.left;}else if (m_nIcoAlign == ICOALIGN_TOP){rcShowImage.top += nEdge;rcShowImage.left = (rcClient.Width() - m_sizeBkgrBitmap.cx) / 2;rcShowImage.right = rcShowImage.left + m_sizeBkgrBitmap.cx;rcShowImage.bottom = rcShowImage.top + m_sizeBkgrBitmap.cy + 2;rcShowText.top = rcShowImage.bottom;}else if (m_nIcoAlign == ICOALIGN_BOTTOM){rcShowImage.left = (rcClient.Width() - m_sizeBkgrBitmap.cx) / 2;rcShowImage.right = rcShowImage.left + m_sizeBkgrBitmap.cx;rcShowImage.top = rcClient.bottom - m_sizeBkgrBitmap.cy - nEdge;rcShowImage.bottom = rcClient.bottom - nEdge;rcShowText.bottom = rcShowImage.top;}else{if (!rcShowText.IsRectEmpty() && strlen(szWindowText) > 0){// 显示文字rcShowImage.left = (rcClient.Width() - m_sizeBkgrBitmap.cx) / 2;rcShowImage.right = rcShowImage.left + m_sizeBkgrBitmap.cx;rcShowImage.bottom = m_sizeBkgrBitmap.cy;rcShowText.top = rcShowImage.bottom;}}g.DrawImage(m_pBkgrBitmap, RectF(rcShowImage.left, rcShowImage.top, rcShowImage.Width(), rcShowImage.Height()), rcClipImage.left, rcClipImage.top, rcClipImage.Width(), rcClipImage.Height(), UnitPixel, NULL, NULL, NULL);if (!rcShowText.IsRectEmpty() && strlen(szWindowText)>0){std::wstring wsFont = CStringHelper::Ansi2Unicode(m_sFontName.c_str());Gdiplus::Font font(wsFont.c_str(), m_nFontSize, bIsFocused ? (FontStyleRegular | FontStyleUnderline) : FontStyleRegular);StringFormat stringFormat;stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);stringFormat.SetAlignment(StringAlignmentCenter);stringFormat.SetLineAlignment(StringAlignmentCenter);std::wstring wsTitle = CStringHelper::Ansi2Unicode(szWindowText);SolidBrush brush(m_clrFont);g.SetTextRenderingHint(TextRenderingHintAntiAlias);g.DrawString(wsTitle.c_str(), wsTitle.size(), &font, RectF(rcShowText.left, rcShowText.top, rcShowText.Width(), rcShowText.Height()), &stringFormat, &brush);}if ((m_bTracking) || (!m_bMuliStatus && bIsFocused && m_penEdge.GetSafeHandle() && m_bTabStop)){rcClient.DeflateRect(1, 1, 1, 1);LOGPEN logPen;m_penEdge.GetLogPen(&logPen);Gdiplus::Pen pen(Color(255, 255-GetRValue(logPen.lopnColor), 255-GetGValue(logPen.lopnColor), 255-GetBValue(logPen.lopnColor)), logPen.lopnWidth.x);pen.SetDashStyle(Gdiplus::DashStyle::DashStyleDot);// 直角矩形//g.DrawRectangle(&pen, rcClient.left+1, rcClient.top+1, rcClient.Width()-2, rcClient.Height()-2);// 圆角矩形int nCornerRadius = 2;GraphicsPath* path = new GraphicsPath;path->StartFigure();path->AddArc((REAL)rcClient.left, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 180.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 270.0, 90.0);path->AddArc((REAL)rcClient.left + rcClient.Width() - nCornerRadius * 2, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 0.0, 90.0);path->AddArc((REAL)rcClient.left, (REAL)rcClient.top + rcClient.Height() - nCornerRadius * 2, (REAL)nCornerRadius * 2, (REAL)nCornerRadius * 2, 90.0, 90.0);path->CloseFigure();g.DrawPath(&pen, path);delete path;path = NULL;}
}void CSWButton::PreSubclassWindow()
{// TODO: 在此添加专用代码和/或调用基类DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);// GetButtonStyle();if (dwStyle & BS_DEFPUSHBUTTON)m_bTracking = TRUE;if (dwStyle & WS_TABSTOP)m_bTabStop = TRUE;DWORD dwExStyle = GetWindowLong(m_hWnd, GWL_EXSTYLE); if (dwExStyle & WS_EX_TRANSPARENT)m_bTransParent = TRUE;SetButtonStyle(dwStyle | BS_OWNERDRAW);CButton::PreSubclassWindow();//ModifyStyle(WS_TABSTOP, 0);
}void CSWButton::OnKillFocus(CWnd* pNewWnd)
{CButton::OnKillFocus(pNewWnd);// TODO: 在此处添加消息处理程序代码m_bTracking = FALSE;InvalidateRect(NULL, TRUE);
}void CSWButton::OnMouseMove(UINT nFlags, CPoint point)
{// TODO: 在此添加消息处理程序代码和/或调用默认值CButton::OnMouseMove(nFlags, point);if (nFlags & MK_LBUTTON && m_bTracking == FALSE)return;CWnd* pWnd = GetActiveWindow();CWnd* pParent = GetOwner();ClientToScreen(&point);CWnd* pWndUnderMouse = WindowFromPoint(point);//if (pWndUnderMouse && pWndUnderMouse->m_hWnd == this->m_hWnd){if (!m_bTracking){m_bTracking = TRUE;InvalidateRect(NULL, TRUE);TRACKMOUSEEVENT trackMoureEvent;trackMoureEvent.cbSize = sizeof(trackMoureEvent);trackMoureEvent.dwFlags = TME_LEAVE;trackMoureEvent.hwndTrack = m_hWnd;::_TrackMouseEvent(&trackMoureEvent);TRACE("CSWButton::OnMouseMove(1)=%d\n", GetTickCount());}}else{if (m_bTracking){m_bTracking = FALSE;InvalidateRect(NULL, TRUE);TRACE("CSWButton::OnMouseMove(2)=%d\n", GetTickCount());}}return;//if ((GetCapture() != this) && (pWnd != NULL && pParent != NULL)) {if (!m_bTracking){m_bTracking = TRUE;SetCapture();InvalidateRect(NULL, TRUE);}}else{if (pWndUnderMouse && pWndUnderMouse->m_hWnd != this->m_hWnd){if (m_bTracking == TRUE){m_bTracking = FALSE;InvalidateRect(NULL, TRUE);}if (!(nFlags & MK_LBUTTON)) ReleaseCapture();}}
}void CSWButton::OnMouseLeave()
{// TODO: 在此添加消息处理程序代码和/或调用默认值m_bTracking = FALSE;InvalidateRect(NULL, TRUE);CButton::OnMouseLeave();
}void CSWButton::OnCaptureChanged(CWnd *pWnd)
{// TODO: 在此处添加消息处理程序代码if (m_bTracking == TRUE){ReleaseCapture();InvalidateRect(NULL, TRUE);}CButton::OnCaptureChanged(pWnd);
}void CSWButton::NoticeBkgrRefresh()
{if (m_bTransParent){if (m_dcBkgr.m_hDC != NULL)m_dcBkgr.DeleteDC();InvalidateRect(NULL, FALSE);}
}BOOL CSWButton::OnEraseBkgnd(CDC* pDC)
{// TODO: 在此添加消息处理程序代码和/或调用默认值return TRUE;// CButton::OnEraseBkgnd(pDC);
}void CSWButton::SetIcoAlign(int nAlign)
{m_nIcoAlign = nAlign;InvalidateRect(NULL);
}// 字体设置
void CSWButton::SetFont(LPCTSTR lpszFontName, int nFontSize, COLORREF crFont)
{m_sFontName = lpszFontName;m_nFontSize = nFontSize;m_clrFont = Gdiplus::Color(255, GetRValue(crFont), GetGValue(crFont), GetBValue(crFont));InvalidateRect(NULL);
}LRESULT CSWButton::OnSetText(WPARAM wParam, LPARAM lParam)
{LRESULT Result = Default();InvalidateRect(NULL, TRUE);return Result;
}void CSWButton::SetCursor(HCURSOR hCursor)
{m_hCursor = hCursor;if (m_hCursor == NULL) // No cursor handle - load our own{m_hCursor = ::LoadCursor(NULL, IDC_HAND);}
}BOOL CSWButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{if (m_hCursor){::SetCursor(m_hCursor);return TRUE;}return FALSE;
}
整体界面效果图: