Delphi资源文件学习笔记

鼠标指针

Screen.Cursors[1] := LoadCursor(HInstance, ‘cur1′);
Screen.Cursors[2] := LoadCursor(HInstance, ‘cur2′);
Panel1.Cursor := TCursor(1); //Panel1.Cursor := 1; 也可, 因为 TCursor 就是个数字类型.
Panel2.Cursor := TCursor(2);

Image1.Picture.Bitmap.Handle := LoadBitmap(HInstance, ‘bmp1′);
image1.Picture.Bitmap.LoadFromResourceName(HInstance, ‘bmp2′);

BMP图片 用图片作程序背景:

bit := TBitmap.Create;
bit.LoadFromResourceID(HInstance, 1);
Self.Brush.Bitmap := bit;
Repaint;

其中rc文件中用编号 1 BITMAP img\bmpFile1.bmp 菜单资源

//rc 文件:
MyMenu1 MENU
begin
Popup "File"
begin
MenuItem "&Open"
MenuItem "&Save"
MenuItem Separator
MenuItem "E&xit"
end
end

procedure TForm1.FormCreate(Sender: TObject);
var
hm: HMENU;
begin
hm := LoadMenu(HInstance, ‘MyMenu1′);
SetMenu(Handle, hm);
end;

字符串资源

//rc 文件:
StringTable
begin
0 "AAAA"
1 "BBBB"
2 "CCCC"
3 "DDDD"
end

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
with Memo1.Lines do
begin
    Add(LoadStr(0));
    Add(LoadStr(1));
    Add(LoadStr(2));
    Add(LoadStr(3));   
end;
end;

//假如有这样一个字符串资源: 
StringTable
begin
0 "String_One"
1 "String_Two"
end

procedure TForm1.Button1Click(Sender: TObject);
var
sp: array[0..254] of Char;
begin
LoadString(HInstance, 0, sp, SizeOf(sp));
ShowMessage(sp); {String_One}

LoadString(HInstance, 1, sp, SizeOf(sp));
ShowMessage(sp); {String_Two}
end;

提取dll中的BMP文件

procedure TForm1.FormPaint(Sender: TObject);
var
h: THandle;
bit: TBitmap;
begin
h := LoadLibrary(‘c:\temp\Project1.dll’); {载入 DLL}
bit := TBitmap.Create;
bit.LoadFromResourceName(h, ‘img1′);      {提取资源}
Canvas.Draw(10, 10, bit);                 {在窗体上绘制图片}
FreeLibrary(h);                           {载卸 DLL}
bit.Free;
end;

WinAPI: ExtractIcon – 获取 EXE、DLL 或 ICO 文件中的图标

//声明:
ExtractIcon(
hInst: HINST;           {调用函数的程序实例}
lpszExeFileName: PChar; {文件路径; 文件可以是 *.exe、*.dll、*.ico}
nIconIndex: UINT        {图标索引}
): HICON; {返回图标句柄; 索引为 0 时返回第一个图标句柄; 索引为 #FFFFFFFF 时返回图标总数}