Solved

Display characters and numbers on the canvas

Posted on 2007-03-24
5
223 Views
Last Modified: 2010-04-05
Dear Experts,

I have a procedure called FillBuffer1Click that fills the whole canvas with characters and numbers.
But when I click on the menu-item, I see nothing, what do I do wrong:

Const
  cdDefault = $00;
  cdFieldAttr = $01;
  caDefault = $00;
  caProtect = $20;
  caNumeric = $10;
  caSelPen = $04;
  caIntens = $08;
  caNonDisp = $0C;
  caMDT = $01;
  ccDefault = $00;
  ccBlue = $F1;
  ccRed = $F2;
  ccPink = $F3;
  ccGreen = $F4;
  ccTurquoise = $F5;
  ccYellow = $F6;
  ccWhite = $F7;
  chDefault = $00;
  chBlink = $F1;
  chReverse = $F2;
  chUnderLine = $F4;
  csDefault = $00;
  csAPL = $F1;

type
  TScrCell = record
    Data: char;
    Attrib: char;
    Hilite: char;
    CharSet: char;
    Color: char;
    OldColor: char;
    IsOldColorSet: Boolean;
  end;

  TScrBuf = array[1..25, 1..80] of TScrCell;
  PScrBuf = ^TScrBuf;

 
  TCharSet = set of char;
const ValidChars: TCharSet = ['A'..'Z', 'a'..'z', '0'..'9'];

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    FillBuffer1: TMenuItem;
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FillBuffer1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
      procedure ShowBuf;
      procedure DispCell(x: integer; y: integer; PaintBlanks: boolean);
      function ColorMapI2W(ic: char): TColor;
  end;

var
  MainForm: TMainForm;
  SCRROWS: integer;      
  SCRCOLS: integer;      
  FontWidthPix: integer;  
  FontHeightPix: integer;  
  ScrBuf: TScrBuf;        
  ColorRed: TColor;
  ColorGreen: TColor;
  ColorBlue: TColor;
  ColorYellow: TColor;
  ColorPink: TColor;
  ColorTurq: TColor;
  ColorWhite: TColor;
  ColorBlack: TColor;

implementation

{$R *.dfm}

procedure TMainForm.FormPaint(Sender: TObject);
begin
  ShowBuf;
end;
(*---------------------------------------------------*)
procedure TMainForm.ShowBuf;
var
  i,j:integer;
begin
  Canvas.Lock;  
  try
    with Canvas do
    begin
    Brush.Style := bsSolid;    
    Brush.Color := ColorBlack;  
    FillRect(ClientRect);    
    end;
     for i := 1 to SCRROWS do  
    for j := 1 to SCRCOLS do    
      DispCell(j, i, false);
 // DrawCursor(CsrCol, CsrRow);
 // ShowStatus ;
  finally
    Canvas.Unlock;    
    end;
 end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  SCRCOLS := 80;    
  SCRROWS := 24;  
  ColorRed := clRed;
  ColorGreen := clLime;
  ColorBlue := clBlue;
  ColorYellow := clYellow;
  ColorPink := clFuchsia;
  ColorTurq := clAqua;
  ColorWhite := clWhite;
  ColorBlack := clBlack;
end;
(*--------------------------------------*)
procedure TMainForm.DispCell(x, y: integer; PaintBlanks: boolean);
var
  xpos, ypos: integer;
begin
  xpos := (x * FontWidthPix) - FontWidthPix;
  ypos := (y * FontHeightPix) - FontHeightPix;
  with Canvas do
  begin
    Brush.Style := bsSolid;
    Brush.Color := ColorBlack;
    if (ScrBuf[y, x].data = Char(cdDefault)) or
      (ScrBuf[y, x].data = Char(cdFieldAttr)) then
    begin
      if PaintBlanks then
      begin
        Font.Style := [];
        TextOut(xpos, ypos, ' ');
      end;
    end
    else
    begin
      if (Byte(ScrBuf[y, x].attrib) and caNonDisp) <> caNonDisp then
      begin
        Pen.Mode := pmCopy;
        Font.Color := ColorMapI2W(ScrBuf[y, x].color);
        if ScrBuf[y, x].hilite = Char(chDefault) then
          Font.Style := []
        else if ScrBuf[y, x].hilite = Char(chReverse) then
        begin
          Font.Style := [];
          Pen.Mode := pmXor;
          Brush.Color := ColorMapI2W(ScrBuf[y, x].color);
          Font.Color := ColorBlack;
        end
        else if ScrBuf[y, x].hilite = Char(chUnderLine) then
          Font.Style := [fsUnderline]
        else if ScrBuf[y, x].hilite = Char(chBlink) then
          Font.Style := [fsStrikeout]
        else
          Font.Style := [];
        if (Byte(ScrBuf[y, x].attrib) and caIntens) = caIntens then
          Font.Style := Font.Style + [fsBold];
        TextOut(xpos, ypos, ScrBuf[y, x].data);
      end
      else
        TextOut(xpos, ypos, ' ');
    end;
  end;
