Archive

Posts Tagged ‘text’

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 Print Text

December 15th, 1999 m3Rlin No comments

This is the harder way to print text in Delphi :-) If you want to do it the easier way check out tip 4.1. Well, if for some reasons you may want to print using the Windows API here’s the code:

Code 1:

uses
  Printers, WinSpool;
...
var
  aDriver, aPort, aPrinterName: array[0..255] of Char; { aDriver and aPort won't be used }
  iSize, iN: Integer;
  Info: PAddJobInfo1;
  fPrinter: TextFile;
  hPrinter: THandle;
begin
  Printer.GetPrinter(aPrinterName, aDriver, aPort, hPrinter);
  OpenPrinter(aPrinterName, hPrinter, nil);
  try
    AddJob(hPrinter, 1, nil, 0, iSize); { Get buffer size }
    GetMem(Info, iSize);
    try
      { This function will return the name of the file we can write to }
      AddJob(hPrinter, 1, Info, iSize, iN);
      { Now write to the file }
      AssignFile(fPrinter, Info^.Path);
      Rewrite(fPrinter);
      try
        WriteLn(fPrinter, 'Hello world!');
        WriteLn(fPrinter, 'This is text will be printed...');
      finally
        CloseFile(fPrinter);
      end;
      { Put the file into the printing line, after that Windows will delete it }
      ScheduleJob(hPrinter, Info^.JobId);
    finally
      FreeMem(Info);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;

Code 2:

{ Will work only if the printer is on LPT1 (almost always:-) }
...
LPTHandle := CreateFile('LPT1', GENERIC_WRITE, 0, PSecurityAttributes(nil),
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
...

Then use WriteFile() to send text or:

...
while not TransmitCommChar(LPTHandle, CharToSend) do
  Application.ProcessMessages;
(* This code will send characters to the printer waiting for each one of them
** to be taken care of.
*)