Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TBitmap.Create在delphi控制台应用程序中不起作用_Delphi_Delphi Xe2 - Fatal编程技术网

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

TBitmap.Create在delphi控制台应用程序中不起作用,delphi,delphi-xe2,Delphi,Delphi Xe2,我需要使用控制台应用程序处理一组bmp文件,我使用的是TBitmap类,但由于此错误,代码无法编译 E2003 Undeclared identifier: 'Create' 此示例应用程序复制了该问题 {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, Vcl.Graphics, WinApi.Windows; procedure CreateBitMap; Var Bmp : TBitmap; Flag : DWOR

我需要使用控制台应用程序处理一组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的类型,使用uses列表中存在的最后一个单元解析该类型。在这种情况下,使用指向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;