// HoverButton.cpp : implementation file
//
#include "stdafx.h"
#include "HoverButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHoverButton
CHoverButton::CHoverButton()
{
m_bHover = FALSE;
m_bTracking = FALSE;
}
CHoverButton::~CHoverButton()
{
}
IMPLEMENT_DYNAMIC(CHoverButton, CBitmapButton)
BEGIN_MESSAGE_MAP(CHoverButton, CBitmapButton)
//{{AFX_MSG_MAP(CHoverButton)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////
// CHoverButton message handlers
void CHoverButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE|TME_HOVER;
tme.dwHoverTime = 1;
m_bTracking = _TrackMouseEvent(&tme);
}
CBitmapButton::OnMouseMove(nFlags, point);
}
BOOL CHoverButton::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
// Set the tooltip with a string resource
void CHoverButton::SetToolTipText(int nId, BOOL bActivate)
{
CString sText;
// load string resource
sText.LoadString(nId);
// If string resource is not empty
if (sText.IsEmpty() == FALSE) SetToolTipText(&sText, bActivate);
}
// Set the tooltip with a CString
void CHoverButton::SetToolTipText(CString *spText, BOOL bActivate)
{
// We cannot accept NULL pointer
if (spText == NULL) return;
// Initialize ToolTip
InitToolTip();
// If there is no tooltip defined then add it
if (m_ToolTip.GetToolCount() == 0)
{
CRect rectBtn;
GetClientRect(rectBtn);
m_ToolTip.AddTool(this, (LPCTSTR)*spText, rectBtn, 1);
}
// Set text for tooltip
m_ToolTip.UpdateTipText((LPCTSTR)*spText, this, 1);
m_ToolTip.Activate(bActivate);
}
void CHoverButton::InitToolTip()
{
if (m_ToolTip.m_hWnd == NULL)
{
// Create ToolTip control
m_ToolTip.Create(this);
// Create inactive
m_ToolTip.Activate(FALSE);
}
} // End of InitToolTip
// Activate the tooltip
void CHoverButton::ActivateTooltip(BOOL bActivate)
{
// If there is no tooltip then do nothing
if (m_ToolTip.GetToolCount() == 0) return;
// Activate tooltip
m_ToolTip.Activate(bActivate);
} // End of EnableTooltip
void CHoverButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CRect r_app;
int w,h;
this->GetClientRect(&r_app);
this->ClientToScreen(&r_app);
w=r_app.right-r_app.left;
h=r_app.bottom-r_app.top;
CDC *mydc=CDC::FromHandle(lpDrawItemStruct->hDC);
CDC * pMemDC = new CDC;
pMemDC -> CreateCompatibleDC(mydc);
CBitmap * pOldBitmap;
CBitmap * pNormalBitmap;
CBitmap * pHoverBitmap;
CBitmap * pClickBitmap;
pNormalBitmap = pMemDC -> SelectObject(&mybitmap);
BITMAP bm;
CPoint point(0,0);
if(lpDrawItemStruct->itemState & ODS_SELECTED)
{
my_click.GetBitmap(&bm);
pMemDC->SelectObject(&my_click);
pMemDC->SetStretchBltMode(STRETCH_DELETESCANS);
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
mydc->StretchBlt(0,0,w,h,pMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
else
{
if(m_bHover)
{
my_hover.GetBitmap(&bm);
pMemDC->SelectObject(&my_hover);
pMemDC->SetStretchBltMode(STRETCH_DELETESCANS);
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
mydc->StretchBlt(0,0,w,h,pMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}else
{
pMemDC ->SelectObject(&my_normal);
my_normal.GetBitmap(&bm);
pMemDC->SetStretchBltMode(STRETCH_DELETESCANS);
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
mydc->StretchBlt(0,0,w,h,pMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
}
// clean up
pMemDC -> SelectObject(pNormalBitmap);
delete pMemDC;
}
// Load a bitmap from the resources in the button, the bitmap has to have 3 buttonsstates next to each other: Up/Down/Hover
BOOL CHoverButton::LoadBitmap(UINT bitmapid)
{
mybitmap.Attach(::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(bitmapid), IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS));
BITMAP bitmapbits;
mybitmap.GetBitmap(&bitmapbits);
m_ButtonSize.cy=bitmapbits.bmHeight;
m_ButtonSize.cx=bitmapbits.bmWidth;
SetWindowPos( NULL, 0,0, m_ButtonSize.cx,m_ButtonSize.cy,SWP_NOMOVE |SWP_NOOWNERZORDER );
return TRUE;
}
BOOL CHoverButton::LoadBitmap(CString normal_name,CString hover_name,CString click_name)
{
my_normal.Detach();
my_click.Detach();
my_hover.Detach();
my_normal.Attach(::LoadImage(0, normal_name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
my_click.Attach(::LoadImage(0, click_name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
my_hover.Attach(::LoadImage(0, hover_name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
return TRUE;
}
BOOL CHoverButton::LoadBitmap(UINT id_normal,UINT id_hover,UINT id_click)
{
my_normal.Attach(::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(id_normal), IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS));
my_click.Attach(::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(id_click), IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS));
my_hover.Attach(::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(id_hover), IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS));
BITMAP bitmapbits;
my_normal.GetBitmap(&bitmapbits);
m_ButtonSize.cy=bitmapbits.bmHeight;
m_ButtonSize.cx=bitmapbits.bmWidth;
//SetWindowPos( NULL, 0,0, m_ButtonSize.cx,m_ButtonSize.cy,SWP_NOMOVE |SWP_NOOWNERZORDER );
return TRUE;
}
LRESULT CHoverButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
{
// TODO: Add your message handler code here and/or call default
m_bHover=TRUE;
Invalidate();
return true;
}
LRESULT CHoverButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
m_bTracking = FALSE;
m_bHover=FALSE;
Invalidate();
return 0;
}
void CHoverButton::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CBitmapButton::OnLButtonDblClk(nFlags, point);
}
void CHoverButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// Set text for tooltip
m_ToolTip.Activate(TRUE);
CBitmapButton::OnLButtonUp(nFlags, point);
}
- 1
- 2
前往页