Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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