• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

Draw line on Panel

Hi

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


10x
Asi
0
asi
Asked:
asi
  • 5
  • 3
  • 2
  • +1
1 Solution
 
kretzschmarCommented:
just drop a tshape on it or a picturebox and draw there a line
0
 
SlavakCommented:
or you can create a canvas :

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

 
0
 
AleqCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
AleqCommented:
ooops, I'm late...
ok, sorry.
0
 
SlavakCommented:
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
 
TOndrejCommented:
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
 
kretzschmarCommented:
thats it, tondrej :-)
0
 
TOndrejCommented:
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
 
kretzschmarCommented:
i know the trick, but thought not on it :-)
0
 
kretzschmarCommented:
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
 
TOndrejCommented:
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
 
kretzschmarCommented:
i will email you, tondrej
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now