// ButtonEx.cpp : 实现文件
//
#include "stdafx.h"
#include "ButtonEx.h"
// CButtonEx
IMPLEMENT_DYNAMIC(CButtonEx, CButton)
/*
* 构造函数
*/
CButtonEx::CButtonEx()
{
m_if_disable = false;
m_if_hover = false;
m_if_hand = false;
m_if_click = false;
m_if_toolTip = false;
m_mouseTrack = true;
m_imageWidth = 0;
m_imageHeight = 0;
m_buttonWidth = 0;
m_buttonHeight = 0;
m_cursor = ::LoadCursor(NULL, IDC_HAND);
}
/*
* 析构函数
*/
CButtonEx::~CButtonEx()
{
// 释放资源
if(m_image != NULL){
delete m_image;
m_image = NULL;
}
if(m_graphics != NULL){
delete m_graphics;
m_graphics = NULL;
}
}
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
/*
* 子窗口预处理(改成自绘模式)
*/
void CButtonEx::PreSubclassWindow()
{
// 改成自绘模式
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
/*
* 重绘按钮
*/
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
Rect imageRect;
UINT state;
// 生成绘制区域大小,获得当前按钮状态
imageRect = Rect(0, 0, m_imageWidth / 4, m_imageHeight);
state = lpDrawItemStruct->itemState;
if(m_graphics == NULL)
return;
if(m_image == NULL)
return;
// 按钮禁用状态
if(m_if_disable == true)
m_graphics->DrawImage(m_image, imageRect, BUTTON_DISABLE * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
// 按钮按下状态
else if(state & ODS_SELECTED)
m_graphics->DrawImage(m_image, imageRect, BUTTON_CLICK * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
// 鼠标在按钮上
else if(m_if_hover == true) {
if(m_buttonType == SPECIAL && m_if_click == true)
m_graphics->DrawImage(m_image, imageRect, BUTTON_CLICK * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
else
m_graphics->DrawImage(m_image, imageRect, BUTTON_HOVER * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
}
// 按钮正常状态
else {
if(m_buttonType == SPECIAL && m_if_click == true)
m_graphics->DrawImage(m_image, imageRect, BUTTON_CLICK * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
else
m_graphics->DrawImage(m_image, imageRect, BUTTON_ENABLE * m_imageWidth / 4, 0, m_imageWidth / 4, m_imageHeight, UnitPixel);
}
}
/*
* 拦截消息预处理
*/
BOOL CButtonEx::PreTranslateMessage(MSG* pMsg)
{
if(m_if_toolTip == true)
m_toolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
/*
* 鼠标在控件区域上移动时
*/
void CButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
// 鼠标要追踪WM_MOUSELEAVE和WM_MOUSEHOVER事件
if (m_mouseTrack) {
TRACKMOUSEEVENT csTME;
csTME.cbSize = sizeof(csTME);
csTME.dwFlags = TME_LEAVE | TME_HOVER; // 指定要追踪的事件
csTME.dwHoverTime = 50;
csTME.hwndTrack = m_hWnd; // 指定要追踪的窗口
::_TrackMouseEvent(&csTME); // 开启Windows的WM_MOUSELEAVE事件支持
m_mouseTrack = false; // 若已经追踪,则停止追踪
}
// 如果要求鼠标形状为手型
if(m_if_hand == true)
::SetCursor(m_cursor);
CButton::OnMouseMove(nFlags, point);
}
/*
* 鼠标停在控件区域上时
*/
void CButtonEx::OnMouseHover(UINT nFlags, CPoint point)
{
// 鼠标在按钮上
m_if_hover = true;
UpdateCtrlArea();
CButton::OnMouseHover(nFlags, point);
}
/*
* 鼠标离开控件区域时
*/
void CButtonEx::OnMouseLeave()
{
// 鼠标离开按钮
m_if_hover = false;
// 开始追踪
m_mouseTrack = true;
UpdateCtrlArea();
CButton::OnMouseLeave();
}
/*
* 鼠标左键按下
*/
void CButtonEx::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_buttonType == SPECIAL) {
if(m_if_click == true)
m_if_click = false;
else
m_if_click = true;
}
UpdateCtrlArea();
CButton::OnLButtonDown(nFlags, point);
}
/*
* 鼠标左键抬起
*/
void CButtonEx::OnLButtonUp(UINT nFlags, CPoint point)
{
UpdateCtrlArea();
CButton::OnLButtonUp(nFlags, point);
}
/*
* 更新控件区域
*/
void CButtonEx::UpdateCtrlArea()
{
CRect rc;
GetWindowRect(rc);
RedrawWindow();
GetParent()->ScreenToClient(rc);
GetParent()->InvalidateRect(rc, TRUE);
GetParent()->UpdateWindow();
}
/*
* 设置图片路径
*
* @param imagePath 图片路径
*/
void CButtonEx::SetImage(CString imagePath)
{
CWnd* pWnd;
CRect buttonRect;
// 加载图片,并设置图片的宽和高
m_image = new Image(imagePath);
m_imageWidth = m_image->GetWidth();
m_imageHeight = m_image->GetHeight();
// 重新调整按钮的大小(以适应图片的大小
pWnd = this->GetParent();
GetWindowRect( &buttonRect );
pWnd -> ScreenToClient(buttonRect);
buttonRect.right = buttonRect.left + m_imageWidth / 4;
buttonRect.bottom = buttonRect.top + m_imageHeight;
SetWindowPos(NULL, 0, 0, buttonRect.Width(), buttonRect.Height(), SWP_NOMOVE);
// 初始化m_graphics
m_graphics = new Graphics(GetWindowDC()->m_hDC);
}
/*
* 设置手型鼠标
*
* @param ifHand(true | false)
*/
void CButtonEx::SetHand(bool ifHand)
{
m_if_hand = ifHand;
}
/*
* 设置按钮类型
*
* @param type(NORMAL | SPECIAL)
*/
void CButtonEx::SetType(int type)
{
m_buttonType = type;
}
/*
* 设置消息提示
*
* @param strText
*/
void CButtonEx::SetToolTip(CString strText)
{
m_if_toolTip = true;
m_toolTip.Create(this);
m_toolTip.Activate(TRUE);
m_toolTip.AddTool(this, strText);
m_toolTip.SetDelayTime(300);
}