函数名称可见函数功能
//截图代码 void Screenshots() { HBITMAP hDesktopCompatibleBitmap=NULL; HDC hDesktopCompatibleDC=NULL; HDC hDesktopDC=NULL; HWND hDesktopWnd=NULL; RECT clientRect = {0,0,0,0}; NOTIFYICONDATA nid; bool bMinimized=false; int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; OPENFILENAME ofn; char szFileName[512]; strcpy(szFileName,"J://ScreenShot.bmp"); ZeroMemory(&ofn,sizeof(ofn)); ofn.lStructSize=sizeof(OPENFILENAME); ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST; ofn.lpstrFilter="Bitmap Files (*.bmp)/0*.bmp/0"; ofn.lpstrDefExt="bmp"; ofn.lpstrFile=szFileName; ofn.nMaxFile=512; // ofn.hwndOwner = hWnd; // if(!GetSaveFileName(&ofn)) // return; SetCursor(LoadCursor(NULL,IDC_WAIT)); int nWidth=GetSystemMetrics(SM_CXSCREEN); int nHeight=GetSystemMetrics(SM_CYSCREEN); hDesktopDC = CreateDC("DISPLAY", NULL, NULL, NULL); HDC hBmpFileDC=CreateCompatibleDC(hDesktopDC); HBITMAP hBmpFileBitmap=CreateCompatibleBitmap(hDesktopDC,nWidth,nHeight); HBITMAP hOldBitmap = (HBITMAP) SelectObject(hBmpFileDC,hBmpFileBitmap); BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); SelectObject(hBmpFileDC,hOldBitmap); SaveBitmap(ofn.lpstrFile,hBmpFileBitmap); DeleteDC(hBmpFileDC); DeleteObject(hBmpFileBitmap); SetCursor(LoadCursor(NULL,IDC_ARROW)); } //将内存中位图写为Bmp格式 //szFile为目标地址 void SaveBitmap(char *szFilename,HBITMAP hBitmap) { HDC hdc=NULL; FILE* fp=NULL; LPVOID pBuf=NULL; BITMAPINFO bmpInfo; BITMAPFILEHEADER bmpFileHeader; do{ hdc=GetDC(NULL); ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); if(bmpInfo.bmiHeader.biSizeImage<=0) bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8; if((pBuf=malloc(bmpInfo.bmiHeader.biSizeImage))==NULL) { // MessageBox(NULL,_T("Unable to Allocate Bitmap Memory"),_T("Error"),MB_OK|MB_ICONERROR); break; } bmpInfo.bmiHeader.biCompression=BI_RGB; GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS); if((fp=fopen(szFilename,"wb"))==NULL) { // MessageBox(NULL,_T("Unable to Create Bitmap File"),_T("Error"),MB_OK|MB_ICONERROR); break; } bmpFileHeader.bfReserved1=0; bmpFileHeader.bfReserved2=0; bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage; bmpFileHeader.bfType='MB'; bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp); fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp); fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); }while(false); if(hdc) ReleaseDC(NULL,hdc); if(pBuf) free(pBuf); if(fp) fclose(fp); }