function PrintWindow(hWnd: HWND; hDCBlt: HWND; nFlags: Word): Bool; far; external 'user32.dll';procedure TMainform.Button2Click(Sender: TObject);
var
  lHwnd: HWND;
  lhDC, lhBmp, lhMemDC: Integer;
  lRect: TRect;
  lBmp: TBitmap;
begin
  if lvCurWin.Selected = nil then Exit;
  lHwnd := StrToInt(lvCurWin.Selected.SubItems[0]);
  { 用了一个 ListView 保存当前窗口的句柄 }
  lhDC := GetWindowDC(lHwnd);
  if lhDC <> 0 then
  begin
    lhMemDC := CreateCompatibleDC(lhDC);
    if lhMemDC <> 0 then
    begin
      GetWindowRect(lHwnd, lRect);
      lhBmp := CreateCompatibleBitmap(lhDC, lRect.Right-lRect.Left, lRect.Bottom-lRect.Top);
      if lhBmp <> 0 then
      begin
        SelectObject(lhMemDC, lhBmp);
        if not PrintWindow(lHwnd, lhMemDC, 0) then
          ShowMessage('不成功!'); { 老是提示不成功 }
        lBmp := TBitmap.Create;
        lBmp.Handle := lhBmp;
        lBmp.SaveToFile('c:\abc.bmp');
        ImgTum.Picture.Bitmap.Assign(lBmp);
        lBmp.Free;
        DeleteObject(lhBmp);
      end;
      DeleteObject(lhMemDC);
    end;
    ReleaseDC(lHwnd, lhDC);
  end;
end;上面代码,为什么执行 PrintWindow 时老是提示不成功呢!保存的 Bmp 文件是一团黑。

解决方案 »

  1.   

    用 Google、Baidu 查了,也查了以前的帖子,没有答案。
    但是好像用其它语言没问题,怎么用 Delphi 就不行呢!
    另外我的操作系统是 XP
      

  2.   

    function PrintWindow(hWnd: HWND; hDCBlt: HWND; nFlags: Word): Bool; far; external 'user32.dll'; 
    是否调错了
      

  3.   

    function PrintWindow(hWnd: HWND; hDCBlt: HWND; nFlags: Word): Bool; stdcall; external 'user32.dll';
    改成这样试试
      

  4.   

    function PrintWindow(Wnd: HWND; hDCBlt: HDC; nFlags: DWord): Bool; stdcall external 'user32.dll';
    改成这样就可以了,以前用过。
    另外注意该函数95,98,win me 不支持
      

  5.   

    果然是这样!太感谢了!
    不过为什么不能显示在 Image 中呢?
    就是这句 ImgTum.Picture.Bitmap.Assign(lBmp); 为什么看不到效果呢?
      

  6.   

    还有个问题,因为 PrintWindow 只有 Win2003/XP 才支持,如何让我的程序在 Win2000 下正常运行?
      

  7.   

    把 XP 下的 user32.dll 同时发布到2000系统的程序目录下,他还是会访问系统目录下的 user32.dll。这时把 user32.dll 改个名,程序引用改名后的 Dll,还是不行,提示无法定位另一个函数在 ntdll.dll 中,应该是在 PrintWindow 中引用了另一个 dll 中的函数。
    怎么办呢?