截图代码优化(100)

M

m8858

Unregistered / Unconfirmed
GUEST, unregistred user!
{完整代码 tu.dpr}-------------------------------------------------------------------program tu;uses Windows, Graphics;function PrintWindow(SourceWindow: hwnd; Destination: hdc; nFlags: cardinal): bool; stdcall; external 'user32.dll' name 'PrintWindow';procedure jietu(); stdcall;var bmp : TBitmap; wnd : cardinal; rec : TRect;begin wnd := FindWindow(nil, '计算器'); GetWindowRect(wnd, rec); bmp := TBitmap.Create; try bmp.Width := rec.Right - rec.Left; bmp.Height := rec.Bottom - rec.Top; bmp.PixelFormat := pf24bit; PrintWindow(wnd, bmp.Canvas.Handle, 0); bmp.SaveToFile('tu.jpg'); finally bmp.Free; end;end;begin jietu;end.-------------------------------------------------------------------现在想去掉Graphics单元 应该怎样做? 我试着分离出Graphics里的函数 结果水平不行 做不到,高手来试试???
 
这位老大能明示我一下吗
 
CreateBitmapSelectObjectCreateFile
 
有能多打出几个字的回答吗
 
再顶一次
 
type TPrintToBitmap=class private hDC:HDC; hBmp:HBITMAP; pszFile:String; pbi:pBITMAPINFO; function CreateBitmapInfoStruct:pBITMAPINFO; 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:pBITMAPINFO;var bmp:BITMAP; cClrBits:WORD; pbmi:pBITMAPINFO;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:DWORD;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;
 
顶部