Hello all! I need to write simple game with Forms, Buttons, but by using only!!! .dpr file.
I tried to transfer the information from .pas, .dfm files and even has achieved success to compile it, but at start there is an exception EResNotFound ('Resourse TForm1 not found')
I shall be very grateful for any help. Now it something like this:

program Project1;
{$R *.res}
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  with Form1 do
  begin
  Left :=192 ;
  Top:= 112  ;
  Width :=696  ;
  Height :=480 ;
  Caption :='Form1'  ;
  Color :=clBtnFace ;
  Font.Charset :=DEFAULT_CHARSET ;
  Font.Color :=clWindowText;
  Font.Height :=-11 ;
  Font.Name :='MS Sans Serif' ;
  Font.Style :=[] ;
  OldCreateOrder :=False ;
  PixelsPerInch :=96 ;
  //TextHeight :=13 ;
  end;
   Application.Run;
end.

Recommended Answers

All 6 Replies

Member Avatar for Micheus

I tried to transfer the information from .pas, .dfm files and even has achieved success to compile it, but at start there is an exception EResNotFound ('Resourse TForm1 not found')

If You don't have dfm to the form You cannot make by this way. You will need to create your form step-by-step in the source code. Try this:

program Project1;
{$R *.res}
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;

var
  Form1: TForm;  // Form base class

begin
  Application.Initialize;
  Application.CreateForm(TForm, Form1);
  with Form1 do
  begin
    Left :=192 ;
    Top:= 112  ;
    Width :=696  ;
    Height :=480 ;
    Caption :='Form1'  ;
    Color :=clBtnFace ;
    Font.Charset :=DEFAULT_CHARSET ;
    Font.Color :=clWindowText;
    Font.Height :=-11 ;
    Font.Name :='MS Sans Serif' ;
    Font.Style :=[] ;
    OldCreateOrder :=False ;
    PixelsPerInch :=96 ;
    //TextHeight :=13 ;
  end;
  Application.Run;
end.

if You was put other components in it, You will need create than at run-time too and set Parent property to the form in order to see it:

...
 // using this approach, You don't need to declare a BitBtn object variable
  with TBitBtn.Create(Form1) do
  begin
    Parent := Form1;
    Caption := 'Ok';
    OnClick := BtnOkClick;
  end;
  ...
  Application.Run;
end.

Bye

>Micheus
Muito obrigado, amigo! Now it work!!!
But I is slightly confused about procs and funcs declaration and description in this code...

Member Avatar for Micheus

Muito obrigado, amigo! Now it work!!!

ops... writing in portuguese!!! :icon_cheesygrin:
Sorry by don't answer you before - I was not notified by e-mail.

But I is slightly confused about procs and funcs declaration and description in this code...

You will need to write procedure code normally and assigned to the events at run-time too.
notice: the .dfm files has only properties information - not codes.

The event's procedures must be declared like (in general): TNotifyEvent(Sender :TObject) of object;
So, your procedure must be declared into your form class definition (see this post) or other class in order to be assigned to any object's event.

You can declare a class that group all objets and functionalities if necessary.
Example:

program Project1;
{$R *.res}
uses
  Windows, Messages, SysUtils, Variants, 
  Classes, Graphics, Controls, Forms, Dialogs;

type
  TGameForm = class (TForm)
    constructor Create(aOnwer :TComponent); override;
  private
    procedure BtnOkClick(Sender :Object);
  end;

// form class codes...
constructor TGameForm.Create(aOnwer :TComponent); 
begin
  inherited;
  Left :=192 ;
  Top:= 112  ;
  Width :=696  ;
  Height :=480 ;
  Caption :='Form1'  ;
  Color :=clBtnFace ;
  Font.Charset :=DEFAULT_CHARSET ;
  Font.Color :=clWindowText;
  Font.Height :=-11 ;
  Font.Name :='MS Sans Serif' ;
  Font.Style :=[] ;
  OldCreateOrder :=False ;
  PixelsPerInch :=96 ;
  //TextHeight :=13 ;
 // creating a bitbtn 
  with TBitBtn.Create(Form1) do
  begin
    Parent := Self;
    Caption := 'Ok';
    OnClick := BtnOkClick;
  end;
