266 lines
5.6 KiB
C++
Executable File
266 lines
5.6 KiB
C++
Executable File
// ThumbIcon.cpp : implementation file
|
|
//
|
|
|
|
#include "stdafx.h"
|
|
#include "musicplayer.h"
|
|
#include "ThumbIcon.h"
|
|
|
|
#ifdef _DEBUG
|
|
#define new DEBUG_NEW
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[] = __FILE__;
|
|
#endif
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CThumbIcon
|
|
|
|
CThumbIcon::CThumbIcon()
|
|
{
|
|
m_txtFont.CreateFont(12,0,0,0,FW_BOLD,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,
|
|
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial"));
|
|
|
|
m_NormalID = 0;
|
|
m_DownID = 0;
|
|
m_ImageID = 0;
|
|
m_InitDC = false;
|
|
m_IsSelected = false;
|
|
m_Title.Empty();
|
|
m_ShowIcon = FALSE;
|
|
m_BmpSize = CSize(0,0);
|
|
m_txtColor = RGB(255,255,255);
|
|
m_IsDown = FALSE;
|
|
}
|
|
|
|
CThumbIcon::~CThumbIcon()
|
|
{
|
|
m_NormalDC.DeleteDC();
|
|
m_DownDC.DeleteDC();
|
|
m_BackgroundDC.DeleteDC();
|
|
|
|
m_txtFont.DeleteObject();
|
|
m_MemDC.DeleteDC();
|
|
}
|
|
|
|
|
|
BEGIN_MESSAGE_MAP(CThumbIcon, CButton)
|
|
//{{AFX_MSG_MAP(CThumbIcon)
|
|
ON_WM_ERASEBKGND()
|
|
ON_WM_PAINT()
|
|
ON_WM_LBUTTONDOWN()
|
|
ON_WM_KILLFOCUS()
|
|
//}}AFX_MSG_MAP
|
|
END_MESSAGE_MAP()
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CThumbIcon message handlers
|
|
|
|
void CThumbIcon::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
|
|
{
|
|
|
|
}
|
|
|
|
void CThumbIcon::PreSubclassWindow()
|
|
{
|
|
// TODO: Add your specialized code here and/or call the base class
|
|
// 在此设置按钮样式为自绘按钮
|
|
ModifyStyle(0, GetStyle()|BS_OWNERDRAW);
|
|
|
|
CButton::PreSubclassWindow();
|
|
}
|
|
|
|
BOOL CThumbIcon::OnEraseBkgnd(CDC* pDC)
|
|
{
|
|
// TODO: Add your message handler code here and/or call default
|
|
if (!m_InitDC && m_ImageID)
|
|
{
|
|
// 建立临时对象从资源中加载图片
|
|
CDC tmpDC;
|
|
tmpDC.CreateCompatibleDC(NULL);
|
|
|
|
CBitmap tmpBmp;
|
|
tmpBmp.LoadBitmap(m_ImageID);
|
|
tmpDC.SelectObject(&tmpBmp);
|
|
|
|
tmpBmp.DeleteObject();
|
|
|
|
// 计算按钮在窗口内的位置
|
|
CRect rect;
|
|
GetClientRect(&rect);
|
|
|
|
// 获取按钮的宽和高
|
|
int cx = rect.Width();
|
|
int cy = rect.Height();
|
|
|
|
// 主/子窗体矩形区域
|
|
CRect mainWnd;
|
|
CRect subWnd;
|
|
|
|
// 获取父窗体矩形
|
|
CWnd *pWnd = GetParent();
|
|
pWnd->GetWindowRect(&mainWnd);
|
|
|
|
// 获取按钮矩形
|
|
GetWindowRect(&subWnd);
|
|
|
|
// 计算相对位置
|
|
CPoint tpLeft;
|
|
tpLeft.x = subWnd.TopLeft().x - mainWnd.TopLeft().x;
|
|
tpLeft.y = subWnd.TopLeft().y - mainWnd.TopLeft().y;
|
|
|
|
// 复制图片到内存中
|
|
tmpBmp.CreateCompatibleBitmap(&tmpDC,cx,cy);
|
|
|
|
m_BackgroundDC.CreateCompatibleDC(NULL);
|
|
m_BackgroundDC.SelectObject(&tmpBmp);
|
|
|
|
m_BackgroundDC.BitBlt(0,0,cx,cy,&tmpDC,tpLeft.x,tpLeft.y,SRCCOPY);
|
|
|
|
// 清理资源
|
|
tmpBmp.DeleteObject();
|
|
tmpDC.DeleteDC();
|
|
|
|
tmpBmp.CreateCompatibleBitmap(pDC,cx,cy);
|
|
m_MemDC.CreateCompatibleDC(pDC);
|
|
m_MemDC.SelectObject(&tmpBmp);
|
|
m_MemDC.SelectObject(&m_txtFont);
|
|
m_MemDC.SetBkMode(TRANSPARENT);
|
|
tmpBmp.DeleteObject();
|
|
m_InitDC = true;
|
|
}
|
|
return FALSE;
|
|
//return CButton::OnEraseBkgnd(pDC);
|
|
}
|
|
|
|
void CThumbIcon::OnPaint()
|
|
{
|
|
CPaintDC dc(this); // device context for painting
|
|
|
|
// TODO: Add your message handler code here
|
|
CRect rc;
|
|
GetClientRect(&rc);
|
|
|
|
|
|
if(m_ShowIcon)
|
|
{
|
|
if (m_NormalID && m_DownID)
|
|
{
|
|
if (!m_IsSelected)
|
|
{
|
|
m_MemDC.SetTextColor(RGB(255,255,255));
|
|
m_MemDC.BitBlt(0,0,rc.Width(),rc.Height(),&m_NormalDC,0,0,SRCCOPY);
|
|
}
|
|
else
|
|
{
|
|
m_MemDC.SetTextColor(RGB(10,0,0));
|
|
m_MemDC.BitBlt(0,0,rc.Width(),rc.Height(),&m_DownDC,0,0,SRCCOPY);
|
|
}
|
|
|
|
m_MemDC.DrawText(m_Title,&rc,DT_BOTTOM|DT_LEFT);
|
|
|
|
CDC tmpDC;
|
|
CBitmap tmpBmp;
|
|
|
|
tmpBmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
|
|
tmpDC.CreateCompatibleDC(&dc);
|
|
|
|
tmpDC.SelectObject(&tmpBmp);
|
|
tmpDC.BitBlt(0,0,rc.Width(),rc.Height(),&m_BackgroundDC,0,0,SRCCOPY);
|
|
|
|
TransparentImage(tmpDC.m_hDC,0,0,rc.Width(),rc.Height(),
|
|
m_MemDC.m_hDC,0,0,m_BmpSize.cx,m_BmpSize.cy,m_BgColor);
|
|
|
|
dc.BitBlt(0,0,rc.Width(),rc.Height(),&tmpDC,0,0,SRCCOPY);
|
|
|
|
|
|
tmpDC.DeleteDC();
|
|
tmpBmp.DeleteObject();
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dc.BitBlt(0,0,rc.Width(),rc.Height(),&m_BackgroundDC,0,0,SRCCOPY);
|
|
}
|
|
// Do not call CButton::OnPaint() for painting messages
|
|
}
|
|
|
|
void CThumbIcon::LoadIcon(int nNormalID, int nDownID, int nImageID, COLORREF BgColor)
|
|
{
|
|
m_NormalID = nNormalID;
|
|
m_DownID = nDownID;
|
|
m_ImageID = nImageID;
|
|
m_BgColor = BgColor;
|
|
// 初始化背景画刷
|
|
CBitmap tmpBmp;
|
|
BITMAP bmpInfo;
|
|
// 从资源中加载背景图片
|
|
tmpBmp.LoadBitmap(m_NormalID);
|
|
tmpBmp.GetBitmap(&bmpInfo);
|
|
|
|
m_BmpSize = CSize(bmpInfo.bmWidth,bmpInfo.bmHeight);
|
|
m_NormalDC.DeleteDC();
|
|
m_DownDC.DeleteDC();
|
|
m_NormalDC.CreateCompatibleDC(NULL);
|
|
m_NormalDC.SelectObject(&tmpBmp);
|
|
|
|
tmpBmp.DeleteObject();
|
|
|
|
tmpBmp.LoadBitmap(m_DownID);
|
|
m_DownDC.CreateCompatibleDC(NULL);
|
|
m_DownDC.SelectObject(&tmpBmp);
|
|
|
|
tmpBmp.DeleteObject();
|
|
}
|
|
|
|
void CThumbIcon::ShowIcon(bool bShow)
|
|
{
|
|
m_ShowIcon = bShow;
|
|
}
|
|
|
|
CString CThumbIcon::GetButtonTitle()
|
|
{
|
|
return m_Title;
|
|
}
|
|
|
|
void CThumbIcon::SetButtonTitle(CString sTitle)
|
|
{
|
|
m_Title = sTitle;
|
|
}
|
|
|
|
void CThumbIcon::SetChecked(bool bCheck)
|
|
{
|
|
m_IsSelected = bCheck;
|
|
Invalidate();
|
|
}
|
|
|
|
bool CThumbIcon::GetChecked()
|
|
{
|
|
return m_IsDown;
|
|
}
|
|
|
|
void CThumbIcon::OnLButtonDown(UINT nFlags, CPoint point)
|
|
{
|
|
// TODO: Add your message handler code here and/or call default
|
|
m_IsSelected = !m_IsSelected;
|
|
|
|
if (m_IsSelected)
|
|
{
|
|
m_IsDown = true;
|
|
}
|
|
else
|
|
{
|
|
m_IsDown = false;
|
|
}
|
|
|
|
Invalidate();
|
|
CButton::OnLButtonDown(nFlags, point);
|
|
}
|
|
|
|
void CThumbIcon::OnKillFocus(CWnd* pNewWnd)
|
|
{
|
|
CButton::OnKillFocus(pNewWnd);
|
|
|
|
// TODO: Add your message handler code here
|
|
m_IsSelected = FALSE;
|
|
}
|