HDC hdcScreen;
int xS,yS; HBITMAP hBitmap;
HDC hMemDC;
初始化:
hdcScreen=::GetDC(HWND_DESKTOP);
xS=GetDeviceCaps(hdcScreen,HORZRES);
yS=GetDeviceCaps(hdcScreen,VERTRES);
hMemDC=CreateCompatibleDC(hdcScreen);
hBitmap=CreateCompatibleBitmap(hdcScreen,xS,yS);
捕获屏幕函数体:
PatBlt(hMemDC,0,0,xS,yS,WHITENESS);
SelectObject(hMemDC,hBitmap);
StretchBlt(hMemDC,0,0,xS,yS,hdcScreen,xS,yS,xS,yS,SRCCOPY);
GetObject(hBitmap,sizeof(BITMAP),&Bitmap);
显示到View:
在OnDraw函数里,或者自定义函数里从hMemDC里copy到View DC里就行了
退出时释放:
DeleteObject(hBitmap);
DeleteDC(hMemDC);
::ReleaseDC(HWND_DESKTOP,hdcScreen);
int xS,yS; HBITMAP hBitmap;
HDC hMemDC;
初始化:
hdcScreen=::GetDC(HWND_DESKTOP);
xS=GetDeviceCaps(hdcScreen,HORZRES);
yS=GetDeviceCaps(hdcScreen,VERTRES);
hMemDC=CreateCompatibleDC(hdcScreen);
hBitmap=CreateCompatibleBitmap(hdcScreen,xS,yS);
捕获屏幕函数体:
PatBlt(hMemDC,0,0,xS,yS,WHITENESS);
SelectObject(hMemDC,hBitmap);
StretchBlt(hMemDC,0,0,xS,yS,hdcScreen,xS,yS,xS,yS,SRCCOPY);
GetObject(hBitmap,sizeof(BITMAP),&Bitmap);
显示到View:
在OnDraw函数里,或者自定义函数里从hMemDC里copy到View DC里就行了
退出时释放:
DeleteObject(hBitmap);
DeleteDC(hMemDC);
::ReleaseDC(HWND_DESKTOP,hdcScreen);
StretchBlt(hMemDC,0,0,xS,yS,hdcScreen,0,0,xS,yS,SRCCOPY);
GetObject(hBitmap,sizeof(BITMAP),&Bitmap);这句话出现错误
Bitmap是用来获得位图参数的
你可以不要这句
如果你需要的话,定义一下:BITMAP Bitmap
::BitBlt(pDC->m_hDC,0,0,xS,yS,hMemDC,0,0,SRCCOPY);
程序顺利执行并显示当前屏幕,谢谢高手!!!!!
这句话有什么用处啊,我发现把这句话去掉程序没受什么影响还有,我要把图片存储起来该如何做那
高手,请接着指教
PatBlt(hMemDC,0,0,xS,yS,WHITENESS);
是刷新背景,对你这个程序应该没有,因为我原来想截取屏幕中的一块来着,要去掉干扰象素。保存函数:
LPTSTR szFile;
{
HPALETTE hPalette;
HDIB hDIB = NULL;
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
hPalette = GetSystemPalette();
hDIB = BitmapToDIB(hBitmap, hPalette); if (!hDIB)
return FALSE; CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
return TRUE;
}其中:
HDIB BitmapToDIB(HBITMAP hBitmap, HPALETTE hPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB, h;
HDC hDC;
WORD biBits;
if (!hBitmap) return NULL;
if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm)) return NULL;
if (hPal == NULL) hPal = GetStockObject(DEFAULT_PALETTE);
biBits = bm.bmPlanes * bm.bmBitsPixel;
if (biBits <= 1) biBits = 1;
else if (biBits <= 4) biBits = 4;
else if (biBits <= 8) biBits = 8;
else
biBits = 24;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = biBits;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwLen = bi.biSize + PaletteSize((LPSTR)&bi);
hDC = GetDC(NULL);
hPal = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
hDIB = GlobalAlloc(GHND, dwLen);
if (!hDIB)
{
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
*lpbi = bi;
GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
bi = *lpbi;
GlobalUnlock(hDIB);
if (bi.biSizeImage == 0)
bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;
dwLen = bi.biSize + PaletteSize((LPSTR)&bi) + bi.biSizeImage;
if (h = GlobalReAlloc(hDIB, dwLen, 0))
hDIB = h;
else
{
GlobalFree(hDIB);
hDIB = NULL;
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi + (WORD)lpbi->biSize + PaletteSize((LPSTR)lpbi), (LPBITMAPINFO)lpbi, DIB_RGB_COLORS) == 0)
{
GlobalUnlock(hDIB);
hDIB = NULL;
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
bi = *lpbi;
GlobalUnlock(hDIB);
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return hDIB;
}
return之前要file.Close();我从来没存过,呵呵,祝好运
我刚才试了一下,有问题
你去
http://www.codeguru.com/bitmap/index.shtml
看一下
里面有解决方案