C#的简单例子转成DELPHI的问题请教
请问如下的东东转成DELPHI的,为何截图是黑的:
private void PrintBu_Click(object sender, EventArgs e)
{
if (this.Title_tb.Text != " ")
{
IntPtr Wind_hwnd = Code.Win32Api.FindWindow( "SciCalc ", "计算器 ");
IntPtr hscrdc = Code.Win32Api.GetWindowDC(Wind_hwnd);
Code.Win32Api.RECT Rect = new PrintWindow.Code.Win32Api.RECT();
Code.Win32Api.GetWindowRect(Wind_hwnd,ref Rect);
IntPtr hbitmap = Code.Win32Api.CreateCompatibleBitmap(hscrdc, Rect.Right-Rect.Left, Rect.Bottom-Rect.Top);
IntPtr hmemdc = Code.Win32Api.CreateCompatibleDC(hscrdc);
Code.Win32Api.SelectObject(hmemdc, hbitmap);
Code.Win32Api.PrintWindow(Wind_hwnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
Code.Win32Api.DeleteDC(hscrdc);//删除用过的对象
Code.Win32Api.DeleteDC(hmemdc);//删除用过的对象
this.PhotoShow.Image = bmp;
this.PhotoShow.Refresh();
}
}
-------------------------------------------------
我转成DELPHI:
function PrintWindow(hwnd:HWND;hdcBlt:HWND;nFlags:word ):boolean;far;external 'user32.dll ';
....
procedure TForm1.Button3Click(Sender: TObject);
var
Wind_hwnd,hscrdc, hbitmap, hmemdc: Integer;
Rect: TRect;
Map: TBitMap;
begin
Map := TBitMap.Create;
Map.Width := 500;
Map.Height := 500;
Wind_hwnd := FindWindow( 'SciCalc ', '计算器 ');
hscrdc := GetWindowDC(Wind_hwnd);
GetWindowRect(Wind_hwnd, Rect);
hbitmap := CreateCompatibleBitmap(hscrdc, Rect.Right-Rect.Left, Rect.Bottom-Rect.Top);
hmemdc := CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(Wind_hwnd, hmemdc, 0);
Image1.Picture.Bitmap := TBitMap.Create;
Image1.Picture.Bitmap.Handle := hbitmap;
Image1.Repaint;
Image1.Picture.SaveToFile( 'c:\124.bmp ');
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
DeleteObject(hbitmap);
end;
[解决办法]
没必要转C#的!
截图代码到我网站上找,或Baidu的
[解决办法]
var
R: TRect;
W, H: Integer;
DC, memDC: HDC;
bm, oldBM: HBITMAP;
Wnd: HWND;
bmp:TBitmap;
begin
Wnd := FindWindow( 'SciCalc ', '计算器 ');
GetWindowRect(Wnd, R);
W := R.Right - R.Left;
H := R.Bottom - R.Top;
DC := GetWindowDC(Wnd);
memDC := CreateCompatibleDC(DC);
bm := CreateCompatibleBitmap(DC, W, H);
oldBM := SelectObject(memDC, bm);
BitBlt(memDC, 0,0, w, h, DC, 0,0, SRCCOPY);
SelectObject(memDC, oldBM);
DeleteDC(memDC);
DeleteObject(oldBM);
ReleaseDC(Wnd, DC);
bmp:=TbitMap.Create;
bmp.Handle:=bm;
bmp.SaveToFile( 'd:\123.bmp ');
//self.Image1.Picture.Bitmap.Assign( bmp );
bmp.Free;
end;