Solved

Draw line on Panel

Posted on 2001-08-21
12
464 Views
Last Modified: 2010-04-06
Hi

How can i draw line on panel (TPanel does not have canvas like TForm) ?!


10x
Asi
0
Comment
Question by:asi
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6409202
just drop a tshape on it or a picturebox and draw there a line
0
 
LVL 3

Accepted Solution

by:
Slavak earned 50 total points
ID: 6409217
or you can create a canvas :

Var
 ACanvas : TCanvas;
............................
  ACanvas := TControlCanvas.Create;
  TControlCanvas(ACanvas).Control := Panel;

 
0
 

Expert Comment

by:Aleq
ID: 6409234
TPanel has no DeviceContext, so only way to do this is to put TImage, TShape or similar component on the panel, set align to alClient (and optionally send it to the back).
----
procedure TForm1.Button1Click(Sender: TObject);
var
  Image: TImage;
begin
  Image:=TImage.Create(Panel1);
  Image.Align:=alClient;
  Image.Parent:=Panel1;
  Image.Transparent:=True;
  Image.Canvas.LineTo(Panel1.Width, Panel1.Height);
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Aleq
ID: 6409237
ooops, I'm late...
ok, sorry.
0
 
LVL 3

Expert Comment

by:Slavak
ID: 6409293
all window (TWinControl) have device context.

You can create canvas also as,

 ACanvas := TCanvas.Create;
 ACanvas.Handle := GetDC(Panel1.Handle);

but, i prefer a first way.

Note,
 GetDC(Panel1.Handle) give you a device context, that you can use with window API functions
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6409343
The inheritance chain is the following:
TPanel <- TCustomPanel <- TCustomControl <- ...

TCustomControl has protected Canvas property, the problem is only that none of the descendants make it public.
A quick workaround:

type
  THackPanel = class(TPanel);

  with THackPanel(Panel1) do
  begin
    // you now have access to all protected properties
    Canvas.MoveTo(0, 0);
    Canvas.LineTo(ClientWidth, ClientHeight);
  end;

HTH
TOndrej
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6409385
thats it, tondrej :-)
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6409395
yes kretzschmar :-)
I went through all the previous comments several times to make sure that none of them mentioned this well-known trick...
I was surprised :-P
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6409403
i know the trick, but thought not on it :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6409415
btw tondrej (sorry to be off topic)
how are you?
what about mike lischke?
are you two working already together?

(many more questions in mind)
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6409427
1. fine, busy, thanks :-)
2. fine too, busy, too :-)
3. yes! :-P
sorry for offtopic, I've included my e-mail in my profile, if you want to write
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6409453
i will email you, tondrej
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

696 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