Archive

Posts Tagged ‘window’

How to Convert Delphi Forms to Text

December 15th, 1999 m3Rlin No comments

You shouldn’t have too much problem opening a Delphi 4 form in Delphi 2 but you won’t have luck opening a 32 bit or C++ Builder form in Delphi 1. The only way is to save the file in text form. Delphi 5 can do this automatically but in all the previous versions you have to do this all by yourself, form by form.
This is a full featured program that will do all the work for you. The source is dumped into Public Domain.

(* DfmToTxt 1.0
**
** Author: Jacob Dybala (m3Rlin). Public Domain
**   <m3rlin@programmer.net>
**
** Created : January 2 2000
** Modified: April 12 2000
*)
program DfmToTxt; 

{$apptype console} { Generate console application }
{$debuginfo off}
{$localsymbols off}
{$optimizations on}
{$rangechecks off} 

uses
  Classes, SysUtils; 

var
  sNewFileName: string;
  strInput, strOutput: TStream;
begin
  { Initialize streams }
  strInput := nil;
  strOutput := nil; 

  { Get commandline parameters }
  if (ParamCount > 2) or (ParamCount = 0) then begin
    WriteLn('DfmToTxt 1.0');
    WriteLn('By Jacob Dybala (m3Rlin). Public Domain.');
    WriteLn;
    WriteLn('Usage: DfmToTxt <input /> <output></output>');
    Exit;
  end else if not FileExists(ParamStr(1)) then begin
    WriteLn(Format('Error: %s can not be found.', [sNewFileName]));
    Exit;
  end if ExtractFileExt(ParamStr(1) <> '.dfm') then begin
    WriteLn('Error: Input file must be a .dfm (Delphi Form) file');
    Exit;
end; 

if ParamCount = 1 then begin
  sNewFileName := ParamStr(1);
  sNewFileName := ChangeFileExt(sNewFileName, '.txt');
end else begin
  sNewFileName := ParamStr(2);
  if FileExists(sNewFileName) then begin
    WriteLn(Format('Error: A file named %s already exists.', [sNewFileName]));
    Exit;
  end;
end; 

{ Main code }
try
  try
    strInput := TFileStream.Create(ParamStr(1), fmOpenRead);
    strOutput := TFileStream.Create(sNewFileName, fmCreate);
  expect
    Write('Error: Failed creating ');
    WriteLn(sNewFileName);
    Exit;
  end;
  ObjectResourceToText(strInput, strOutput);
  finally
    strInput.Free;
    strOutput.Free;
  end;
end;

How to Create a Gradient Form

December 15th, 1999 m3Rlin No comments

In setup programs or sometimes in AboutBoxes you can find gradient forms.

It sure looks as if it were a lot of work but it’s really very easy. All you have to do is add this code to your program.
This example will create a gradient rectangle on the whole form Canvas. To change it’s size just change the parameters of the Rectangle() function.

Add this code to your form’s OnPaint event:

1) Constant number of steps:

const
  btN = 100; { Number of steps }
var
  iI: Integer;
  Col: TColor;
begin
  for iI := 0 to btN - 1 do
    with Canvas do begin
      Col := RGB(0, 0, Round(50 + 205 * (iI / btN)));
      Pen.Color := Col;
      Brush.Color := Col;
      Rectangle(0, Round(ClientHeight * (iI / btN)), ClientWidth, Round(ClientHeight * ((iI + 1) / btN)));
    end;
end;

To get rid of problems when resizing the form just add this code to the form’s OnResize event:

...
Invalidate;
...

2) It calculates the number of steps:

var
  wHeight, wRow: Word;
begin
  wHeight := (ClientHeight + 255) div 256;
  for Row := 0 to 255 do
    with Canvas do begin
      Brush.Color := RGB(wRow, 0, 0); { Modify this for a different color }
      FillRect(Rect(0, wRow * wHeight, ClientWidth, (wRow + 1) * wHeight)) ;
    end;
end;

3) Hacking our way around… :-)
This function works in Delphi 3 but not in Delphi 4.

uses
  Charts;
...
  GradientFill(Canvas, ClientRect, clBlack, clBlue, False);
...