Home > OS >  How do I change the color of the line I enter in a Delphi file, so when I visualize it later differe
How do I change the color of the line I enter in a Delphi file, so when I visualize it later differe

Time:09-09

I have a form program in Delphi. I've made a log for different actions in the program. I'm pretty sure I need to save it in rtf file with different colors depending on the line I'm entering but don't know how to change the color. I'm also opening the file with richedit, so I can see the different colors when done.

This is the procedure I use to enter the lines previously inserted in a stringlist(logText), whenever I want the actions added to the log file(logfil). Usually I do it when there's a new login and when a session of an account is ended. At the moment I use .txt file but I'm pretty sure I need rtf?

procedure AddToLogFile();

var
  i : integer;

begin
  with frmMain do
  begin
    i := 0;
    AssignFile(logFile, 'C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\LogFile.txt');
    Append(logFile);
    while i < logText.Count do
    begin

      Writeln(logFile, logText.Strings[i]);
      i := i   1;
    end;
    CloseFile(logFile);
  end;
end;

This is how I add text to the stringlist(logText)

dateTimeNow := Now;

  logText.Add('<'   DateTimeToStr(dateTimeNow)   '> A new flight was added');

And this is how I call the procedure LogFileUse.AddToLogFile;

CodePudding user response:

Yes, your could use TRichEdit like this:

RichEdit1.Lines.Add('Hello World!');
RichEdit1.SelAttributes.Color := clRed;
RichEdit1.Lines.Add('Delphi rocks');
RichEdit1.Lines.SaveToFile('text.rtf');

You may also create your log file directly in the RTF format. RTF file format is actually plain text format. It contains "tags" which select all text attribute, a bit like HTML does.

The easiest way writing your RTF file directly is to generate one sample (Like the text.rtf file generated above) and open it using notepad. You'll then easily see which tags you have to use and reproduce that in your log file.

The code below will probably help you:

procedure TForm2.Button2Click(Sender: TObject);
var
    LogFile : TextFile;
begin
    // Open the log file and write RTF header with color table
    AssignFile(LogFile, 'd:\Temp\text1.rtf');
    Rewrite(LogFile);
    WriteLn(LogFile, '{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}}'#13#10  
                     '{\colortbl ;'#13#10  
                     '\red0\green0\blue0;'#13#10  
                     '\red255\green0\blue0;'#13#10  
                     '\red0\green0\blue255;}'#13#10  
                     '\viewkind4\uc1'#13#10  
                     '\pard\f0\fs18'#13#10);

    // Write a few text in various colors    
    WriteLn(LogFile, '\cf1 Line in black\par');
    WriteLn(LogFile, '\cf2 Line in red\par');
    WriteLn(LogFile, '\cf3 Line in blue\par');

    // Finalize and close the file
    WriteLn(LogFile, '}');
    CloseFile(LogFile);
end;

Of course, you can load the generated RTF file into a TRichEdit to see it. In your real code, you should probably encapsulate all that in a nice object to make using it easier.

Another similar option is to format your log file as an HTML document.

  • Related