Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2003, 05:23 AM
DelUS DelUS is offline
Senior Member
 
Join Date: Jan 2001
Posts: 105
Default how can i Draw a Line on canvas?

Hi all,

I have to draw a line on my Form canvas. My starting point is the middle of the form say x= 300 and y = 300. I have a choice to move to left or right. suppose i have moved to left by 100 (x become 200) and then i have moved to right by 50. so the final line is between 250 to 300. when i am moving to right after a movement to left the line is not cleaning up. i mean my final result is a line between 200 to 300. Always i want horizontal or vertical line only.

looking for a help

Jomon
Reply With Quote
  #2  
Old 10-28-2003, 05:58 AM
pollo pollo is offline
Senior Member
 
Join Date: Jun 2002
Posts: 342
Default RE: how can i Draw a Line on canvas?

Hi!

I have an idea, it might help you but is a little complicated. I once used it to make an application like paint for the erase tool.

You are drawing on a surface, for instance, a background image. So, you can have a copy of that image in memory. Then when you need to erase the line just replace the pixels of the line with the originals.

Hope it helps!
Pollo
Reply With Quote
  #3  
Old 10-28-2003, 08:20 AM
Eddy-B Eddy-B is offline
Senior Member
 
Join Date: Feb 2002
Posts: 1,467
Default RE: how can i Draw a Line on canvas?

Just use LineTo and MoveTo as you would expect, but play around with the Pen.Mode property. I think pmNot would create the desired effect, but you might want to check the other values.
Go to TPen help to find out out what they are !

Regards,

Eddy-B

Please click Accept as answer if this helped
Reply With Quote
  #4  
Old 10-28-2003, 08:37 AM
nikolayj nikolayj is offline
Senior Member
 
Join Date: May 2002
Posts: 335
Default RE: how can i Draw a Line on canvas?

Hi how about this:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
nCurrX, nCurrY, nPointX, nPointY: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
nCurrX := X;
nCurrY := Y;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
with Canvas do begin
MoveTo(nPointX, nPointY);
LineTo(nCurrX, nCurrY);
end;

end;

procedure TForm1.FormClick(Sender: TObject);
begin
nPointX := nCurrX;
nPointY := nCurrY;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
nPointX := 300;
nPointY := 300;
end;

end.

Click Accept if Helped
Reply With Quote
  #5  
Old 10-28-2003, 09:07 AM
DelUS DelUS is offline
Senior Member
 
Join Date: Jan 2001
Posts: 105
Default RE: how can i Draw a Line on canvas?

Hi

Thanks for the response. when i increased the width of pen(say 4) the line is smooth. also there is pixel left when we move to left after a move to right. I tried the pen mode value to pmnotxor. both have the same effect. hope u r experience with drawing toolbar in microsoft word application. As long as we are keeping the mouse button down we can draw any line. this is what i am looking for.

Rgds,
Jomon
Reply With Quote
  #6  
Old 10-28-2003, 09:21 AM
Eddy-B Eddy-B is offline
Senior Member
 
Join Date: Feb 2002
Posts: 1,467
Default RE: how can i Draw a Line on canvas?

so actually you want to have the user "drag" a line until you "drop" it unto its final location/position ?

Then you should do it the correct way:
  • draw a line;
  • then when your ending position has changed remove the current line;
  • and draw the new one;
  • repeat this procerss until final ending point achieved.

    Since all your lines start at the same position, and will only move horizontally, as you said, you only need to store the current X position. Then when a new ending-point is determined, use the stored X position to remove the old line. The best way to do this (same way windows is doing it with word, or with moving a window around for example) is to use the XOR method: any color on the screen (background) will be XORed with FF, meaning you will get the direct opposite of it: white will result in black, bue in yellow etc..
    That way, when you perform this operation twice all changes are undone without having to "memorize" anything you might draw over.

    good luck !

    Eddy-B

    Please click Accept as answer if this helped
  • Reply With Quote
      #7  
    Old 10-28-2003, 11:28 AM
    DelUS DelUS is offline
    Senior Member
     
    Join Date: Jan 2001
    Posts: 105
    Default RE: how can i Draw a Line on canvas?

    here is my sample code. But i am repainting the canvas by each mouse movement and i have to redraw my previous lines also. now i dont want to trigger canvas repaint procedure. pls checkout my code

    unit line;

    interface

    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    Buttons;

    type
    TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
    Y: Integer);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    StartX, EndX, StartY, EndY, DoAction, VCount : Word;
    Val : Array[0..100, 0..1] of Word;

    implementation

    {$R *.DFM}

    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
    begin
    if DoAction = 0 then
    begin
    DoAction := 1;
    if StartX = 0 then
    StartX := X;
    if StartY = 0 then
    StartY := Y;
    if VCount = 0 then
    begin
    Val[VCount, 0] := StartX;
    Val[VCount, 1] := StartY;
    inc(VCount);
    end;
    end
    else
    begin
    DoAction := 0;
    CanVas.LineTo(EndX, StartY);
    CanVas.LineTo(EndX, EndY);
    Val[VCount, 0] := EndX;
    Val[VCount, 1] := StartY;
    inc(VCount);
    Val[VCount, 0] := EndX;
    Val[VCount, 1] := EndY;
    inc(VCount);

    StartX := EndX;
    StartY := EndY;
    end;

    end;

    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
    Y: Integer);
    Var
    i : Word;
    begin
    EndX := X;
    EndY := Y;
    with Canvas Do
    begin
    if DoAction = 1 then
    begin
    repaint;
    if VCount > 1 then
    begin
    MoveTo(Val[0,0], Val[0,1]);
    for i := 1 to VCount-1 do
    LineTo(Val[i,0], Val[i,1]);
    end;

    MoveTo(StartX, StartY);
    LineTo(EndX, StartY);
    LineTo(EndX, EndY);
    end;
    end;

    end;



    end.
    Reply With Quote
      #8  
    Old 10-28-2003, 11:44 AM
    Eddy-B Eddy-B is offline
    Senior Member
     
    Join Date: Feb 2002
    Posts: 1,467
    Default RE: how can i Draw a Line on canvas?

    i'll review the code.
    1 question: do you really NEED this array[0..100] for storing the points for other reasons than just the drawing ?


    Eddy-B

    Please click Accept as answer if this helped
    Reply With Quote
      #9  
    Old 10-28-2003, 12:14 PM
    DelUS DelUS is offline
    Senior Member
     
    Join Date: Jan 2001
    Posts: 105
    Default RE: how can i Draw a Line on canvas?

    hi

    while triggering repaint event will loose all the previous drawing. i need this application to draw some electronic circuits. i am going to draw line by line. otherwise i dont need that array.

    thanks
    Jomon
    Reply With Quote
    Reply

    Thread Tools
    Display Modes

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is On

    Forum Jump


    All times are GMT. The time now is 08:23 AM.


    Powered by vBulletin® Version 3.8.7
    Copyright ©2000 - 2014, vBulletin Solutions, Inc.