📄 rtdraw.cpp
字号:
return TRUE;
}
lf->lfEscapement = 0;
lf->lfOrientation = 0;
CFont font;
font.CreateFontIndirect(lf);
pDC->SelectObject(&font);
while(ptr <= end)
{
//UNICODE
if(*ptr & 0x80)
{
if(ptr + 1 > end)break;
start = ptr;
ptr += 2;
sz = pDC->GetTextExtent(start,(int)(ptr - start));
CRect rtText;
rtText.SetRect(lpRect->left + (lpRect->right - lpRect->left - sz.cx)/2,
top,lpRect->right,lpRect->bottom);
pDC->TextOut(rtText.left,rtText.top, start,(int)(ptr - start));
top += sz.cy;
}
else
{
start = ptr;
ptr += 1;
sz = pDC->GetTextExtent(start,(int)(ptr - start));
CRect rtText;
rtText.SetRect(lpRect->left + (lpRect->right - lpRect->left - sz.cx)/2,
top,lpRect->right,lpRect->bottom);
pDC->TextOut(rtText.left,rtText.top, start,(int)(ptr - start));
top += sz.cy;
}
}
return TRUE;
}
//BITMAP Width and height > 9
BOOL CRTDraw::RTDrawBitmap(CDC* pDC, LPCRECT lpRect, CBitmap* pBitmap,UINT Mode)
{
if(pDC == NULL)return FALSE;
if(pBitmap == NULL)return FALSE;
BITMAP bitmap;
pBitmap->GetBitmap(&bitmap);
if(bitmap.bmWidth < 9)return FALSE;
if(bitmap.bmHeight < 9)return FALSE;
CDC srcDC;
srcDC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap = srcDC.SelectObject(pBitmap);
if(Mode == DrawModeState)
{
pDC->StretchBlt(lpRect->left,lpRect->top,bitmap.bmWidth,bitmap.bmHeight,
&srcDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
return TRUE;
}
if((DrawMode)Mode == DrawModeAllStretch)
{
pDC->StretchBlt(lpRect->left,lpRect->top,lpRect->right - lpRect->left,
lpRect->bottom - lpRect->top,&srcDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
srcDC.SelectObject(pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
int height;
int width;
if((DrawMode)Mode == DrawModeFill)
{
int left = lpRect->left;
int top = lpRect->top;
while(left < lpRect->right)
{
if(lpRect->right - left <= bitmap.bmWidth)
{
width = lpRect->right - left;
if(lpRect->bottom - top <= bitmap.bmHeight)
{
height = lpRect->bottom - top;
pDC->BitBlt(left,top,width,height,&srcDC,0,0,SRCCOPY);
break;
}
else
{
height = bitmap.bmHeight;
pDC->BitBlt(left,top,width,height,&srcDC,0,0,SRCCOPY);
top += height;
left = lpRect->left;
}
}
else
{
width = bitmap.bmWidth;
if(lpRect->bottom - top <= bitmap.bmHeight)
{
height = lpRect->bottom - top;
pDC->BitBlt(left,top,width,height,&srcDC,0,0,SRCCOPY);
}
else
{
height = bitmap.bmHeight;
pDC->BitBlt(left,top,width,height,&srcDC,0,0,SRCCOPY);
}
left += width;
}
}
srcDC.SelectObject(pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
if(Mode == DrawModeHeightStretch)
{
height = lpRect->bottom - lpRect->top;
int top = lpRect->top;
int left = lpRect->left;
int right = lpRect->right;
while((right - left) >= bitmap.bmWidth)
{
pDC->StretchBlt(left,top,bitmap.bmWidth,height,
&srcDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
left += bitmap.bmWidth;
}
width = right - left;
pDC->StretchBlt(left,top,width,height,
&srcDC,0,0,width,bitmap.bmHeight,SRCCOPY);
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
if(Mode == DrawModeWidthStretch)
{
height = lpRect->bottom - lpRect->top;
int top = lpRect->top;
width = lpRect->right - lpRect->left;
int left = lpRect->left;
int bottom = lpRect->bottom;
while((bottom - top) >= bitmap.bmHeight)
{
pDC->StretchBlt(left,top,width,bitmap.bmHeight,
&srcDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
top += bitmap.bmHeight;
}
height = bottom - top;
pDC->StretchBlt(left,top,width,height,
&srcDC,4,0,bitmap.bmWidth,height,SRCCOPY);
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
}
if(Mode == DrawMode3D)
{
int width = bitmap.bmWidth/3;
int height = bitmap.bmHeight/3;
int x = lpRect->left + width;
int y = lpRect->top + height;
//fill center
while( x + width < lpRect->right)
{
while(y + height < lpRect->bottom)
{
pDC->BitBlt(x,y,width,height,&srcDC,width,height,SRCCOPY);
y += height;
}
y = lpRect->top + height;
x += width;
}
//draw top border
x = lpRect->left + width;
y = lpRect->top;
pDC->BitBlt(lpRect->left,y,width,height,&srcDC,0,0,SRCCOPY);
while( x + width < lpRect->right)
{
pDC->BitBlt(x,y,width,height,&srcDC,width,0,SRCCOPY);
x += width;
}
pDC->BitBlt(lpRect->right - width,y,width,height,&srcDC,width + width,0,SRCCOPY);
//draw left border
x = lpRect->left;
y = lpRect->top + height;
while( y + height < lpRect->bottom)
{
pDC->BitBlt(x,y,width,height,&srcDC,0,height,SRCCOPY);
y += height;
}
//draw right border
x = lpRect->right - width;
y = lpRect->top + height;
while( y + height < lpRect->bottom)
{
pDC->BitBlt(x,y,width,height,&srcDC,width + width,height,SRCCOPY);
y += height;
}
//draw bottom border
x = lpRect->left + width;
y = lpRect->bottom - height;
pDC->BitBlt(lpRect->left,y,width,height,&srcDC,0,height + height,SRCCOPY);
while( x + width < lpRect->right)
{
pDC->BitBlt(x,y,width,height,&srcDC,width,height + height,SRCCOPY);
x += width;
}
pDC->BitBlt(lpRect->right - width,y,width,height,&srcDC,width + width,height + height,SRCCOPY);
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
if(Mode == DrawModeHeightStretch3D)
{
int width = bitmap.bmWidth / 3;
int height = lpRect->bottom - lpRect->top;
int x = lpRect->left;
int y = lpRect->top;
int right = lpRect->right;
pDC->StretchBlt(x,y,width,height,&srcDC,0,0,width,bitmap.bmHeight,SRCCOPY);
while((x + width) < right)
{
pDC->StretchBlt(x,y,width,height,&srcDC,width,0,width,bitmap.bmHeight,SRCCOPY);
x += width;
}
pDC->StretchBlt(right - width,y,width,height,&srcDC,width + width,0,width,bitmap.bmHeight,SRCCOPY);
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
if(Mode == DrawModeHeightCenter3D)
{
int width = bitmap.bmWidth / 3;
int height = lpRect->bottom - lpRect->top;
int x = lpRect->left;
int y = lpRect->top;
int right = lpRect->right;
while((x + width) < right)
{
pDC->StretchBlt(x,y,width,height,&srcDC,width,0,width,bitmap.bmHeight,SRCCOPY);
x += width;
}
pDC->StretchBlt(right - width,y,width,height,&srcDC,width,0,width,bitmap.bmHeight,SRCCOPY);
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
srcDC.SelectObject(&pOldBitmap);
srcDC.DeleteDC();
return TRUE;
}
HRGN CRTDraw::BitmapToRegion (HBITMAP hBmp, COLORREF cTransparentColor, COLORREF cTolerance)
{
HRGN hRgn = NULL;
if (hBmp)
{
// Create a memory DC inside which we will scan the bitmap content
HDC hMemDC = CreateCompatibleDC(NULL);
if (hMemDC)
{
// Get bitmap size
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
// Create a 32 bits depth bitmap and select it into the memory DC
BITMAPINFOHEADER RGB32BITSBITMAPINFO = {
sizeof(BITMAPINFOHEADER), // biSize
bm.bmWidth, // biWidth;
bm.bmHeight, // biHeight;
1, // biPlanes;
32, // biBitCount
BI_RGB, // biCompression;
0, // biSizeImage;
0, // biXPelsPerMeter;
0, // biYPelsPerMeter;
0, // biClrUsed;
0 // biClrImportant;
};
VOID * pbits32;
HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
if (hbm32)
{
HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);
// Create a DC just to copy the bitmap into the memory DC
HDC hDC = CreateCompatibleDC(hMemDC);
if (hDC)
{
// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
BITMAP bm32;
GetObject(hbm32, sizeof(bm32), &bm32);
while (bm32.bmWidthBytes % 4)
bm32.bmWidthBytes++;
// Copy the bitmap into the memory DC
HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
// For better performances, we will use the ExtCreateRegion() function to create the
// region. This function take a RGNDATA structure on entry. We will add rectangles by
// amount of ALLOC_UNIT number in this structure.
#define ALLOC_UNIT 100
DWORD maxRects = ALLOC_UNIT;
HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
pData->rdh.dwSize = sizeof(RGNDATAHEADER);
pData->rdh.iType = RDH_RECTANGLES;
pData->rdh.nCount = pData->rdh.nRgnSize = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
// Keep on hand highest and lowest values for the "transparent" pixels
BYTE lr = GetRValue(cTransparentColor);
BYTE lg = GetGValue(cTransparentColor);
BYTE lb = GetBValue(cTransparentColor);
BYTE hr = min(0xff, lr + GetRValue(cTolerance));
BYTE hg = min(0xff, lg + GetGValue(cTolerance));
BYTE hb = min(0xff, lb + GetBValue(cTolerance));
// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
for (int y = 0; y < bm.bmHeight; y++)
{
TRACE("\n");
// Scan each bitmap pixel from left to right
for (int x = 0; x < bm.bmWidth; x++)
{
// Search for a continuous range of "non transparent pixels"
int x0 = x;
LONG *p = (LONG *)p32 + x;
while (x < bm.bmWidth)
{
TRACE("1");
BYTE b = GetRValue(*p);
if (b >= lr && b <= hr)
{
b = GetGValue(*p);
if (b >= lg && b <= hg)
{
b = GetBValue(*p);
if (b >= lb && b <= hb)
{
TRACE("0");
// This pixel is "transparent"
break;
}
}
}
p++;
x++;
}
if (x > x0)
{
// Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
if (pData->rdh.nCount >= maxRects)
{
GlobalUnlock(hData);
maxRects += ALLOC_UNIT;
hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
pData = (RGNDATA *)GlobalLock(hData);
}
RECT *pr = (RECT *)&pData->Buffer;
SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
if (x0 < pData->rdh.rcBound.left)
pData->rdh.rcBound.left = x0;
if (y < pData->rdh.rcBound.top)
pData->rdh.rcBound.top = y;
if (x > pData->rdh.rcBound.right)
pData->rdh.rcBound.right = x;
if (y+1 > pData->rdh.rcBound.bottom)
pData->rdh.rcBound.bottom = y+1;
pData->rdh.nCount++;
// On Windows98, ExtCreateRegion() may fail if the number of rectangles is too
// large (ie: > 4000). Therefore, we have to create the region by multiple steps.
if (pData->rdh.nCount == 2000)
{
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
pData->rdh.nCount = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
}
}
}
// Go to next row (remember, the bitmap is inverted vertically)
p32 -= bm32.bmWidthBytes;
}
// Create or extend the region with the remaining rectangles
HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
if (hRgn)
{
CombineRgn(hRgn, hRgn, h, RGN_OR);
DeleteObject(h);
}
else
hRgn = h;
// Clean up
GlobalFree(hData);
SelectObject(hDC, holdBmp);
DeleteDC(hDC);
}
DeleteObject(SelectObject(hMemDC, holdBmp));
}
DeleteDC(hMemDC);
}
}
return hRgn;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -