// XSPngButton.cpp : 实现文件
//
#include "stdafx.h"
#include "XSPngButton.h"
#include "classInternetManager.h"
// CXSPngButton
IMPLEMENT_DYNAMIC(CXSPngButton, CButton)
CXSPngButton::CXSPngButton()
{
m_bTracked = false;
m_pImage = NULL;
m_nState = CTRL_NOFOCUS;
m_nBtnType = BTN_TYPE_NORMAL;
m_bIsMouseHover = false;
}
CXSPngButton::CXSPngButton(int nItem, int nSubItem, CRect rect, HWND hParent)
{
m_inItem = nItem;
m_inSubItem = nSubItem;
m_rect = rect;
m_hParent = hParent;
bEnable = TRUE;
}
CXSPngButton::~CXSPngButton()
{
if(m_pImage == NULL)
{
delete m_pImage;
m_pImage = NULL;
}
}
void CXSPngButton::InitButton(UINT nImage, UINT nBtnType /* = BTN_TYPE_NORMAL */)
{
m_nBtnType = nBtnType;
m_pImage = LoadImageFromResource(AfxGetResourceHandle(), nImage, L"PNG");
if (m_pImage == NULL)
return;
}
void CXSPngButton::InitButton(UINT nNormalImageID,UINT nHoverImageID,UINT nClickedImageID,UINT nBtnType /*= BTN_TYPE_NORMAL*/)
{
m_nBtnType = nBtnType;
m_pImage = LoadImageFromResource(AfxGetResourceHandle(), nNormalImageID, L"PNG");
m_pHoverImage = LoadImageFromResource(AfxGetResourceHandle(), nHoverImageID, L"PNG");
if (m_pImage == NULL ||m_pHoverImage == NULL)
return;
}
BEGIN_MESSAGE_MAP(CXSPngButton, CButton)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_CONTROL_REFLECT(BN_CLICKED, &CXSPngButton::OnBnClicked)
ON_MESSAGE(WM_MOUSEHOVER,OnMouseHOver)
ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
END_MESSAGE_MAP()
BOOL CXSPngButton::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CXSPngButton::OnPaint()
{
CButton::OnPaint();
}
// CXSPngButton 消息处理程序
void CXSPngButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (!IsWindowEnabled())
m_nState = CTRL_DISABLE;
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
if(m_bIsMouseHover)
DrawButtonImage(&dc, m_pHoverImage,m_nState);
else
DrawButtonImage(&dc, m_pImage,m_nState);
}
bool CXSPngButton::DrawButtonImage(CDC* pDC, Image* pImage, UINT nState)
{
bool result = false;
if (pImage == NULL)
return result;
CRect rcButton;
rcButton = CRect(0, 0, pImage->GetWidth(), pImage->GetHeight());
Graphics graph(pDC->GetSafeHdc());
graph.SetSmoothingMode(SmoothingMode::SmoothingModeHighQuality);
graph.DrawImage(pImage, 0, 0, rcButton.left, rcButton.top, rcButton.Width(), rcButton.Height(), UnitPixel);
graph.ReleaseHDC(pDC->GetSafeHdc());
result = true;
return result;
}
Image *CXSPngButton::LoadImageFromResource(HINSTANCE hInstance, UINT uImgID, LPCTSTR lpType)
{
HRSRC hResInfo = ::FindResource(hInstance, MAKEINTRESOURCE(uImgID), lpType);
if (hResInfo == NULL)
return NULL; //fail
DWORD dwSize;
dwSize = SizeofResource(hInstance, hResInfo); //get resource size(bytes)
HGLOBAL hResData;
hResData = ::LoadResource(hInstance, hResInfo);
if (hResData == NULL)
return NULL; //fail
HGLOBAL hMem;
hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
if (hMem == NULL){
::FreeResource(hResData);
return NULL;
}
LPVOID lpResData, lpMem;
lpResData = ::LockResource(hResData);
lpMem = ::GlobalLock(hMem);
::CopyMemory(lpMem, lpResData, dwSize); //copy memory
::GlobalUnlock(hMem);
::FreeResource(hResData); //free memory
IStream *pStream;
HRESULT hr;
hr = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);//create stream object
Image *pImage = NULL;
if (SUCCEEDED(hr)){
pImage = Image::FromStream(pStream);//get GDI+ pointer
pStream->Release();
}
::GlobalFree(hMem);
return pImage;
}
void CXSPngButton::PreSubclassWindow()
{
ModifyStyle(0, BS_OWNERDRAW);
if (NULL != GetSafeHwnd())
{
if (!(GetButtonStyle() & WS_CLIPSIBLINGS))
SetWindowLong(GetSafeHwnd(), GWL_STYLE, GetWindowLong(GetSafeHwnd(),
GWL_STYLE) | WS_CLIPSIBLINGS);
}
CButton::PreSubclassWindow();
}
void CXSPngButton::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_nState != CTRL_FOCUS)
m_nState = CTRL_FOCUS;
PaintParent();
CButton::OnLButtonDown(nFlags, point);
}
void CXSPngButton::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_nState != CTRL_FOCUS)
{
m_nState = CTRL_FOCUS;
PaintParent();
}
CButton::OnLButtonUp(nFlags, point);
}
void CXSPngButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(!m_bTracked)
{
TRACKMOUSEEVENT tme;
ZeroMemory(&tme,sizeof(TRACKMOUSEEVENT ));
tme.cbSize = sizeof(TRACKMOUSEEVENT );
tme.dwFlags = TME_HOVER|TME_LEAVE;
tme.dwHoverTime = 1;
tme.hwndTrack = this->GetSafeHwnd();
if(::_TrackMouseEvent(&tme))
{
m_bTracked = true;
m_bIsMouseHover = true;
}
}
//RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
CButton::OnMouseMove(nFlags, point);
}
LRESULT CXSPngButton::OnMouseHOver(WPARAM wParam,LPARAM lParam)
{
//鼠标放上去
m_bIsMouseHover = true;
if(m_nState != CTRL_FOCUS)
{
m_nState = CTRL_FOCUS;
PaintParent();
}
//RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
return 0;
}
LRESULT CXSPngButton::OnMouseLeave(WPARAM wParam,LPARAM lParam)
{
//鼠标移开时
m_bTracked=false;
m_bIsMouseHover = false;
if (m_nBtnType == BTN_TYPE_NORMAL)
m_nState = CTRL_NOFOCUS;
PaintParent();
//RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
return 0;
}
void CXSPngButton::PaintParent()
{
CRect rect;
GetWindowRect(&rect);
GetParent()-> ScreenToClient(&rect);
GetParent()-> InvalidateRect(&rect);
}
void CXSPngButton::OnBnClicked()
{
AfxMessageBox(TEXT("妈蛋,谁让你点我的!"));
//CButton::OnBnClicked();
}