📄 dibapi.cpp
字号:
for(i=0;i<16;i++)
{
lpTarget->bmiColors[i].rgbRed = pe[i].peRed;
lpTarget->bmiColors[i].rgbGreen = pe[i].peGreen;
lpTarget->bmiColors[i].rgbBlue = pe[i].peBlue;
lpTarget->bmiColors[i].rgbReserved = pe[i].peFlags;
}
}
}
break; // end 4bpp
// 1bpp - need 2 entry mono color table
case 1:
lpTarget->bmiColors[0].rgbRed = 0;
lpTarget->bmiColors[0].rgbGreen = 0;
lpTarget->bmiColors[0].rgbBlue = 0;
lpTarget->bmiColors[0].rgbReserved = 0;
lpTarget->bmiColors[1].rgbRed = 255;
lpTarget->bmiColors[1].rgbGreen = 255;
lpTarget->bmiColors[1].rgbBlue = 255;
lpTarget->bmiColors[1].rgbReserved = 0;
break; // end 1bpp
// no color table for the > 8bpp modes
case 32:
case 24:
case 16:
default:
break;
}
return TRUE;
}
/* End CopyColorTable() *****************************************************/
HBITMAP DIBToDIBSection(LPBYTE lpDIB)
{
LPBYTE lpSourceBits;
HDC hDC = NULL, hSourceDC;
HBITMAP hSourceBitmap, hOldSourceBitmap;
DWORD dwSourceBitsSize;
LPBITMAPINFO lpSrcDIB = (LPBITMAPINFO)lpDIB;
if (! lpSrcDIB)
return NULL;
// Gonna use DIBSections and BitBlt() to do the conversion, so make 'em
hDC = GetDC( NULL );
hSourceBitmap = CreateDIBSection( hDC, lpSrcDIB, DIB_RGB_COLORS, (VOID **)&lpSourceBits, NULL, 0 );
hSourceDC = CreateCompatibleDC( hDC );
// Flip the bits on the source DIBSection to match the source DIB
dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight * BytesPerLine((LPBYTE)&(lpSrcDIB->bmiHeader));
memcpy( lpSourceBits, FindDIBBits((LPBYTE)lpSrcDIB), dwSourceBitsSize );
// Select DIBSections into DCs
hOldSourceBitmap = (HBITMAP)SelectObject( hSourceDC, hSourceBitmap );
// Set the color tables for the DIBSections
if( lpSrcDIB->bmiHeader.biBitCount <= 8 )
SetDIBColorTable( hSourceDC, 0, 1 << lpSrcDIB->bmiHeader.biBitCount, lpSrcDIB->bmiColors );
// Clean up and delete the DCs
SelectObject( hSourceDC, hOldSourceBitmap );
DeleteDC( hSourceDC );
ReleaseDC( NULL, hDC );
// Flush the GDI batch, so we can play with the bits
GdiFlush();
return hSourceBitmap;
}
// Create DIBSECTION from DIB
HBITMAP DIBToDIBSection(HDIB hDIB)
{
HBITMAP hSourceBitmap;
// Get DIB pointer
if (! hDIB)
return NULL;
LPBYTE lpSrcDIB = (LPBYTE)GlobalLock(hDIB);
if (! lpSrcDIB)
return NULL;
hSourceBitmap = DIBToDIBSection(lpSrcDIB);
// final cleanup
GlobalUnlock(hDIB);
return hSourceBitmap;
}
HDIB DIBSectionToDIB(HBITMAP hBitmap)
{
HDC hDC = NULL, hSourceDC;
HBITMAP hOldSourceBitmap;
HANDLE hNewDIB;
LPBITMAPINFO lpbmi = NULL;
DWORD dwSize;
DIBSECTION ds;
DWORD dwColorNum;
// get DIB info
GetObject(hBitmap, sizeof(DIBSECTION), &ds);
dwColorNum = ds.dsBmih.biClrUsed;
if (dwColorNum == 0 && ds.dsBmih.biBitCount <= 8)
dwColorNum = 1 << ds.dsBmih.biBitCount;
// Allocate and fill out a BITMAPINFO struct for the new DIB
dwSize = sizeof(BITMAPINFOHEADER) + (dwColorNum * sizeof(RGBQUAD)) + ds.dsBmih.biSizeImage;
hNewDIB = GlobalAlloc(GHND, dwSize);
if (! hNewDIB)
return NULL;
lpbmi = (LPBITMAPINFO)GlobalLock(hNewDIB);
if (! lpbmi)
return NULL;
lpbmi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
lpbmi->bmiHeader.biWidth = ds.dsBm.bmWidth;
lpbmi->bmiHeader.biHeight = ds.dsBm.bmHeight;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = ds.dsBmih.biBitCount;
lpbmi->bmiHeader.biCompression = ds.dsBmih.biCompression;
lpbmi->bmiHeader.biSizeImage = ds.dsBmih.biSizeImage;
lpbmi->bmiHeader.biXPelsPerMeter = ds.dsBmih.biXPelsPerMeter;
lpbmi->bmiHeader.biYPelsPerMeter = ds.dsBmih.biYPelsPerMeter;
lpbmi->bmiHeader.biClrUsed = ds.dsBmih.biClrUsed;
lpbmi->bmiHeader.biClrImportant = ds.dsBmih.biClrImportant;
// get DC for operation
hDC = GetDC( NULL );
// get DIB bits
if (! GetDIBits(hDC,
hBitmap,
0L,
(DWORD)ds.dsBm.bmHeight,
(LPBYTE)lpbmi + (WORD)lpbmi->bmiHeader.biSize + (dwColorNum*sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbmi,
(DWORD)DIB_RGB_COLORS))
{
GlobalUnlock(hNewDIB);
ReleaseDC(NULL,hDC);
return NULL;
}
// memory DC
hSourceDC = CreateCompatibleDC( hDC );
// Select DIBSections into DCs
hOldSourceBitmap = (HBITMAP)SelectObject( hSourceDC, hBitmap );
// Fill in the color table from DIBSection
if( lpbmi->bmiHeader.biBitCount <= 8 )
GetDIBColorTable( hSourceDC, 0, 1 << lpbmi->bmiHeader.biBitCount, lpbmi->bmiColors );
// Clean up and delete the DCs
SelectObject( hSourceDC, hOldSourceBitmap );
DeleteDC( hSourceDC );
ReleaseDC( NULL, hDC );
GlobalUnlock(hNewDIB);
return hNewDIB;
}
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution
// check for an empty rectangle
if (IsRectEmpty(lpRect))
return NULL;
// create a DC for the screen and create
// a memory DC compatible to screen DC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
// get points of rectangle to grab
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
// get screen resolution
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//make sure bitmap rectangle is visible
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// create a bitmap compatible with the screen DC
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// select new bitmap into memory DC
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
// bitblt screen DC to memory DC
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
// select old bitmap back into memory DC and get handle to
// bitmap of the screen
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// return handle to the bitmap
return hBitmap;
}
HBITMAP CopyWindowToBitmap(HWND hWnd, WORD fPrintArea)
{
HBITMAP hBitmap = NULL; // handle to device-dependent bitmap
// check for a valid window handle
if (!hWnd)
return NULL;
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
RECT rectWnd;
// get the window rectangle
GetWindowRect(hWnd, &rectWnd);
// get the bitmap of that window by calling
// CopyScreenToBitmap and passing it the window rect
hBitmap = CopyScreenToBitmap(&rectWnd);
break;
}
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2;
// get client dimensions
GetClientRect(hWnd, &rectClient);
// convert client coords to screen coords
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.bottom;
ClientToScreen(hWnd, &pt1);
ClientToScreen(hWnd, &pt2);
rectClient.left = pt1.x;
rectClient.top = pt1.y;
rectClient.right = pt2.x;
rectClient.bottom = pt2.y;
// get the bitmap of the client area by calling
// CopyScreenToBitmap and passing it the client rect
hBitmap = CopyScreenToBitmap(&rectClient);
break;
}
default: // invalid print area
return NULL;
}
// return handle to the bitmap
return hBitmap;
}
HBITMAP CopyClientRectToBitmap(HWND hWnd, LPRECT lpRect)
{
HBITMAP hBitmap = NULL; // handle to DIB
// check for a valid window handle
if (!hWnd)
return NULL;
POINT pt1, pt2;
// convert client coords to screen coords
pt1.x = lpRect->left;
pt1.y = lpRect->top;
pt2.x = lpRect->right;
pt2.y = lpRect->bottom;
ClientToScreen(hWnd, &pt1);
ClientToScreen(hWnd, &pt2);
lpRect->left = pt1.x;
lpRect->top = pt1.y;
lpRect->right = pt2.x;
lpRect->bottom = pt2.y;
// get the DIB of the client area by calling
// CopyScreenToDIB and passing it the client rect
hBitmap = CopyScreenToBitmap(lpRect);
// restore lpRect
pt1.x = lpRect->left;
pt1.y = lpRect->top;
pt2.x = lpRect->right;
pt2.y = lpRect->bottom;
ScreenToClient(hWnd, &pt1);
ScreenToClient(hWnd, &pt2);
lpRect->left = pt1.x;
lpRect->top = pt1.y;
lpRect->right = pt2.x;
lpRect->bottom = pt2.y;
// return the handle to the DIB
return hBitmap;
}
HDIB CopyScreenToDIB(LPRECT lpRect)
{
HBITMAP hBitmap; // handle to device-dependent bitmap
HPALETTE hPalette; // handle to palette
HDIB hDIB = NULL; // handle to DIB
// get the device-dependent bitmap in lpRect by calling
// CopyScreenToBitmap and passing it the rectangle to grab
hBitmap = CopyScreenToBitmap(lpRect);
// check for a valid bitmap handle
if (!hBitmap)
return NULL;
// get the current palette
hPalette = GetSystemPalette();
// convert the bitmap to a DIB
hDIB = BitmapToDIB(hBitmap, hPalette);
// clean up
DeleteObject(hPalette);
DeleteObject(hBitmap);
// return handle to the packed-DIB
return hDIB;
}
HDIB CopyWindowToDIB(HWND hWnd, WORD fPrintArea)
{
HDIB hDIB = NULL; // handle to DIB
// check for a valid window handle
if (!hWnd)
return NULL;
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
RECT rectWnd;
// get the window rectangle
GetWindowRect(hWnd, &rectWnd);
// get the DIB of the window by calling
// CopyScreenToDIB and passing it the window rect
hDIB = CopyScreenToDIB(&rectWnd);
break;
}
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2;
// get the client area dimensions
GetClientRect(hWnd, &rectClient);
// convert client coords to screen coords
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -