Difference between revisions of "Lazarus Resources/ko"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; deleted category included in page template)
 
(11 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{Lazarus Resources}}
 
{{Lazarus Resources}}
  
==Introduction==
+
==소개==
 
Resource files contain data which should be compiled into the executable file. That data could consist of images, string tables, version info, ... even a Windows XP manifest and forms. This includes data that the programmer can retrieve in his code (accessing them as files). Using resources can be handy if you want to distribute self-contained executables.
 
Resource files contain data which should be compiled into the executable file. That data could consist of images, string tables, version info, ... even a Windows XP manifest and forms. This includes data that the programmer can retrieve in his code (accessing them as files). Using resources can be handy if you want to distribute self-contained executables.
  
Line 8: Line 8:
 
Normal resources are now recommended for current FPC '''(including all recent Lazarus versions)'''. Please see [[#FPC resources]] below.
 
Normal resources are now recommended for current FPC '''(including all recent Lazarus versions)'''. Please see [[#FPC resources]] below.
  
==Lazarus resources==
+
==라자루스 리소스==
{{Warning|As mentioned above, using "normal"/Windows resources. The current development (trunk) version of Lazarus has also switched to this format; see [[Lazarus_1.4.0_release_notes]] }}
+
{{Warning|라자루스 1.4버전 부터 윈도우 리소스 사용방식이 변경 되었습니다. :참조: [[Lazarus_1.4.0_release_notes]] }}
 +
 
 
In order to use your files as Lazarus resources, you had to recreate them with lazres. Lazres can be found in the "Tools" directory (C:\Lazarus\Tools\ - you first have to compile the lazres.lpi project) of your Lazarus installation folder.
 
In order to use your files as Lazarus resources, you had to recreate them with lazres. Lazres can be found in the "Tools" directory (C:\Lazarus\Tools\ - you first have to compile the lazres.lpi project) of your Lazarus installation folder.
  
 
Then you can compile Lazarus resource files (*.lrs) via the command line. The syntax for lazres is:
 
Then you can compile Lazarus resource files (*.lrs) via the command line. The syntax for lazres is:
:<pre><nowiki>lazres <filename of resource file> <files to include (file1 file2 file3 ...)></nowiki></pre>
+
:<pre><nowiki>lazres <리소스 파일이름> <포함시킬 리소스파일명(파일1 파일2 파일3 ...)></nowiki></pre>
Example:
+
예문:
 
<syntaxhighlight lang="dos">lazres mylazarusresource.lrs image.jpg</syntaxhighlight>
 
<syntaxhighlight lang="dos">lazres mylazarusresource.lrs image.jpg</syntaxhighlight>
  
Line 21: Line 22:
 
You can access the data in the resources directly or with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires a string parameter that indicates which object should be loaded from the resource file.
 
You can access the data in the resources directly or with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires a string parameter that indicates which object should be loaded from the resource file.
  
Example:
+
예문:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
uses ...LResources...;
 
uses ...LResources...;
  
Line 42: Line 43:
 
Every class that is derived from TGraphic contains the LoadFromLazarusResource procedure.
 
Every class that is derived from TGraphic contains the LoadFromLazarusResource procedure.
  
=== Lazarus Resource Form File ===
+
=== 라자루스 리소스 폼 파일 ===
 
Lazarus generates .LRS files from .LFM form files.  
 
Lazarus generates .LRS files from .LFM form files.  
  
Line 52: Line 53:
 
* (easier): make a trivial change in the form design, revert it and save the form; this will recreate the .lrs file without needing to run lazres.
 
* (easier): make a trivial change in the form design, revert it and save the form; this will recreate the .lrs file without needing to run lazres.
  
===Getting the raw data of an lrs resource===
+
===lrs에서 원데이터 추출하는 방법===
  
 
You can retrieve the data of the resource with:
 
You can retrieve the data of the resource with:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
uses ...LResources...;
 
uses ...LResources...;
  
Line 72: Line 73:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== FPC resources ==
+
== FPC 리소스 ==
Starting from FPC 2.4 you can use standard .rc (resource script) and .res (compiled resource) files in your project to include resources. To turn a .rc script used in the sources into a binary resource (.res file), FPC runs the appropriate external resource compiler (windres or GoRC). Therefore that resource compiler needs to be installed and present in the PATH environment variable.
+
FPC 2.4에서 기본적으로 사용할 수 있습니다.기본적으로 .rc (resource script) 또는.res (compiled resource) 파일을 당신의 프로젝트에 포함 시킬수 있습니다.
For more details, see: [http://www.freepascal.org/docs-html/prog/progch13.html FPC Programmer's guide, chapter 13 "Using Windows resources"]
+
To turn a .rc script used in the sources into a binary resource (.res file), FPC runs the appropriate external resource compiler (windres or GoRC). Therefore that resource compiler needs to be installed and present in the PATH environment variable.
 +
더자세히는 다음을 참고: [http://www.freepascal.org/docs-html/prog/progch13.html FPC 프로그래머 가이드 챕터13 "윈도우에서 리소스 사용하기"]
  
 
To simplify the compiling process, it is possible to use only the compiled resources in the .res files. You can precompile the resources by any available resource compiler - windres (available both on unixes and windows), GoRC (windows only), Microsoft resource compiler (rc.exe included in Visual Studio), Borland resource compiler (brcc32.exe included in Delphi, C++ Builder or Rad Studio products) or any other.
 
To simplify the compiling process, it is possible to use only the compiled resources in the .res files. You can precompile the resources by any available resource compiler - windres (available both on unixes and windows), GoRC (windows only), Microsoft resource compiler (rc.exe included in Visual Studio), Borland resource compiler (brcc32.exe included in Delphi, C++ Builder or Rad Studio products) or any other.
Line 100: Line 102:
 
The main class used to work with resources is TResourceStream. LCL uses it to load embedded bitmaps, icons and form streams. Look at TGraphic.LoadFromResourceID or TIcon.LoadFromResourceHandle to see how it is used inside LCL.
 
The main class used to work with resources is TResourceStream. LCL uses it to load embedded bitmaps, icons and form streams. Look at TGraphic.LoadFromResourceID or TIcon.LoadFromResourceHandle to see how it is used inside LCL.
  
=== Adding resources to your program ===
+
=== 개발프로그램에 리소스 파일 추가 ===
 
Let's review the situation when you need to store some data inside your executable and during the program run you want to extract this data from it.
 
Let's review the situation when you need to store some data inside your executable and during the program run you want to extract this data from it.
  
Line 108: Line 110:
 
Here MYDATA is the resource name, RCDATA is the type of resource (look here http://msdn.microsoft.com/en-us/library/ms648009(VS.85).aspx for explanation of possible types) and "mydata.dat" is your data file.  
 
Here MYDATA is the resource name, RCDATA is the type of resource (look here http://msdn.microsoft.com/en-us/library/ms648009(VS.85).aspx for explanation of possible types) and "mydata.dat" is your data file.  
  
Let's instruct the compiler to include the resource into your project:
+
다음처럼 리소스 파일을 당신의 프로젝트에 포함 시킬수 있습니다.:
<syntaxhighlight>program mydata;
+
<syntaxhighlight lang=pascal>program mydata;
  
 
{$R mydata.rc}
 
{$R mydata.rc}
Line 115: Line 117:
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
  
Behind the scenes, the FPC compiler actually instructs the resource compiler distributed with FPC to follow the .rc script to compile your data files into a binary .res resource file. The linker will then include this into the executable. Though this is transparent to the programmer, you can, if you want to, create your own .res file with e.g. the Borland resource compiler. Instead of using <syntaxhighlight>{$R mydata.rc}</syntaxhighlight> you'd use <syntaxhighlight>{$R mydata.res}</syntaxhighlight>.
+
Behind the scenes, the FPC compiler actually instructs the resource compiler distributed with FPC to follow the .rc script to compile your data files into a binary .res resource file. The linker will then include this into the executable. Though this is transparent to the programmer, you can, if you want to, create your own .res file with e.g. the Borland resource compiler. Instead of using <syntaxhighlight lang=pascal>{$R mydata.rc}</syntaxhighlight> you'd use <syntaxhighlight lang=pascal>{$R mydata.res}</syntaxhighlight>.
  
 
=== Checking you have windres ===
 
=== Checking you have windres ===
Line 131: Line 133:
 
On Windows, windres and the gorc resource compiler should be provided by the FPC and Lazarus installers.
 
On Windows, windres and the gorc resource compiler should be provided by the FPC and Lazarus installers.
  
=== Using resources in your program ===
+
=== 개발시 리소스들을 사용하는 방법 ===
 
Now let's extract the stored resource into a file, e.g. mydata.dat:
 
Now let's extract the stored resource into a file, e.g. mydata.dat:
<syntaxhighlight>program mydata;
+
<syntaxhighlight lang=pascal>program mydata;
 
uses
 
uses
 
   SysUtils, Windows {needed for RT_RCDATA, could be omitted};
 
   SysUtils, Windows {needed for RT_RCDATA, could be omitted};
Line 159: Line 161:
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
  
Many classes (at least in Lazarus trunk/development version) also support loading directly from FPC resources, e.g.
+
많은 클래스(최신 개발버전에서는) FPC 리소스 로딩을 지원합니다. 예를들면,
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure exampleproc;
 
procedure exampleproc;
 
var
 
var
Line 166: Line 168:
 
begin
 
begin
 
   Image := TImage.Create;
 
   Image := TImage.Create;
   Image.Picture.LoadFromResourceName(HInstance,'image'); // note that there is no need for the extension
+
   Image.Picture.LoadFromResourceName(HInstance,'image'); // 확장자가 없는것에 주의
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
[[Category:Tutorials/ko]]
 
[[Category:Lazarus/ko]]
 
[[Category:FPC/ko]]
 

Latest revision as of 01:26, 19 February 2020

Deutsch (de) English (en) español (es) français (fr) 한국어 (ko) русский (ru)

소개

Resource files contain data which should be compiled into the executable file. That data could consist of images, string tables, version info, ... even a Windows XP manifest and forms. This includes data that the programmer can retrieve in his code (accessing them as files). Using resources can be handy if you want to distribute self-contained executables.

Before FPC 2.4 it was not possible to use "normal" resource files (*.res) in Lazarus because they were Win32 specific. Please see #Lazarus resources below.

Normal resources are now recommended for current FPC (including all recent Lazarus versions). Please see #FPC resources below.

라자루스 리소스

Warning-icon.png

Warning: 라자루스 1.4버전 부터 윈도우 리소스 사용방식이 변경 되었습니다. :참조: Lazarus_1.4.0_release_notes

In order to use your files as Lazarus resources, you had to recreate them with lazres. Lazres can be found in the "Tools" directory (C:\Lazarus\Tools\ - you first have to compile the lazres.lpi project) of your Lazarus installation folder.

Then you can compile Lazarus resource files (*.lrs) via the command line. The syntax for lazres is:

lazres <리소스 파일이름> <포함시킬 리소스파일명(파일1 파일2 파일3 ...)>

예문:

lazres mylazarusresource.lrs image.jpg

To use a Lazarus resource file in your project, include the file with the $I compiler directive in the initialization section of your unit.

You can access the data in the resources directly or with the LoadFromLazarusResource method of the variables which will hold the file contents afterwards. LoadFromLazarusResource requires a string parameter that indicates which object should be loaded from the resource file.

예문:

uses ...LResources...;

...
procedure exampleproc;
var
  Image: TImage
begin
  Image := TImage.Create;
  Image.Picture.LoadFromLazarusResource('image'); // note that there is no need for the extension
end;

initialization
  {$I mylazarusresource.lrs}

This code includes the file mylazarusresource.lrs into the project. In the procedure exampleproc an icon object is created and loaded from the object "image" out of the resource. The file which was compiled into the resource was probably named image.jpg.

Every class that is derived from TGraphic contains the LoadFromLazarusResource procedure.

라자루스 리소스 폼 파일

Lazarus generates .LRS files from .LFM form files.

When a LRS form file is missing, FPC reports the following error: ERROR: unit1.pas(193,4) Fatal: Can't open include file "unit1.lrs"

To solve it you can either:

  • use lazres: c:\lazarus\tools\lazres.exe unit1.lrs unit1.lfm
  • (easier): make a trivial change in the form design, revert it and save the form; this will recreate the .lrs file without needing to run lazres.

lrs에서 원데이터 추출하는 방법

You can retrieve the data of the resource with:

uses ...LResources...;

...
procedure TForm1.FormCreate(Sender: TObject);
var
  r: TLResource;
  Data: String;
begin
  r:=LazarusResources.Find('datafile1');
  if r=nil then raise Exception.Create('resource datafile1 is missing');
  Data:=r.Value;
  ...do something with the data...
end;

FPC 리소스

FPC 2.4에서 기본적으로 사용할 수 있습니다.기본적으로 .rc (resource script) 또는.res (compiled resource) 파일을 당신의 프로젝트에 포함 시킬수 있습니다. To turn a .rc script used in the sources into a binary resource (.res file), FPC runs the appropriate external resource compiler (windres or GoRC). Therefore that resource compiler needs to be installed and present in the PATH environment variable. 더자세히는 다음을 참고: FPC 프로그래머 가이드 챕터13 "윈도우에서 리소스 사용하기"

To simplify the compiling process, it is possible to use only the compiled resources in the .res files. You can precompile the resources by any available resource compiler - windres (available both on unixes and windows), GoRC (windows only), Microsoft resource compiler (rc.exe included in Visual Studio), Borland resource compiler (brcc32.exe included in Delphi, C++ Builder or Rad Studio products) or any other.

Use

  • {$R filename.rc} to compile a resource script and include the resulting resource file or
  • {$R filename.res} directive to include a compiled resource file

into the executable.

FPC RTL provides both low-level functions as well as high-level classes to access resources.

The low-level functions are:

  • EnumResourceTypes
  • EnumResourceNames
  • EnumResourceLanguages
  • FindResource
  • FindResourceEx
  • LoadResource
  • SizeofResource
  • LockResource
  • UnlockResource
  • FreeResource

They are compatible with the Windows API functions: [1]

The main class used to work with resources is TResourceStream. LCL uses it to load embedded bitmaps, icons and form streams. Look at TGraphic.LoadFromResourceID or TIcon.LoadFromResourceHandle to see how it is used inside LCL.

개발프로그램에 리소스 파일 추가

Let's review the situation when you need to store some data inside your executable and during the program run you want to extract this data from it.

First we need to tell the compiler what files to include in the resource. We do this in a .rc (resource script) file: mydata.rc file:

MYDATA         RCDATA "mydata.dat"

Here MYDATA is the resource name, RCDATA is the type of resource (look here http://msdn.microsoft.com/en-us/library/ms648009(VS.85).aspx for explanation of possible types) and "mydata.dat" is your data file.

다음처럼 리소스 파일을 당신의 프로젝트에 포함 시킬수 있습니다.:

program mydata;

{$R mydata.rc}
begin
end.

Behind the scenes, the FPC compiler actually instructs the resource compiler distributed with FPC to follow the .rc script to compile your data files into a binary .res resource file. The linker will then include this into the executable. Though this is transparent to the programmer, you can, if you want to, create your own .res file with e.g. the Borland resource compiler. Instead of using

{$R mydata.rc}

you'd use

{$R mydata.res}

.

Checking you have windres

On Linux/OSX, you might need to make sure you have the windres resource compiler, which is provided by the mingw32 tools. E.g. on Debian you could run:

aptitude install mingw32-binutils
# this installs e.g. /usr/bin/i586-mingw32msvc-windres
# but FPC expects just windres, so:
ln -s /usr/bin/i586-mingw32msvc-windres /usr/bin/windres
# alternatively (and probably more "official") use -FCi586-mingw32msvc-windres in your calls to fpc or
# add -FCi586-mingw32msvc-windres to your fpc.cfg

On Windows, windres and the gorc resource compiler should be provided by the FPC and Lazarus installers.

개발시 리소스들을 사용하는 방법

Now let's extract the stored resource into a file, e.g. mydata.dat:

program mydata;
uses
  SysUtils, Windows {needed for RT_RCDATA, could be omitted};
{$R mydata.res}
var
  S: TResourceStream;
  F: TFileStream;
begin
  // create a resource stream which points to our resource
  S := TResourceStream.Create(HInstance, 'MYDATA', RT_RCDATA);
  // Replace RT_RCDATA with ??? with what?
  // Please ensure you write the enclosing apostrophes around MYDATA, 
  // otherwise no data will be extracted.
  try
    // create a file mydata.dat in the application directory
    F := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'mydata.dat', fmCreate); 
    try
      F.CopyFrom(S, S.Size); // copy data from the resource stream to file stream
    finally
      F.Free; // destroy the file stream
    end;
  finally
    S.Free; // destroy the resource stream
  end;
end.

많은 클래스(최신 개발버전에서는) FPC 리소스 로딩을 지원합니다. 예를들면,

procedure exampleproc;
var
  Image: TImage
begin
  Image := TImage.Create;
  Image.Picture.LoadFromResourceName(HInstance,'image'); // 확장자가 없는것에 주의
end;