Recent

Author Topic: How to draw a box.  (Read 4056 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
How to draw a box.
« on: September 26, 2020, 07:13:29 pm »
I would like to make a form showing boxes on a form using the ╦, ╠ etc. characters. At one time I saw post or demo on this but can't find it.

Is there an algorithm for this?

Thanks   
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: How to draw a box.
« Reply #1 on: September 26, 2020, 07:33:54 pm »
In order to display such a box in a memo, for example, you should do this:
  • Switch the memo font to "Courier New"
  • Open the Lazarus character table ("Edit" > "Insert from Character map", tab "Unicode").
  • Select "Box Drawing" in the combobox "Range"
  • Click each symbol needed to copy it into the source editor. Repeat with the same or other symbols to create a line of the drawing.
    Code: Pascal  [Select][+][-]
    1. const
    2.   sTOP =     '╔════════════════════════════╗';
    3.   sSIDE =    '║                            ║';
    4.   sHOR_DIV = '╟────────────────────────────╢';
    5.   sBOTTOM =  '╚════════════════════════════╝';
    6.  
    7. { TForm1 }
    8.  
    9. procedure TForm1.FormCreate(Sender: TObject);
    10. begin
    11.   Memo1.Lines.Add(sTOP);
    12.   Memo1.Lines.Add(sSIDE);
    13.   Memo1.Lines.Add(sHOR_DIV);
    14.   Memo1.Lines.Add(sSIDE);
    15.   Memo1.Lines.Add(sBOTTOM);
    16. end;

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #2 on: September 26, 2020, 07:41:57 pm »
    Hi!

    Drawing boxes with ╦, ╠ etc. is old Turbo stuff.

    If you find such stuff it will presumable not work:

    Those signs were part of the IBM-8bit-charset, which is not in use anymore.
    You have to replace these glyphs  with there corresponding UTF8 chars.

    And the code will have problems because those box drawing signs have more than one byte - unlike the old Turbo chars.

    But a basic question:

    Why do you want to do that?
    It is so much easier to draw boxes on a canvas than
    to do complicated stuff with old Turbo semigraphics, which were allways a makeshift.

    Winni
    « Last Edit: September 26, 2020, 07:44:55 pm by winni »

    JLWest

    • Hero Member
    • *****
    • Posts: 1293
    Re: How to draw a box.
    « Reply #3 on: September 26, 2020, 08:17:48 pm »
    @Wp Thanks, Can that be adapted to draw on a form no a memo?

    @ Winni

    It's a long answer but the short of it is looks.

    I have a program that reads 8+ million records ( Variable length text records ). Out of the 8 million records I'm interested in editing a very small percentage (2 - 4%) I have the viewer working to display the records but I would really like it to look slick.

    From what little I know of the paint it's all solid lines. But maybe your right winni.
    FPC 3.2.0, Lazarus IDE v2.0.4
     Windows 10 Pro 32-GB
     Intel i7 770K CPU 4.2GHz 32702MB Ram
    GeForce GTX 1080 Graphics - 8 Gig
    4.1 TB

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #4 on: September 26, 2020, 09:29:21 pm »
    Hi

    Short code:

    Put an Image and a Button on your Form.

    Code: Pascal  [Select][+][-]
    1. procedure PaintImage(Img: TImage);
    2. var PMidleft, PMidRight : TPoint;
    3.     R : Trect;
    4. begin
    5. R := Rect(10,10,120,80);
    6. with Img.Canvas do
    7.          begin
    8.                   Brush.Color := clWhite;
    9.                   Fillrect (0,0,Img.Width, Img.Height);
    10.                   Pen.Color := clred;
    11.                   Rectangle(R);
    12.                   InflateRect(R,-2,-2);
    13.                   PMidLeft := Point(R.left,50);
    14.                   PMidRight :=Point(R.Right,50);
    15.                   Pen.Color := clBlue;
    16.                   Rectangle(R.left,R.Top,PMidRight.x, PMidRight.y+1);
    17.                   Rectangle(PMidLeft.x,PMidLeft.y,R.Right,R.Bottom);
    18.          end; // with
    19. end;
    20.  
    21. procedure TForm1.Button1Click(Sender: TObject);
    22. begin
    23. PaintImage(Image1);
    24. end;
    25.  
    26.  

    And see the result in the attachment.

    Winni

    JLWest

    • Hero Member
    • *****
    • Posts: 1293
    Re: How to draw a box.
    « Reply #5 on: September 27, 2020, 03:19:34 am »
    Haven been able to make it run yet. Trying to run down InflateRect which it says unit1.pas(45,19) Error: Identifier not found "InflateRect". Suspect I need something in my use clause.

    Well it isn't LCL;

    But I see where you drew a box around the image. Is it possible to  draw a free standing box on TForm1.

    .

    « Last Edit: September 27, 2020, 03:39:29 am by JLWest »
    FPC 3.2.0, Lazarus IDE v2.0.4
     Windows 10 Pro 32-GB
     Intel i7 770K CPU 4.2GHz 32702MB Ram
    GeForce GTX 1080 Graphics - 8 Gig
    4.1 TB

    Handoko

    • Hero Member
    • *****
    • Posts: 5131
    • My goal: build my own game engine using Lazarus
    Re: How to draw a box.
    « Reply #6 on: September 27, 2020, 04:07:02 am »
    Error: Identifier not found "InflateRect". Suspect I need something in my use clause.

    It is in lclintf unit:
    https://wiki.lazarus.freepascal.org/lclintf

    JLWest

    • Hero Member
    • *****
    • Posts: 1293
    Re: How to draw a box.
    « Reply #7 on: September 27, 2020, 04:51:13 am »
    hi handako

    Thanks
    FPC 3.2.0, Lazarus IDE v2.0.4
     Windows 10 Pro 32-GB
     Intel i7 770K CPU 4.2GHz 32702MB Ram
    GeForce GTX 1080 Graphics - 8 Gig
    4.1 TB

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #8 on: September 27, 2020, 12:09:45 pm »
    Hi!

    For the inflateRect you do not need to dive deep in the units and use lclintf.
    It is defined in unit Types.

    Connect the procedure FormPaint with the OnPaint event of your Form.
    Code: Pascal  [Select][+][-]
    1. uses ... Types;
    2.  
    3. procedure TForm1.FormPaint(Sender: TObject);
    4. var PMidleft, PMidRight : TPoint;
    5.     R : Trect;
    6. begin
    7. R := Rect(10,10,120,80);
    8. with Canvas do
    9.          begin
    10.                   Brush.Color := clWhite;
    11.                   Fillrect (0,0,self.Width, self.Height);
    12.                   Pen.Color := clred;
    13.                   Rectangle(R);
    14.                   InflateRect(R,-2,-2);
    15.                   PMidLeft := Point(R.left,50);
    16.                   PMidRight :=Point(R.Right,50);
    17.                   Pen.Color := clBlue;
    18.                   Rectangle(R.left,R.Top,PMidRight.x, PMidRight.y+1);
    19.                   Rectangle(PMidLeft.x,PMidLeft.y,R.Right,R.Bottom);
    20.          end; // with
    21. end;                

    If you want to frame the complete Form you have to enlarge the rect.

    Winni

    JLWest

    • Hero Member
    • *****
    • Posts: 1293
    Re: How to draw a box.
    « Reply #9 on: September 27, 2020, 06:07:26 pm »
    @winni

    Yea, I got it to work.

    I want to draw boxes on the form not necessarily around controls. But all the examples I have found only do that. 
    FPC 3.2.0, Lazarus IDE v2.0.4
     Windows 10 Pro 32-GB
     Intel i7 770K CPU 4.2GHz 32702MB Ram
    GeForce GTX 1080 Graphics - 8 Gig
    4.1 TB

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #10 on: September 27, 2020, 06:30:37 pm »
    Hi!

    I have not understood your question .

    But too make things clear:

    In the last code I paint on the canvas of the Form.
    All visual components of that Form are in Front of the canvas -
    otherwise you would not see them.

    I made in example how a button is in front of the Form.Canvas.
    In the attachment.

    Second: On every component that has a canvas you can paint on:
    Image, PaintBox, Panel, Form, ...

    On components that don't on a canvas you can't paint on. Or only with hardrock tricks. : Button, GroupBox, ....

    If you need different Boxes with Turbo-style rectangles then you can use Panels and draw on therir canvas.

    Winni



    JLWest

    • Hero Member
    • *****
    • Posts: 1293
    Re: How to draw a box.
    « Reply #11 on: September 27, 2020, 09:29:08 pm »
    @winni

    I think I kind of understand it. But to me it seems rather limiting. If I wanted to design a form   with boxes for user entry of various data in each box and then print the form for use each box would have to be on a panel and limited to one line.

    Wonder how Adobe does it?   
    FPC 3.2.0, Lazarus IDE v2.0.4
     Windows 10 Pro 32-GB
     Intel i7 770K CPU 4.2GHz 32702MB Ram
    GeForce GTX 1080 Graphics - 8 Gig
    4.1 TB

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #12 on: September 27, 2020, 09:52:21 pm »
    Hi!

    On a canvas you can print as much lines as there is space:

    Code: Pascal  [Select][+][-]
    1. With Canvas do
    2.     begin
    3.       TextOut (5,5,'One')
    4.       TextOut (5,25,'Two');
    5.       TextOut (5,65,' Four');
    6.     end;

    Winni
    « Last Edit: September 27, 2020, 11:13:30 pm by winni »

    jamie

    • Hero Member
    • *****
    • Posts: 6090
    Re: How to draw a box.
    « Reply #13 on: September 27, 2020, 10:21:43 pm »
    @winni

    I think I kind of understand it. But to me it seems rather limiting. If I wanted to design a form   with boxes for user entry of various data in each box and then print the form for use each box would have to be on a panel and limited to one line.

    Wonder how Adobe does it?   

    Show us a picture of Adobe in action and we'll show you the code to make it happen! Most likely for free, Too!  %)
    The only true wisdom is knowing you know nothing

    winni

    • Hero Member
    • *****
    • Posts: 3197
    Re: How to draw a box.
    « Reply #14 on: September 27, 2020, 11:23:16 pm »
    @JLWest

    Introduction to the canvas in Delphi and Lazarus:

    http://www.pp4s.co.uk/main/tu-oop-canvas-intro.html

    Winni


     

    TinyPortal © 2005-2018