📄 bitmapsk.cpp
字号:
// BitmapSK.cpp: implementation of the CBitmapSK class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "CmmbPlayer.h"
#include "BitmapSK.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
///{{{{ for global function
///HOWTO: Drawing Transparent Bitmaps see: Microsoft Knowledge Base Article - Q79212
// TransparentBlt - Copies a bitmap transparently onto the destination DC
// hdcDes - Handle to destination device context
// xDes - x-coordinate of destination rectangle's upper-left corner
// yDes - y-coordinate of destination rectangle's upper-left corner
// width - Width of destination rectangle
// height - height of destination rectangle
// hbmSrc - Handle of the source bitmap
// xSrc - x-coordinate of source rectangle's upper-left corner
// ySrc - y-coordinate of source rectangle's upper-left corner
// crTrans - The transparent color
// hPal - Logical palette to be used with bitmap. Can be NULL
BOOL TransparentBitBlt(HDC hdcDes,
int xDes,
int yDes,
int width,
int height,
HBITMAP hbmSrc,
int xSrc,
int ySrc,
COLORREF crTrans,
HPALETTE hPal
)
{
ASSERT(hdcDes!=NULL);//if(hdcDes == NULL) return FALSE;
ASSERT(hbmSrc!=NULL);//if(hbmSrc == NULL) return FALSE;
CDC dc, memDC, maskDC, tempDC;
dc.Attach(hdcDes);
maskDC.CreateCompatibleDC(&dc);
CBitmap maskBmp;
//add these to store return of SelectObject() calls
CBitmap *pOldMemBmp = NULL;
CBitmap *pOldMaskBmp = NULL;
HBITMAP hOldTempBmp = NULL;
memDC.CreateCompatibleDC(&dc);
tempDC.CreateCompatibleDC(&dc);
CBitmap bmpImage;
bmpImage.CreateCompatibleBitmap(&dc, width, height);
pOldMemBmp = memDC.SelectObject(&bmpImage);
// Select and realize the palette
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && hPal )
{
::SelectPalette(dc, hPal, FALSE);
dc.RealizePalette();
::SelectPalette(memDC, hPal, FALSE);
}
hOldTempBmp = (HBITMAP)::SelectObject(tempDC.m_hDC, hbmSrc);
memDC.BitBlt(0, 0, width, height, &tempDC, xSrc, ySrc, SRCCOPY);
// Create monochrome bitmap for the mask
maskBmp.CreateBitmap(width, height, 1, 1, NULL);
pOldMaskBmp = maskDC.SelectObject(&maskBmp);
memDC.SetBkColor(crTrans);
// Create the mask from the memory DC
maskDC.BitBlt(0, 0, width, height, &memDC, 0, 0, SRCCOPY);
// Set the background in memDC to black. Using SRCPAINT with black
// and any other color results in the other color, thus making
// black the transparent color
memDC.SetBkColor(RGB(0,0,0));
memDC.SetTextColor(RGB(255,255,255));
memDC.BitBlt(0, 0, width, height, &maskDC, 0, 0, SRCAND);
// Set the foreground to black. See comment above.
dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,0));
dc.BitBlt(xDes, yDes, width, height, &maskDC, 0, 0, SRCAND);
// Combine the foreground with the background
dc.BitBlt(xDes, yDes, width, height, &memDC, 0, 0, SRCPAINT);
if (hOldTempBmp)
::SelectObject(tempDC.m_hDC, hOldTempBmp);
if (pOldMaskBmp)
maskDC.SelectObject(pOldMaskBmp);
if (pOldMemBmp)
memDC.SelectObject(pOldMemBmp);
dc.Detach();
maskBmp.DeleteObject();
bmpImage.DeleteObject();
maskDC.DeleteDC();
memDC.DeleteDC();
tempDC.DeleteDC();
pOldMemBmp = NULL;
pOldMaskBmp = NULL;
hOldTempBmp = NULL;
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBitmapSK::CBitmapSK()
{
}
CBitmapSK::~CBitmapSK()
{
}
BOOL CBitmapSK::LoadBitmapEx(LPCTSTR lpszFileName)
{
DeleteObject();
ASSERT(m_hObject==NULL);
HBITMAP hBitmap = NULL;
hBitmap = (HBITMAP)::LoadImage(0, lpszFileName, IMAGE_BITMAP, 0, 0, 0);
if(hBitmap == NULL) return FALSE;
return CBitmap::Attach(hBitmap);
}
BOOL CBitmapSK::LoadBitmapEx(LPCTSTR lpszResourceName, UINT nIDResource)
{
DeleteObject();
ASSERT(m_hObject==NULL);
if (lpszResourceName != NULL)
{
return CBitmap::LoadBitmap(lpszResourceName);
}
if (nIDResource != 0)
{
return CBitmap::LoadBitmap(nIDResource);
}
return FALSE;
}
BOOL CBitmapSK::Draw(CDC *pDC, LPRECT lprDes)
{
ASSERT(pDC!=NULL);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap *bmp = dc.SelectObject(this);
pDC->BitBlt(lprDes->left, lprDes->top,
lprDes->right - lprDes->left, lprDes->bottom - lprDes->top,
&dc, 0, 0, SRCCOPY);
dc.SelectObject(bmp);
dc.DeleteDC();
bmp = NULL;
return TRUE;
}
BOOL CBitmapSK::Draw(CDC *pDC, LPRECT lprDes, COLORREF crTrans, BOOL isTrans)
{
ASSERT(pDC!=NULL);
if (!isTrans)
{
return Draw(pDC, lprDes);
}
else
{
return TransparentBitBlt(pDC->GetSafeHdc(),
lprDes->left,lprDes->top,Width(),Height(),
(HBITMAP)this->GetSafeHandle(),0,0,crTrans,NULL);
}
}
BOOL CBitmapSK::Draw( CDC *pDC, int x, int y, LPRECT lprSrc)
{
ASSERT(pDC!=NULL);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap *pOldbmp = dc.SelectObject(this);
if (lprSrc != NULL)
{
//Copies a bitmap from the source device context to this current device context
pDC->BitBlt(x, y, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,
&dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
else
{
pDC->BitBlt(x, y, Width(), Height(), &dc, 0, 0, SRCCOPY);
}
dc.SelectObject(pOldbmp);
dc.DeleteDC();
pOldbmp = NULL;
return TRUE;
}
BOOL CBitmapSK::Draw( CDC *pDC, int x, int y, LPRECT lprSrc, COLORREF crTrans, BOOL isTrans)
{
ASSERT(pDC!=NULL);
if (!isTrans)
{
return Draw(pDC, x, y, lprSrc);
}
else
{
return TransparentBitBlt(pDC->GetSafeHdc(), x, y, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,
(HBITMAP)this->GetSafeHandle(), lprSrc->left, lprSrc->top, crTrans, NULL );
}
}
BOOL CBitmapSK::Fill(CDC *pDC, LPRECT lprDes, LPRECT lprSrc)
{
ASSERT(pDC!=NULL);
int widthDes, widthSrc, heightDes, heightSrc;
widthDes = abs(lprDes->right - lprDes->left);
widthSrc = abs(lprSrc->right - lprSrc->left);
heightDes = abs(lprDes->bottom - lprDes->top);
heightSrc = abs(lprSrc->bottom - lprSrc->top);
int paddingx, paddingy, i, j;
paddingx = int(widthDes / widthSrc);
paddingy = int(heightDes / heightSrc);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap *pOldbmp = dc.SelectObject(this);
if (widthDes <= widthSrc)
{
if (heightDes <= heightSrc)
{
Draw(pDC, lprDes);
}
else
{
for (i=1; i<paddingy; i++)
{
pDC->BitBlt(lprDes->left, lprDes->top + heightSrc*(i-1), widthDes, heightSrc,&dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
pDC->BitBlt(lprDes->left,lprDes->top + heightSrc*(i-1), widthDes, heightDes - heightSrc*(i-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
}
else
{
if (heightDes <= heightSrc)
{
for (i=1; i<paddingx; i++)
{
pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top, widthSrc, heightDes, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top, widthDes - widthSrc*(i-1), heightDes, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
else
{
for (j=1; j<=paddingy; j++)
{
for (i=1; i<=paddingx; i++)
{
pDC->BitBlt(lprDes->left + widthSrc*(i-1),lprDes->top + heightSrc*(j-1), widthSrc, heightSrc, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1),widthDes - widthSrc*(i-1), heightSrc, &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
}
for (i=1; i<=paddingx; i++)
{
pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1), widthSrc, heightDes - heightSrc*(j-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
pDC->BitBlt(lprDes->left + widthSrc*(i-1), lprDes->top + heightSrc*(j-1), widthDes - widthSrc*(i-1),
heightDes - heightSrc*(j-1), &dc, lprSrc->left, lprSrc->top, SRCCOPY);
}
dc.SelectObject(pOldbmp);
dc.DeleteDC();
pOldbmp = NULL;
return TRUE;
}
BOOL CBitmapSK::TransparentDraw(CDC * pDC, int x, int y, COLORREF crTrans)
{
ASSERT(pDC!=NULL);
return TransparentBitBlt(pDC->GetSafeHdc(), x, y, Width(), Height(), (HBITMAP)this->GetSafeHandle(),
0, 0, crTrans, NULL );
}
BOOL CBitmapSK::StretchDraw(CDC *pDC, LPRECT lprDes, LPRECT lprSrc)
{
ASSERT(pDC!=NULL);
if(lprDes == NULL) return FALSE;
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap *pOldbmp = dc.SelectObject(this);
//SetStretchBltMode(pDC->GetSafeHdc(), COLORONCOLOR);
if(!lprSrc)
{
pDC->StretchBlt(lprDes->left, lprDes->top, lprDes->right, lprDes->bottom, &dc, 0, 0, Width(), Height(), SRCCOPY);
}
else
{
pDC->StretchBlt(lprDes->left, lprDes->top, lprDes->right - lprDes->left, lprDes->bottom - lprDes->top,
&dc, lprSrc->left, lprSrc->top, lprSrc->right - lprSrc->left, lprSrc->bottom - lprSrc->top,SRCCOPY);
}
dc.SelectObject(pOldbmp);
dc.DeleteDC();
pOldbmp = NULL;
return TRUE;
}
BOOL CBitmapSK::StretchDraw(CDC *pDC, LPRECT lprSrc)
{
ASSERT(pDC!=NULL);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap *pOldbmp = dc.SelectObject(this);
pDC->StretchBlt(lprSrc->left, lprSrc->top, lprSrc->right, lprSrc->bottom, &dc,
0, 0, Width(), Height(), SRCCOPY);
dc.SelectObject(pOldbmp);
dc.DeleteDC();
pOldbmp = NULL;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -