,MFC如何獲取客戶區並形成一個 CBitmap對象保存在內存中 ,沒有C幣了,各位大神幫幫忙
// vc截圖.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <afxwin.h>
//#include <Windows.h>
HBITMAP CopyScreenToBitmap(LPRECT lpRect) //lpRect 代表選定區域
{
HDC hScrDC, hMemDC;
// 屏幕和內存設備描述表
HBITMAP hBitmap,hOldBitmap;
// 位圖句柄
int nX, nY, nX2, nY2;
// 選定區域坐標
int nWidth, nHeight;
// 位圖寬度和高度
int xScrn, yScrn;
// 屏幕分辨率
// 確保選定區域不為空矩形
if (IsRectEmpty(lpRect))
return NULL;
//為屏幕創建設備描述表
hScrDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
//為屏幕設備描述表創建兼容的內存設備描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 獲得選定區域坐標
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
// 獲得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//確保選定區域是可見的
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;
// 創建一個與屏幕設備描述表兼容的位圖
hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
// 把新位圖選到內存設備描述表中
hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
// 把屏幕設備描述表拷貝到內存設備描述表中
BitBlt(hMemDC,0,0, nWidth,nHeight,hScrDC, nX, nY, SRCCOPY);
//得到屏幕位圖的句柄
hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// 返回位圖句柄
return hBitmap;
}
int SaveBitmapToFile(HBITMAP hBitmap, LPSTR lpFileName) //hBitmap 為剛才的屏幕位圖句柄
{ //lpFileName 為位圖文件名
HDC hDC;
//設備描述表
int iBits;
//當前顯示分辨率下每個像素所占字節數
WORD wBitCount;
//位圖中每個像素所占字節數
//定義調色板大小, 位圖中像素字節大小 , 位圖文件大小 , 寫入文件字節數
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap;
//位圖屬性結構
BITMAPFILEHEADER bmfHdr;
//位圖文件頭結構
BITMAPINFOHEADER bi;
//位圖信息頭結構
LPBITMAPINFOHEADER lpbi;
//指向位圖信息頭結構
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL;
//定義文件,分配內存句柄,調色板句柄
//計算位圖文件每個像素所占字節數
hDC = CreateDCA("DISPLAY",NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) *
GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 32;
//計算調色板大小
if (wBitCount <= 8)
dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);
//設置位圖信息頭結構
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
//為位圖內容分配內存
/*xxxxxxxx計算位圖大小分解一下(解釋一下上面的語句)xxxxxxxxxxxxxxxxxxxx
//每個掃描行所占的字節數應該為4的整數倍,具體算法為:
int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
if((Bitmap.bmWidth*wBitCount) % 32)
biWidth++; //不是整數倍的加1
biWidth *= 4;//到這裡,計算得到的為每個掃描行的字節數。
dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
// 處理調色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
RealizePalette(hDC);
}
// 獲取該調色板下新的像素值
GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
//恢復調色板
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//創建位圖文件
fh=CreateFileA(lpFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh==INVALID_HANDLE_VALUE)
return FALSE;
// 設置位圖文件頭
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
// 寫入位圖文件頭
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 寫入位圖文件其余內容
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
CRect rect;
GetWindowRect(FindWindow(NULL,L"計算器"),&rect);
HBITMAP hMap = CopyScreenToBitmap(rect);
SaveBitmapToFile(hMap,"C:\\局部截圖.bmp");
//CRect rect;
//rect.left = 0;
//rect.top = 0;
//rect.right = GetSystemMetrics(SM_CXSCREEN);
//rect.bottom = GetSystemMetrics(SM_CYSCREEN);
//HBITMAP hMap = CopyScreenToBitmap(rect);
//SaveBitmapToFile(hMap,"C:\\全屏截圖.bmp");
return 0;
}