end;

// OnClick bitbtn's event
procedure TGameForm.BtnOkClick(Sender :TObject);
begin
  ShowMessage('Hello word!!!');
end;


var
  Form1: TGameForm;  // Form base class

// Main program
begin
  Application.Initialize;
  Application.CreateForm(TGameForm, Form1);
  Application.Run;
end.

Bye

It perfectly looks) and it precisely that it is necessary, but it is impossible to compile this code.(at start there is an exception EResNotFound ('Resourse TGameForm not found'))
Probably I need to change something in adjustments of the compiler?

Member Avatar for Micheus

... but it is impossible to compile this code.(at start there is an exception EResNotFound ('Resourse TGameForm not found'))
Probably I need to change something in adjustments of the compiler?

Sorry. I'm a stupid. I have talking just the opositive in the post #2.

Now I have tested.

program Project1;
{$R *.res}
uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  Buttons;

type
  TGameProject = class(TComponent)
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  private
    GameForm :TForm;
    procedure BtnOkClick(Sender :TObject);
  end;

// form class codes...
constructor TGameProject.Create(AOwner: TComponent);
begin
  inherited;
  Name := 'GameProject';
  with TForm(AOwner) do
  begin
    Left :=192 ;
    Top:= 112  ;
    Width :=696  ;
    Height :=480 ;
    Caption :='Form1'  ;
    Color :=clBtnFace ;
    Font.Charset :=DEFAULT_CHARSET ;
    Font.Color :=clWindowText;
    Font.Height :=-11 ;
    Font.Name :='MS Sans Serif' ;
    Font.Style :=[] ;
    OldCreateOrder :=False ;
    PixelsPerInch :=96 ;
    //TextHeight :=13 ;
  end;
 // creating a bitbtn
  with TBitBtn.Create(AOwner) do
  begin
    Parent := TForm(AOwner);
    Caption := 'Ok';
    OnClick := BtnOkClick;
  end;
end;

destructor TGameProject.Destroy;
begin
  inherited;
end;

// OnClick bitbtn's event
procedure TGameProject.BtnOkClick(Sender :TObject);
begin
  ShowMessage('Hello word!!!');
end;


var
  Form1 :TForm;

// Main program
begin
  Application.Initialize;
  Application.CreateForm(TForm, Form1);
  TGameProject.Create(Form1);
  Application.Run;
end.

Isn't necessary to declare GameProject variable. We have inherited it from TComponent class and setting you owner to main form. So, when the form go to be closed it will be freed.

Bye

The problem is that a form created with the inherited Create constructor assumes that there is a dfm just for it. To get around this, you must redirect you subclass's constructor to CreateNew without any call of inherited.
I don't like to advertise myself, and the topic is not the same, but I've just did a working example in the morning:
http://mozo1.webzona.hu/delphi/delphi_ids.html
This has pas files, but that's generally program code which can go anywhere. The main problem, creating a form without res file is solved.

Watch out for these:
- The {$...} stuffs, called compiler directives, which tells the compiler to include the res files are missing.
- When creating dynamic components at runtime, you set up the owner by the constructor, but you also have to set up the parent manually. Parent is the form that shows the element, owner is the component which frees it. Without this you'll suck alot as I did. Poor me. There are a lot of flawed examples on the net, so really watch out for this!
- The constructor, which is supposed to be a class method is overridden as it would be a virtual one. I don't know how ain't this a syntax error, but it won't run without this. I prefer java... But if it won't run, then the your stuff (or the one that runs instead) will call the inherited Create instead of CreateNew, and you'll suck as I did. Poor me again.
- And that constructor has "CreateNew(AOwner);" instead of "inherited Create(AOwner);", as I've already told. Well, but this is the main thing, the other ones just ruin this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.