15,979
社区成员
发帖
与我相关
我的任务
分享
#pragma once
class XButton : public CWnd
{
DECLARE_DYNAMIC(XButton)
public:
XButton();
virtual ~XButton();
enum
{
State_Normal,
State_Focus,
State_Press,
State_Disable
};
public:
void SetImage(CString szImg);
void SetCaption(CString szText);
void SetCheck(BOOL bCheck);
BOOL GetCheck();
void SetFillColor(Gdiplus::Color col);
protected:
virtual BOOL DrawItem(CDC *pDC);
virtual void DrawImg(Graphics& graphics, CRect& rt);
//发送鼠标左键点击事件
virtual void PostLClickMsg();
private:
void DrawText(Graphics& graphics, CRect& rt);
void DrawFillColor(Graphics& graphics, CRect& rt);
protected:
DWORD m_dwState;//状态
BOOL m_bMouseLeave;//判断鼠标是否离开
BOOL m_bDowned;//判断鼠标是否按下
BOOL m_bFocus;//判断鼠标是否激活
CString m_strImg;//图片路径
CString m_strText;//显示文字
BOOL m_bFillColor;//是滞填充颜色
Gdiplus::Color m_FillColor;//填充颜色
protected:
DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
afx_msg void OnPaint();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnEnable(BOOL bEnable);
};
// lrButton.cpp : 实现文件
//
#include "stdafx.h"
#include "XButton.h"
#include "Constant.h"
#pragma comment( lib, "UxTheme.lib")
// XButton
IMPLEMENT_DYNAMIC(XButton, CWnd)
XButton::XButton()
{
m_dwState = State_Normal;
m_bMouseLeave = TRUE;
m_bDowned = FALSE;
m_bFocus = FALSE;
m_bFillColor = FALSE;
m_strText = _T("");
m_strImg = _T("");
}
XButton::~XButton()
{
}
BEGIN_MESSAGE_MAP(XButton, CWnd)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_MESSAGE( WM_MOUSELEAVE, OnMouseLeave)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_ERASEBKGND()
ON_WM_ENABLE()
END_MESSAGE_MAP()
// XButton 消息处理程序
BOOL XButton::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
return CreateEx(
0,
AfxRegisterWndClass(0),
0,
dwStyle,
rect,
pParentWnd,
nID);
}
BOOL XButton::DrawItem( CDC *pDC )
{
CRect rt;
GetClientRect(&rt);
HDC hdc = pDC->GetSafeHdc();
HDC hMemdc = CreateCompatibleDC(hdc);
HBITMAP hMembmp = CreateCompatibleBitmap(hdc, rt.Width(), rt.Height());
HBITMAP hOldbmp = (HBITMAP)SelectObject( hMemdc, hMembmp);
Graphics graphics(hMemdc);
if (m_bFillColor)
{
graphics.Clear(m_FillColor);
}
else
{
DrawThemeParentBackground(m_hWnd, hMemdc, &rt);
}
DrawImg(graphics, rt);
DrawText(graphics, rt);
BitBlt(hdc, 0, 0, rt.Width(), rt.Height(), hMemdc, 0, 0, SRCCOPY);
graphics.ReleaseHDC( hMemdc);
SelectObject(hMemdc, hOldbmp);
DeleteDC(hMemdc);
DeleteObject(hMembmp);
return TRUE;
}
void XButton::DrawImg(Graphics& graphics, CRect& rt)
{
if (_T("") == m_strImg)
return;
Gdiplus::Image* pImage = Gdiplus::Image::FromFile(m_strImg);
if (pImage->GetLastStatus() == Gdiplus::Ok)
{
UINT nImageWidth = pImage->GetWidth();
UINT nImageHeight = pImage->GetHeight();
graphics.DrawImage(pImage,RectF(0.0f, 0.0f, (Gdiplus::REAL)rt.Width(), (Gdiplus::REAL)rt.Height()),
m_dwState * nImageWidth/4.0f,
0,
nImageWidth/4.0f,
(Gdiplus::REAL)nImageHeight,
UnitPixel);
}
delete pImage;
}
void XButton::DrawText(Graphics& graphics, CRect& rt)
{
if (_T("") == m_strText)
return;
Gdiplus::FontFamily fontfamily(APP_FONT);
Gdiplus::Font font(&fontfamily, 9, FontStyleRegular, UnitPoint);
Gdiplus::StringFormat stringformat;
stringformat.SetAlignment(StringAlignmentCenter);
stringformat.SetLineAlignment(StringAlignmentCenter);
Gdiplus::SolidBrush brush(Color(254, 254, 254));
Gdiplus::RectF reF(0.0f, 0.0f, (Gdiplus::REAL)rt.Width(), (Gdiplus::REAL)rt.Height());
graphics.DrawString(m_strText, -1, &font, reF, &stringformat, &brush);
}
void XButton::SetImage(CString szImg )
{
m_strImg = szImg;
}
void XButton::OnPaint()
{
CPaintDC dc(this);
DrawItem( &dc);
}
void XButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bMouseLeave)
{
TRACKMOUSEEVENT evt = {0};
evt.cbSize = sizeof(evt);
evt.dwFlags = TME_LEAVE;
evt.hwndTrack = m_hWnd;
TrackMouseEvent( &evt);
m_bMouseLeave = FALSE;
m_dwState = State_Focus;
Invalidate(FALSE);
}
CWnd::OnMouseMove(nFlags, point);
}
void XButton::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bDowned)
{
m_dwState = State_Focus;
Invalidate(FALSE);
PostLClickMsg();
m_bDowned = FALSE;
}
CWnd::OnLButtonUp(nFlags, point);
}
void XButton::PostLClickMsg()
{
GetParent()->PostMessage(WM_BTNLCLICK, GetDlgCtrlID(), 0);
}
void XButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bDowned = TRUE;
m_dwState = State_Press;
if ((GetWindowLong( m_hWnd, GWL_STYLE) &WS_TABSTOP ) )
SetFocus();
Invalidate(FALSE);
CWnd::OnLButtonDown(nFlags, point);
}
void XButton::SetCaption(CString szText)
{
m_strText = szText;
}
LRESULT XButton::OnMouseLeave( WPARAM wParam, LPARAM lParam )
{
m_bMouseLeave = TRUE;
if (m_dwState != State_Disable)
{
m_dwState = State_Normal;
Invalidate(FALSE);
}
return 0;
}
void XButton::OnSetFocus(CWnd* pOldWnd)
{
CWnd::OnSetFocus(pOldWnd);
m_bFocus = TRUE;
Invalidate(FALSE);
}
void XButton::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
m_bFocus = FALSE;
Invalidate(FALSE);
}
BOOL XButton::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void XButton::SetFillColor(Gdiplus::Color col)
{
m_FillColor = col;
m_bFillColor = TRUE;
}
void XButton::OnEnable(BOOL bEnable)
{
CWnd::OnEnable(bEnable);
if (bEnable)
{
m_dwState = State_Normal;
}
else
{
m_dwState = State_Disable;
}
Invalidate(FALSE);
}