0

我为 Delphi 2007 应用程序创建了一个资源文件。资源文件包含 10 个位图条目。我想知道是否有一种方法可以通过递归地遍历资源文件将所有位图加载到 Imagelist 中,或者我是否必须一次将它们拉出来。

提前致谢。

4

2 回答 2

5

要将RT_BITMAP当前模块中的所有资源类型图像添加到图像列表中,我将使用以下命令:

uses
  CommCtrl;

function EnumResNameProc(hModule: HMODULE; lpszType: LPCTSTR; lpszName: LPTSTR;
  lParam: LONG_PTR): BOOL; stdcall;
var
  BitmapHandle: HBITMAP;
begin
  Result := True;
  BitmapHandle := LoadBitmap(HInstance, lpszName);
  if (BitmapHandle <> 0) then
  begin
    ImageList_Add(HIMAGELIST(lParam), BitmapHandle, 0);
    DeleteObject(BitmapHandle);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumResourceNames(HInstance, RT_BITMAP, @EnumResNameProc,
    LONG_PTR(ImageList1.Handle));
end;
于 2013-02-01T23:45:07.443 回答
3

我猜想通过“递归遍历资源文件”,您想问是否可以在不知道资源名称的情况下加载资源。为此,有一类 API 函数允许您枚举给定模块中的资源。有关更多信息,请参阅“资源概述,枚举资源”主题。

但是,由于您自己将位图嵌入到 exe 中,因此更容易为它们命名以便于迭代,即在RC文件中:

img1 BITMAP foo.bmp
img2 BITMAP bar.bmp

这里名称“模式”是img+数字。现在很容易循环加载图像:

var x: Integer;
    ResName: string;
begin
  x := 1;
  ResName := 'img1';
  while(FindResource(hInstance, PChar(ResName), RT_BITMAP) <> 0)do begin
     // load the resource and do something with it
     ...
     // name for the next resource
     Inc(x);
     ResName := 'img' + IntToStr(x);
  end;
于 2013-02-01T23:27:12.520 回答