Solved

How to create a .RC file(Resource scripts )

Posted on 2000-02-18
4
417 Views
Last Modified: 2010-08-05
can someone give me detailed instructions on how to do this.
0
Comment
Question by:jonlaukt
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 30 total points
ID: 2536171
Hi
first make a demo project directory  for this then
go to hard drive and find a text file and re-name it test.txt or (make one)

open notepad and type the following line:

TEXT_DEMO  RCDATA C:\test.txt

save this as myres.rc  and put in in delphis bin dir.

TEXT_DEMO is the idendifier ,rcdata is just a datatype and c:\test.txt is the path to your txt file.
 
go to command prompt delphi\bin directory compile myres.rc with brcc32.exe.
if you havent used it before you just at the command prompt type:
brcc32 myres.rc
and you will get a compiled one called myres.res ,put myres.res in your projects directory so it can find it.

now you have your static text file inside a resource file.


in delphi:
create a new project and save it in your project dir that you made where the myres file resides.

drop a richedit on the form
and got to code and add in the private section the following line:
 procedure LoadTextFromResource;
then add in
{myres.rc} in the line below {$R *.DFM}

then add the following procedure:

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;


now drop a button on the form and  make the buttons click event like:

procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;


now compile and run and when the button is clicked the richedit will be filled with your test file...

your complete unit will be like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure LoadTextFromResource;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R Myres.RES}

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;

end.


hope that a help ;-)

Regards Barry

ps
also see the project here:
http://www.bhnet.com.br/~simonet/howtoprojs.htm

about how to create resource only dlls..
0
 

Author Comment

by:jonlaukt
ID: 2536355
thanks for your time
0
 

Author Comment

by:jonlaukt
ID: 2537444
To inthe, yesterday I spent 2 hrs modifying the text file to .exe file, but I still got compiliing errors. iT keeps refering back to the example u have given me. To do it the right way, can u advice as to what codes to modify. thanks, u save my life.
0
 

Author Comment

by:jonlaukt
ID: 2537738
To inthe, yesterday I spent 2 hrs modifying the text file to .exe file, but I still got compiliing errors. iT keeps refering back to the example u have given me. To do it the right way, can u advice as to what codes to modify. thanks, u save my life.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question