📄 yctbitmapbutton.cpp
字号:
// yctBitmapButton.cpp : implementation file
//
#include "stdafx.h"
#include "MyButtonDlg.h"
#include "yctBitmapButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CyctBitmapButton
CyctBitmapButton::CyctBitmapButton(): m_fIsInit(FALSE)
{
}
CyctBitmapButton::~CyctBitmapButton()
{
}
BEGIN_MESSAGE_MAP(CyctBitmapButton, CButton)
//{{AFX_MSG_MAP(CyctBitmapButton)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_DESTROY()
ON_WM_CREATE()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)//手工添加
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CyctBitmapButton message handlers
void CyctBitmapButton::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_MouseState.GetCurState() == _mouse_out)
{
SetBitmap(m_hIn);
return;
}
//当鼠标离开时触发WM_MOUSELEAVE事件
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
tme.dwHoverTime = HOVER_DEFAULT;
::TrackMouseEvent(&tme);
CButton::OnMouseMove(nFlags, point);
}
void CyctBitmapButton::OnLButtonDown(UINT nFlags, CPoint point)
{
SetBitmap(m_hDown);
CButton::OnLButtonDown(nFlags, point);
}
void CyctBitmapButton::OnLButtonUp(UINT nFlags, CPoint point)
{
SetBitmap(m_hIn);
CButton::OnLButtonUp(nFlags, point);
}
int CyctBitmapButton::Init()
{
//(窗口建立后的)初始化
m_hIn = ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_IN));
m_hOut = ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_OUT));
m_hDown = ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_DOWN));
ASSERT(m_hIn != NULL);
ASSERT(m_hOut != NULL);
ASSERT(m_hDown != NULL);
//SetButtonStyle( BS_BITMAP | GetButtonStyle() );
ModifyStyle(0,BS_BITMAP);//增加BS_BITMAP风格
SetBitmap(m_hOut);
return 0;
}
void CyctBitmapButton::OnDestroy()
{
CButton::OnDestroy();
//卸载Bitmap资源
#ifdef _DEBUG
ASSERT(0 != DeleteObject(m_hIn));
ASSERT(0 != DeleteObject(m_hOut));
ASSERT(0 != DeleteObject(m_hDown));
#else
DeleteObject(m_hIn);
DeleteObject(m_hOut);
DeleteObject(m_hPutdown);
#endif
}
void CyctBitmapButton::PreSubclassWindow()
{
//初始化
if(!m_fIsInit)
{
//AfxMessageBox(_T("CyctBitmapButton::PreSubclassWindow"));
Init();
m_fIsInit = TRUE;
}
CButton::PreSubclassWindow();
}
int CyctBitmapButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
//初始化
if(!m_fIsInit)
{
//AfxMessageBox(_T("CyctBitmapButton::OnCreate"));
Init();
m_fIsInit = TRUE;
}
return 0;
}
LRESULT CyctBitmapButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
SetBitmap(m_hOut);
return 0;
}
HBITMAP CyctBitmapButton::SetBitmap(HBITMAP hBitmap)
{
//设置按钮的位图
ASSERT(m_hIn == hBitmap || m_hOut == hBitmap || m_hDown == hBitmap);
if(m_hIn == hBitmap)
m_MouseState.SetCurState( _mouse_in );
else if(m_hOut == hBitmap)
m_MouseState.SetCurState(_mouse_out );
else if(m_hDown == hBitmap)
m_MouseState.SetCurState( _mouse_down );
return CButton::SetBitmap(hBitmap);
}
void CyctBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//
// TODO: Add your code to draw the specified item
ASSERT(FALSE);
}
void CyctBitmapButton::OnSetFocus(CWnd* pOldWnd)
{
//使函数体为空,去掉了虚框
//CButton::OnSetFocus(pOldWnd);
// TODO: Add your message handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -