Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

drawing a line between two points on a tpanel

  • 30-01-2003 01:07PM
    #1
    Registered Users, Registered Users 2 Posts: 2,592 ✭✭✭


    i want to draw a line between startx,starty which are the start positions of the mouse and the current position of the mouse
    this is a snippet of the code that i using but this code will only draw a line on the form that tpanel belongs to not the actual panel itself
    void __fastcall TForm1::mainFrameMouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
    {

    Canvas->MoveTo(startx,starty);
    Canvas->LineTo(X,Y);
    }
    any tips on how to do this would be extremely helpfull
    thanking you
    tommycahir


Comments

  • Registered Users, Registered Users 2 Posts: 2,157 ✭✭✭dazberry


    TPanel does not expose a canvas member (its actually protected in TWinControl).
    In your example the default canvas is that of the form, so by specifying canvas, you are telling it to draw on the form.
    If TPanel.canvas was exposed, you would need to do something like this (sorry its in Delphi, but its would be similar)

    procedure TForm1.mainFrameMouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
    begin
    TPanel(Sender).Canvas.MoveTo(startx,starty);
    etc.
    or
    mainFrame.Canvas.MoveTo
    etc.

    But because as I said the canvas property is not exposed, that won't work.

    You choices are either
    [1] Inherited TPanel into a new component (say TmyPanel) and expose the canvas property (or better yet include the functionality you want in the derived panel.

    [2] Put a TImage inside the canvas and paint on that instead - it will work as my example above TImage(Sender).Canvas.MoveTo() - use the OnMouseDown event from the TImage instead.

    One thing of note: The way you are trying to do this will result in the "drawing" being wiped when the panel is repainted. If you need to keep the drawing there, you will need to use a TImage.

    HTH

    D.


Advertisement