type TPrintToBitmap=class private hDC:HDC; hBmp:HBITMAP; pszFile:String; pbi
BITMAPINFO; function CreateBitmapInfoStruct
BITMAPINFO; procedure CreateBMPFile; public procedure SaveToFile(fileName:String);overload; procedure SaveToFile(hwnd : THandle; fileName:String);overload; class procedure PrintToFile(hwnd : THandle; FileName:String); end;function PrintWindow(SourceWindow: hwnd; Destination: hdc; nFlags: cardinal): bool; stdcall; external 'user32.dll' name 'PrintWindow';function TPrintToBitmap.CreateBitmapInfoStruct
BITMAPINFO;var bmp:BITMAP; cClrBits:WORD; pbmi
BITMAPINFO;begin GetObject(hBmp,sizeof(BITMAP),@bmp); cClrBits:=bmp.bmPlanes*bmp.bmBitsPixel; //分配位图信息结构体 if cClrBits <> 24 then begin pbmi:=PBITMAPINFO(allocMem(sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(cClrBits shl 1))); end else begin pbmi:=PBITMAPINFO(allocMem(sizeof(BITMAPINFOHEADER))); end; //初始化位图结构体 pbmi.bmiHeader.biSize:=sizeof(BITMAPINFOHEADER); pbmi.bmiHeader.biWidth:=bmp.bmWidth; pbmi.bmiHeader.biHeight:=bmp.bmHeight; pbmi.bmiHeader.biPlanes:=bmp.bmPlanes; pbmi.bmiHeader.biBitCount:=bmp.bmBitsPixel; if(cClrBits < 24) then pbmi.bmiHeader.biClrUsed := (cClrBits shl 1); pbmi.bmiHeader.biCompression := BI_RGB; pbmi.bmiHeader.biSizeImage := round((pbmi.bmiHeader.biWidth+7)/8*pbmi.bmiHeader.biHeight*cClrBits); pbmi.bmiHeader.biClrImportant:=0; result := pbmi;end;procedure TPrintToBitmap.CreateBMPFile;var pbih: PBITMAPINFOHEADER; lpBits : PByte; hf : THANDLE; hdr : BITMAPFILEHEADER; wt
WORD;begin pbih := PBITMAPINFOHEADER(pbi); lpBits := PByte(GlobalAlloc(GMEM_FIXED,pbi.bmiHeader.biSizeImage)); GetDIBits(hDC,hBmp,0,pbih.biHeight,lpBits,pbi^,DIB_RGB_COLORS); hf := CreateFile(PChar(pszFile),GENERIC_READ or GENERIC_WRITE, 0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); hdr.bfType := $4d42; hdr.bfSize := sizeof(BITMAPFILEHEADER)+ pbih.biSize+pbih.biClrUsed*sizeof(RGBQUAD)+ pbih.biSizeImage; hdr.bfReserved1 := 0; hdr.bfReserved2 := 0; hdr.bfOffBits := sizeof(BITMAPFILEHEADER)+ pbih.biSize+pbih.biClrUsed*sizeof(RGBQUAD); WriteFile(hf,hdr,sizeof(BITMAPFILEHEADER),wt,nil); WriteFile(hf,pbih^, sizeof(BITMAPINFOHEADER)+pbih.biClrUsed*sizeof(RGBQUAD), wt,nil); //写入位图数据 WriteFile(hf,lpBits^,pbih.biSizeImage,wt,nil); CloseHandle(hf); //GlobalFree(HGLOBAL(lpBits));end;procedure TPrintToBitmap.SaveToFile(fileName : String);begin SaveToFile(0, fileName);end;class procedure TPrintToBitmap.PrintToFile(hwnd: THandle; FileName: String);var PrintToBitmap:TPrintToBitmap;begin PrintToBitmap:=TPrintToBitmap.Create; try PrintToBitmap.SaveToFile(hwnd, FileName); finally PrintToBitmap.Free; end;end;procedure TPrintToBitmap.SaveToFile(hwnd: THandle; fileName : String);var Bitmap: Windows.TBitmap; Rec :TRect; bmp:TBitmap;begin pszFile := fileName; if hwnd = 0 then hwnd := GetDesktopWindow; Windows.GetWindowRect(hwnd, rec); hdc := CreateCompatibleDC(0); hBmp := CreateBitmap(rec.Right - rec.Left, rec.Bottom - rec.Top,1,32,nil); SelectObject(hdc, hBmp); PrintWindow(hwnd, hdc, 0); pbi := CreateBitmapInfoStruct(); CreateBMPFile(); DeleteDC(hdc); DeleteObject(hBmp);end;procedure TForm1.Button1Click(Sender: TObject);begin TPrintToBitmap.PrintToFile(findwindow(nil,'计算器'),'c:/a.bmp');end;