TBitmap.Create不能在delphi控制台应用程序中工作

我需要使用控制台应用程序处理一组bmp文件,我正在使用TBitmap类,但代码不会编译,因为这个错误
E2003 Undeclared identifier: 'Create'

此示例应用程序会再现该问题

{$APPTYPE CONSOLE}

{$R *.res}

uses
 System.SysUtils,Vcl.Graphics,WinApi.Windows;

procedure CreateBitMap;
Var
  Bmp  : TBitmap;
  Flag : DWORD;
begin
  Bmp:=TBitmap.Create; //this line produce the error of compilation
  try
    //do something
  finally
   Bmp.Free;
  end;
end;

begin
  try
    CreateBitMap;

  except
    on E: Exception do
      Writeln(E.ClassName,': ',E.Message);
  end;
end.

为什么这段代码不能编译?

解决方法

问题是按照你的uses子句的顺序,WinApi.Windows和Vcl.Graphics单元有一个名为TBitmap的类型,当编译器找到一个不明确的类型时,使用使用列表中存在的最后一个单元来解析类型.在这种情况下,请使用指向 BITMAP WinAPi结构的Windows单元的TBitmap,以将您的单位的顺序解决
uses
 System.SysUtils,WinApi.Windows,Vcl.Graphics;

或者您可以使用完全限定名称声明类型

procedure CreateBitMap;
Var
  Bmp  : Vcl.Graphics.TBitmap;
  Flag : DWORD;
begin
  Bmp:=Vcl.Graphics.TBitmap.Create;
  try
    //do something
  finally
   Bmp.Free;
  end;
end;

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...