// statbmp.cpp : implementation file
// Copyright 2001 by Troels K.
#include "stdafx.h"
#include "statbmp.h"
#include "mfcext.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static const char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Prototypes
BOOL MFC_Bitmap_LoadImage(CBitmap* pBitmap, LPCTSTR lpszFileName, int cxDesired = 0, int cyDesired = 0,
UINT fuLoad = LR_DEFAULTSIZE | LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS | LR_LOADFROMFILE,
HINSTANCE hInstance = NULL);
BOOL MFC_Bitmap_LoadImage(CBitmap* pBitmap, UINT nID, int cxDesired = 0, int cyDesired = 0,
UINT fuLoad = LR_DEFAULTSIZE | LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS,
HINSTANCE hInstance = NULL);
BOOL Bitmap_Draw(HDC hdc, POINT pt, HBITMAP bitmap, int iBitmap, int bitmap_count, HDC hdc_src);
BOOL MFC_Bitmap_Draw(CDC* pDC, POINT pt, HBITMAP bitmap, int iBitmap = -1, int bitmap_count = 0, CDC* hdc_src = NULL)
{
return Bitmap_Draw(pDC->m_hDC, pt, bitmap, iBitmap, bitmap_count, hdc_src->GetSafeHdc());
}
/////////////////////////////////////////////////////////////////////////////
// CStatusBarBitmap
CStatusBarBitmap::CStatusBarBitmap(int nStateCount) : CBitmap(), m_nStateCount(nStateCount), m_nIDCommand(0)
{
}
BOOL CStatusBarBitmap::LoadBitmap(int nIDBitmap, HINSTANCE hInstance /*= NULL*/)
{
return ::MFC_Bitmap_LoadImage(this, nIDBitmap, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS, hInstance);
}
BOOL CStatusBarBitmap::GrowMinHeight(CStatusBar* pStatusBar)
{
BITMAP bm;
BOOL bOK = GetBitmap(&bm);
if (bOK)
{
::MFC_StatusBar_GrowMinHeight(pStatusBar, bm.bmHeight+2+2);
}
return bOK;
}
BOOL CStatusBarBitmap::HookPane(CStatusBar* pStatusBar, int nPane, int cxExtra)
{
BITMAP bm;
BOOL bOK = GetBitmap(&bm) && (nPane != -1);
if (bOK)
{
UINT nID, nStyle;
int cxWidth;
pStatusBar->GetPaneInfo(nPane, nID, nStyle, cxWidth);
cxWidth = bm.bmWidth / m_nStateCount-3 + cxExtra;
pStatusBar->SetPaneInfo(nPane, nID, nStyle | SBT_OWNERDRAW | SBT_TOOLTIPS, cxWidth);
m_nIDCommand = nID;
}
return bOK;
}
BOOL CStatusBarBitmap::DrawItem(LPDRAWITEMSTRUCT dis, int iBitmap)
{
CDC* pDC = CDC::FromHandle(dis->hDC);
BITMAP bm;
VERIFY(GetBitmap(&bm));
const CRect& rect = (const CRect&)dis->rcItem;
// center
const CPoint pt (rect.left,//rect.left + (rect.Width () - bm.bmWidth / m_nStateCount)/2,
rect.top + (rect.Height() - bm.bmHeight)/2 );
return ::MFC_Bitmap_Draw(pDC, pt, operator HBITMAP(), iBitmap, m_nStateCount);
}
/////////////////////////////////////////////////////////////////////////////
// Implementation
BOOL MFC_Bitmap_LoadImage(CBitmap* bitmap, LPCTSTR lpszFileName, int cxDesired, int cyDesired, UINT fuLoad, HINSTANCE hInstance)
{
if (hInstance == NULL)
{
hInstance = AfxGetResourceHandle();
}
HBITMAP handle = (HBITMAP)::LoadImage(NULL, lpszFileName, IMAGE_BITMAP, cxDesired, cyDesired, fuLoad);
BOOL bOK = (NULL != handle);
if (bOK)
{
bitmap->DeleteObject();
bOK = bitmap->Attach(handle);
}
return bOK;
}
BOOL MFC_Bitmap_LoadImage(CBitmap* bitmap, UINT nIDResource, int cxDesired, int cyDesired, UINT fuLoad, HINSTANCE hInstance)
{
if (hInstance == NULL)
{
hInstance = AfxGetResourceHandle();
}
HBITMAP handle = (HBITMAP)::LoadImage(hInstance, MAKEINTRESOURCE(nIDResource), IMAGE_BITMAP, cxDesired, cyDesired, fuLoad);
BOOL bOK = (NULL != handle);
if (bOK)
{
bitmap->DeleteObject();
bOK = bitmap->Attach(handle);
}
return bOK;
}
BOOL Bitmap_Draw(HDC hdc, POINT pt, HBITMAP bitmap, int iBitmap, int bitmap_count, HDC hdc_src)
{
BITMAP bm;
BOOL bOK = GetObject(bitmap, sizeof(bm), &bm) && ((iBitmap == -1) || (iBitmap < bitmap_count));
if (bOK)
{
HDC temp = NULL;
HBITMAP old;
POINT pt_src;
SIZE size_dst;
if (iBitmap == -1)
{
size_dst.cx = bm.bmWidth;
pt_src.x = 0;
}
else
{
size_dst.cx = bm.bmWidth / bitmap_count;
pt_src.x = size_dst.cx * iBitmap;
}
pt_src.y = 0, size_dst.cy = bm.bmHeight;
if (hdc_src == NULL)
{
temp = hdc_src = CreateCompatibleDC(NULL);
}
old = (HBITMAP)SelectObject(hdc_src, bitmap);
bOK = StretchBlt(hdc, pt .x, pt .y, size_dst.cx, size_dst.cy, hdc_src,
pt_src.x, pt_src.y, size_dst.cx, size_dst.cy, SRCCOPY);
SelectObject(hdc_src, old);
if (temp != NULL)
{
DeleteDC(temp);
}
}
return bOK;
}
/////////////////////////////////////////////////////////////////////////////