在Delphi XE2中使用"使用"时出错

use*_*248 -1 delphi delphi-xe2

嗨我有以下代码的问题,问题是我在使用"使用"时似乎忘记了一个组件因为它总是给我错误"tagBITMAP",代码如下:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,Vcl.Graphics,Vcl.Imaging.jpeg,Windows;



procedure capturar_pantalla(nombre: string);

// Function capturar() based in :
// http://forum.codecall.net/topic/60613-how-to-capture-screen-with-delphi-code/
// http://delphi.about.com/cs/adptips2001/a/bltip0501_4.htm
// http://stackoverflow.com/questions/21971605/show-mouse-cursor-in-screenshot-with-delphi
// Thanks to Zarko Gajic , Luthfi and Ken White

var
  aca: HDC;
  tan: TRect;
  posnow: TPoint;
  imagen1: TBitmap;
  imagen2: TJpegImage;
  curnow: THandle;

begin

  aca := GetWindowDC(GetDesktopWindow);
  imagen1 := TBitmap.Create;

  GetWindowRect(GetDesktopWindow, tan);
  imagen1.Width := tan.Right - tan.Left;
  imagen1.Height := tan.Bottom - tan.Top;
  BitBlt(imagen1.Canvas.Handle, 0, 0, imagen1.Width, imagen1.Height, aca, 0,
    0, SRCCOPY);

  GetCursorPos(posnow);

  curnow := GetCursor;
  DrawIconEx(imagen1.Canvas.Handle, posnow.X, posnow.Y, curnow, 32, 32, 0, 0,
    DI_NORMAL);

  imagen2 := TJpegImage.Create;
  imagen2.Assign(imagen1);
  imagen2.CompressionQuality := 60;
  imagen2.SaveToFile(nombre);

  imagen1.Free;
  imagen2.Free;

end;

//



begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?

Dav*_*nan 7

这里的问题是范围之一.两个Windows单元和所述Graphics单元限定一个命名的类型TBitmap.编译器可以看到两种类型,并且作用域规则是最后定义的是编译器使用的规则.

由于该Windows单元是在之后引入的Graphics,因此您的代码TBitmap将在Windows单元中找到定义的单元.但是你想要在Graphics单元中定义的那个.

通过移动WindowsGraphicsuses子句之前出现来解决问题.

现在,您可以单独保留uses子句并完全指定类型:Vcl.Graphics.TBitmap.我认为您同意重新订购使用条款是更可取的.

顺便说一句,我对这段代码并不十分重视.它完全忽略了检查Win32 API返回值是否有错误.我建议一旦你可以编译它,你添加错误检查.功能Win32Check来自SysUtils你的朋友.还需要一些尝试/最终保护位图的生命周期.