// BmpStatic.cpp : implementation file // #include "stdafx.h" #include "musicplayer.h" #include "BmpStatic.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CBmpStatic CBmpStatic::CBmpStatic() { m_InitDC = FALSE; m_ImageID = IDB_MUSIC_WND; m_TxtFormat = DT_CENTER|DT_VCENTER; } CBmpStatic::~CBmpStatic() { m_BackgroundDC.DeleteDC(); m_txtFont.DeleteObject(); } BEGIN_MESSAGE_MAP(CBmpStatic, CStatic) //{{AFX_MSG_MAP(CBmpStatic) ON_WM_ERASEBKGND() ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBmpStatic message handlers BOOL CBmpStatic::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(); m_InitDC = true; } return TRUE; //return CStatic::OnEraseBkgnd(pDC); } void CBmpStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rc; GetClientRect(&rc); CDC tmpDC; CBitmap tmpBmp; tmpBmp.CreateCompatibleBitmap(&dc,320,240); tmpDC.CreateCompatibleDC(&dc); tmpDC.SelectObject(&tmpBmp); tmpDC.SelectObject(&m_txtFont); tmpDC.SetBkMode(TRANSPARENT); tmpDC.BitBlt(0,0,rc.Width(),rc.Height(),&m_BackgroundDC,0,0,SRCCOPY); CString str; GetWindowText(str); tmpDC.DrawText(str,&rc,m_TxtFormat); dc.BitBlt(0,0,rc.Width(),rc.Height(),&tmpDC,0,0,SRCCOPY); tmpDC.DeleteDC(); tmpBmp.DeleteObject(); // Do not call CStatic::OnPaint() for painting messages } void CBmpStatic::LoadBgBmp(int nBmpID) { m_ImageID = nBmpID; } void CBmpStatic::SetTxtFont(int nFont) { m_txtFont.DeleteObject(); switch(nFont) { case 0: m_txtFont.CreateFont(14,0,0,0,FW_BOLD,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial")); break; case 1: m_txtFont.CreateFont(32,0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial")); m_TxtFormat = DT_RIGHT|DT_BOTTOM; break; case 2: m_txtFont.CreateFont(16,0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("Arial")); m_TxtFormat = DT_LEFT|DT_BOTTOM; break; default: break; } }