end;
(*--------------------------------------*)
function TMainForm.ColorMapI2W(ic: char): TColor;
begin
  if Byte(ic) = ccDefault then Result := ColorGreen
  else if Byte(ic) = ccBlue then Result := ColorBlue
  else if Byte(ic) = ccRed then Result := ColorRed
  else if Byte(ic) = ccPink then Result := ColorPink
  else if Byte(ic) = ccGreen then Result := ColorGreen
  else if Byte(ic) = ccYellow then Result := ColorYellow
  else if Byte(ic) = ccTurquoise then Result := ColorTurq
  else if Byte(ic) = ccWhite then Result := ColorWhite
  else Result := ColorGreen;
end;
(*--------------------------------------*)
procedure TMainForm.FillBuffer1Click(Sender: TObject);
var
  i, j, k: integer;
  ch: char;
begin
  k := 0;
  i := 1;
  while i <= SCRROWS do
  begin
    j := 1;
    while j <= SCRCOLS do
    begin
      ch := chr(k);
      if ch in ValidChars then
      begin
        ScrBuf[i, j].data := ch;
        inc(j);
      end;
      k := k + 1;
      if k > 255 then
        k := 0;
    end;
    inc(i);
  end;
end;
(*--------------------------------------*)
end.

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
dinilud earned 500 total points
ID: 18784801
First thing you have to set values of tese variables
   FontWidthPix,FontHeightPix


Second Use invalide function after filling the buffer

(*--------------------------------------*)
procedure TMainForm.FillBuffer1Click(Sender: TObject);
var
  i, j, k: integer;
  ch: char;
begin
  k := 0;
  i := 1;
  while i <= SCRROWS do
  begin
    j := 1;
    while j <= SCRCOLS do
    begin
      ch := chr(k);
      if ch in ValidChars then
      begin
        ScrBuf[i, j].data := ch;
        inc(j);
      end;
      k := k + 1;
      if k > 255 then
        k := 0;
    end;
    inc(i);
  end;
  Invalidate;
end;
(*--------------------------------------*)
0
 
LVL 1

Author Comment

by:peterkiers
ID: 18785956
Thanks for the answer.

1. What does Invalidate?

2. First I have used a Timage as a display of text. Now I have
    choose to use the Form's canvas. This was what I had
    when i used the Timage canvas:

procedure TMainForm.FormCreate(Sender: TObject);
var
  TxMetric: TTextMetric;
  Bitmap: TBitmap;
begin
  ColorRed := clRed;
  ColorGreen := clLime;
  ColorBlue := clBlue;
  ColorYellow := clYellow;
  ColorPink := clFuchsia;
  ColorTurq := clAqua;
  ColorWhite := clWhite;
  ColorBlack := clBlack;
  SCRCOLS := 80;
  SCRROWS := 24;
  CurCellColor := Char(ccDefault);
  CurCellHilite := Char(chDefault);
  CsrShape := csrUnderLine;
  CsrRow := 1;
  CsrCol := 1;
  SocOpen := False;
  FontName := 'Terminal';
  FontSize := 10;
  Bitmap := TBitmap.Create;
  ScrImage1.Canvas.Font.Size := FontSize;
  ScrImage1.Canvas.Font.Name := FontName;
  GetTextMetrics(ScrImage1.Canvas.Handle, TxMetric);
  FontWidthPix := TxMetric.tmMaxCharWidth;
  FontHeightPix := TxMetric.tmHeight;
  Bitmap.Height := SCRROWS * FontHeightPix + (FontHeightPix + 2);
  Bitmap.Width := SCRCOLS * FontWidthPix;
  ScrImage1.Picture.Graphic := Bitmap;
  ScrImage1.Canvas.Font.Size := FontSize;
  ScrImage1.Canvas.Font.Name := FontName;
  ClientHeight := SCRROWS * FontHeightPix + (FontHeightPix + 2);
  ClientWidth := SCRCOLS * FontWidthPix;
  ClearBuf;
  ShowBuf;
end;

In this procedure above i used to specify the font in the TImage canvas
Now I use the form's canvas. What do I have to change about the procedure?

Peter

0
 
LVL 1

Author Comment

by:peterkiers
ID: 18785961
Or do I need to change event.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 18786107
I allready did it myself:

procedure TMainForm.FormCreate(Sender: TObject);
var
  TxMetric: TTextMetric;
begin
  SCRCOLS := 80;          
  SCRROWS := 24;          
  ColorRed := clRed;
  ColorGreen := clLime;
  ColorBlue := clBlue;
  ColorYellow := clYellow;
  ColorPink := clFuchsia;
  ColorTurq := clAqua;
  ColorWhite := clWhite;
  ColorBlack := clBlack;
  FontName := 'Terminal';  
  FontSize := 10;          
  Font.Size := FontSize;
  Font.Name := FontName;
  GetTextMetrics(Canvas.Handle, TxMetric);
  FontWidthPix := TxMetric.tmMaxCharWidth;
  FontHeightPix := TxMetric.tmHeight;
end;

500 points are comming your way, but I still wanna know
what Invalidate means.

Greetings,

Peter Kiers
0
 
LVL 10

Expert Comment

by:dinilud
ID: 18790831
Invalidate procedure will repaint the control.

After changing the character buffer you have to rapaint the window.


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 196
calling Tcolordialog in dll make the form disappear 8 110
LAN or WAN ? 11 89
How to build JSON File in Delphi 6 3 32
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now