Lazarus

Free Pascal => Beginners => Topic started by: JLWest on September 26, 2020, 07:13:29 pm

Title: How to draw a box.
Post by: JLWest 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   
Title: Re: How to draw a box.
Post by: wp on September 26, 2020, 07:33:54 pm
In order to display such a box in a memo, for example, you should do this:
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;
Title: Re: How to draw a box.
Post by: winni 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
Title: Re: How to draw a box.
Post by: JLWest 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.
Title: Re: How to draw a box.
Post by: winni 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
Title: Re: How to draw a box.
Post by: JLWest 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.

.

Title: Re: How to draw a box.
Post by: Handoko 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
Title: Re: How to draw a box.
Post by: JLWest on September 27, 2020, 04:51:13 am
hi handako

Thanks
Title: Re: How to draw a box.
Post by: winni 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
Title: Re: How to draw a box.
Post by: JLWest 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. 
Title: Re: How to draw a box.
Post by: winni 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


Title: Re: How to draw a box.
Post by: JLWest 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?   
Title: Re: How to draw a box.
Post by: winni 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
Title: Re: How to draw a box.
Post by: jamie 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!  %)
Title: Re: How to draw a box.
Post by: winni 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 (http://www.pp4s.co.uk/main/tu-oop-canvas-intro.html)

Winni

Title: Re: How to draw a box.
Post by: JLWest on September 28, 2020, 02:14:29 am
with

 
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;

I didn't get any lines.

@Jamie a PDF is attached. Just a few boxes will do. To big so it's at this link.

https://drive.google.com/file/d/1NF3Xft14bLi0raQKvhB2IfDm725-ym2Z/view?usp=sharing

@ winni

I tried the intro and the first try wouldn't compile,

@winni These are the instructions in the
ttp://www.pp4s.co.uk/main/tu-oop-canvas-intro.html

{In Lazarus, Project > New project, select Application.
   View Units and select Project1.  Copy this code into Project1
   and save the unit as unit1 (although it will not be needed).
   Save the project as CanvasDemo2.}

However the code is for a program not an application. I have tried it 4 times and can't get it to compile.
Title: Re: How to draw a box.
Post by: jamie on September 28, 2020, 03:08:32 am
I can not down load that PDF.. Every step I take its trying to get me to  install software I don't want and sign up for services that will cost me in the end..

  A simple PNG image which is small attached here would of been just fine..

  I just down loaded a 10-10EZ health benefit form.

 In any case If you are looking to use scattered layout then I guess  a Tgrid is out of the question I would think

 But if they are in a formatted arrangement then you can use the TStringGrid and select an editor for the cell..

 The grid lines can be set for thick lines, color etc.

 If you don't like that idea, then a bunch of panels with anchors to each other with the edit controls inside..

 how many boxes are going to be alive here ?


EDIT:
  have you considered using a Spread sheet component ?

  That will allow you to have enter fields, adjust your lines etc.

https://wiki.lazarus.freepascal.org/FPSpreadsheet

You only need to download and install it..
there are tutorials for you to look at too.
Title: Re: How to draw a box.
Post by: JLWest on September 28, 2020, 03:57:59 am
Thanks I'll take a look.

Maybe I'll just give up on the idea.
Title: Re: How to draw a box.
Post by: winni on September 29, 2020, 11:30:20 am
Hi!

Put the follwing code into your TForm1.
Connect it with the OnPaint event of TForm1.
Done.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. begin
  3. with Canvas do
  4.          begin
  5.           TextOut (5,5,'One')
  6.           TextOut (5,25,'Two');
  7.           TextOut (5,65,' Four');    
  8.         end; // with
  9. end;                

Winni
Title: Re: How to draw a box.
Post by: Handoko on September 29, 2020, 12:03:18 pm
I can not down load that PDF.. Every step I take its trying to get me to  install software I don't want and sign up for services that will cost me in the end..

I did not have problem downloading the file from Google Drive. Below is the screenshot of the pdf viewing using Linux's Atril Document Viewer:
Title: Re: How to draw a box.
Post by: Handoko on September 29, 2020, 02:04:12 pm
I didn't fully follow this thread. But, hey it is not difficult to write something like the pdf form using Pascal using TEdit, TCheckBox and TShape:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     CheckBox1: TCheckBox;
  16.     CheckBox2: TCheckBox;
  17.     CheckBox3: TCheckBox;
  18.     CheckBox4: TCheckBox;
  19.     CheckBox5: TCheckBox;
  20.     CheckBox6: TCheckBox;
  21.     Edit1: TEdit;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     Label3: TLabel;
  25.     Shape1: TShape;
  26.     Shape2: TShape;
  27.     Shape3: TShape;
  28.     procedure FormCreate(Sender: TObject);
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   Edit1.Text        := '';
  43.   Edit1.Color       := clWhite;
  44.   Edit1.BorderStyle := bsNone;
  45. end;
  46.  
  47. end.

It works on Linux but it should works too on Windows with no or some modifications.
Title: Re: How to draw a box.
Post by: JLWest on September 29, 2020, 06:20:37 pm
This is what I'm try to enhance with boxes or double borders around some controls. In the upper right of the form I would like to have borders or boxes around the items.

May seem like a  rather small item but actually I have been working on this for over two years (off and on of course).

It is a specialized editor which will allow a user to edit the Apt.Dat file(s) in X-Plane 11. The file has over 9 million variable length text records. There are two complete copies of the file on the system and a single purpose apt.dat  file for each Custom airports installation.  My system has 20 apt.dat's 18 custom airports, the global and Resource file.


 
Title: Re: How to draw a box.
Post by: Handoko on September 29, 2020, 06:34:33 pm
This is what I'm try to enhance with boxes or double borders around some controls.

I personally think using background color is better than double-lined box, it catches attention and looks better. And it can be easily done by setting the TShape.Brush.Color property.
Title: Re: How to draw a box.
Post by: JLWest on September 29, 2020, 06:52:32 pm
@Handoko
Yea, you might be right.

@winni

I recoded and still don't get boxes. Maybe that's a operating thing.  I coded the Onpaint event of the Form.  I don't know.

@handoko

I think I can get a long way with TShape's.
Title: Re: How to draw a box.
Post by: winni on September 30, 2020, 10:48:47 pm
Hi!

I made you an example for a box.
And I made you an example for text - without a box.

I thought you could combine the two examples.
Obviously not .

You should read the introduction to the canvas.

Here is an example for a Turbo box  with text.
Result in the attachment.
Code: Pascal  [Select][+][-]
  1. procedure PaintImage(Img: TImage);
  2. var PMidleft, PMidRight : TPoint;
  3.     R : Trect;
  4. begin
  5. R := Rect(10,10,200,140);
  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,90);
  14.                   PMidRight :=Point(R.Right,90);
  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.  
  19.  
  20.                   Font.Height := 14;
  21.                   TextOut(15,15,'The Doctor is in ');
  22.                   TextOut (15,30,'Fr - Su   22°° - 4°°');
  23.                   TextOut(15,100,'Dr. Hackenbush');
  24.          end; // with
  25. end;
  26.  
  27. procedure TForm1.Button1Click(Sender: TObject);
  28. begin
  29.    PaintImage(Image1);
  30. end;
  31.    

Winni
Title: Re: How to draw a box.
Post by: JLWest on October 01, 2020, 12:54:36 am
@ winni - Thank You

Something came up and I'm just now getting back to this. I had stated the research on the
paint a box, but had to stop.

On your comment "Obviously not' that may in fact be true in the end. But I did make a little progress before having to stopping.

When I get an answer to a problem on the form I usually make a demo of the solution in a directory (CODLIB). There are 202 sub-directories in the CODLIB Soon to be 203.

You may say WOW what a lot of work. Bit I do it because I can then copy the Unit1.pas to a text file, load it into an editor and set the front to 22 and read the code. Plus I can return to the demo.

It is true that ctl and the wheel will enlarge most things but you get you forms all setup to code and test. Then you have to read some code and I would have to take the editor to full screen. Enlarge the code. Then go back to my working setup.

So I have two monitors. One where I write code in an editor with the font size 22. Then I copy and past.

Going to set up you code in a demo and see how it works.

Thanks again.
TinyPortal © 2005-2018