Link to home
Start Free TrialLog in
Avatar of Delene_Heukelman
Delene_Heukelman

asked on

drawing Graphics and mouse position simultaneously

I am using canvas.moveto and canvas.lineto to draw a stepped line that is moving from right to left, by drawing, changing the color to tha background and redrawing. I want to try to follow the line while it is being drawn, and moving, with the mouse, then compare the mouse path with the actual line. Is this possible? The stepped line coordinates are in an array (columns 1 to 4 for starting point and endpoint) and I want the mouse coordinates in the same array (columns 5 and 6).

Can onmousemove be active while already drawing without the mouse?

I need a very urgent answer!!!
Thanks
Delene
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Something like that ?

procedure Draw_Canvas_Line(Canvas: TCanvas; P1, P2: TPoint; Cl: TColor);
var
  P0:     TPoint;
  IX:     Integer;
  IY:     Integer;
  DX:     Integer;
  DY:     Integer;
  T:      Extended;
begin
  DX := P2.X - P1.X;
  DY := P2.Y - P1.Y;
  if (DX=0) then
  begin
    Canvas.Pen.Color := Cl;
    if (P1.Y<P2.Y) then
    begin
      for IY := P1.Y to P2.Y-1 do
      begin
        Canvas.MoveTo(P1.X, IY);
        Canvas.LineTo(P1.X, IY+1);
        Sleep(100);
        Application.ProcessMessages;
      end;
    end
    else
    begin
      for IY := P1.Y downto P2.Y-1 do
      begin
        Canvas.MoveTo(P1.X, IY);
        Canvas.LineTo(P1.X, IY-1);
        Sleep(100);
        Application.ProcessMessages;
      end;
    end
  end
  else
  if (DY=0) then
  begin
    Canvas.Pen.Color := Cl;
    if (P1.X<P2.X) then
    begin
      for IX := P1.X to P2.X-1 do
      begin
        Canvas.MoveTo(IX, P1.Y);
        Canvas.LineTo(IX+1, P1.Y);
        Sleep(100);
        Application.ProcessMessages;
      end;
    end
    else
    begin
      for IX := P1.X downto P2.X-1 do
      begin
        Canvas.MoveTo(IX, P1.Y);
        Canvas.LineTo(IX-1, P1.Y);
        Sleep(100);
        Application.ProcessMessages;
      end;
    end
  end
  else
  begin
    T := DY / DX;
    Canvas.Pen.Color := Cl;
    if (DX>=DY) then
    begin
      if (P1.X<P2.X) then
      begin
        for IX := P1.X to P2.X {-1} do
        begin
          P0.X := IX;
          P0.Y := P1.Y + Round((IX - P1.X) * T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          Sleep(100);
          Application.ProcessMessages;
        end;
      end
      else
      begin
        for IX := P1.X downto P2.X {-1} do
        begin
          P0.X := IX;
          P0.Y := P1.Y + Round((IX - P1.X) * T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          Sleep(100);
          Application.ProcessMessages;
        end;
      end;
    end
    else
    begin
      if (P1.Y<P2.Y) then
      begin
        for IY := P1.Y to P2.Y {-1} do
        begin
          P0.Y := IY;
          P0.X := P1.X + Round((IY - P1.Y) / T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          Sleep(100);
          Application.ProcessMessages;
        end;
      end
      else
      begin
        for IY := P1.Y downto P2.Y {-1} do
        begin
          P0.Y := IY;
          P0.X := P1.X + Round((IY - P1.Y) / T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          Sleep(100);
          Application.ProcessMessages;
        end;
      end;
    end;
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Draw_Canvas_Line(Img.Canvas, Point(8,8), Point(48,120), clBlack);
  Draw_Canvas_Line(Img.Canvas, Point(48,120), Point(112,96), clBlack);
  Draw_Canvas_Line(Img.Canvas, Point(112,96), Point(120,8), clBlack);
  Draw_Canvas_Line(Img.Canvas, Point(120,8), Point(8,8), clBlack);
end;
download a super example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21127913.zip

//........

procedure Draw_Canvas_Line(Canvas: TCanvas; P1, P2: TPoint; Cl: TColor; PT: TPoint; Wait: Integer);
var
  P0:     TPoint;
  IX:     Integer;
  IY:     Integer;
  DX:     Integer;
  DY:     Integer;
  T:      Extended;
begin
  DX := P2.X - P1.X;
  DY := P2.Y - P1.Y;
  if (DX=0) then
  begin
    Canvas.Pen.Color := Cl;
    if (P1.Y<P2.Y) then
    begin
      for IY := P1.Y to P2.Y-1 do
      begin
        Canvas.MoveTo(P1.X, IY);
        Canvas.LineTo(P1.X, IY+1);
        SetCursorPos(PT.X + P1.X, PT.Y + IY+1);
        Sleep(Wait);
        Application.ProcessMessages;
      end;
    end
    else
    begin
      for IY := P1.Y downto P2.Y+1 do
      begin
        Canvas.MoveTo(P1.X, IY);
        Canvas.LineTo(P1.X, IY-1);
        SetCursorPos(PT.X + P1.X, PT.Y + IY-1);
        Sleep(Wait);
        Application.ProcessMessages;
      end;
    end
  end
  else
  if (DY=0) then
  begin
    Canvas.Pen.Color := Cl;
    if (P1.X<P2.X) then
    begin
      for IX := P1.X to P2.X-1 do
      begin
        Canvas.MoveTo(IX, P1.Y);
        Canvas.LineTo(IX+1, P1.Y);
        SetCursorPos(PT.X + IX+1, PT.Y + P1.Y);
        Sleep(Wait);
        Application.ProcessMessages;
      end;
    end
    else
    begin
      for IX := P1.X downto P2.X+1 do
      begin
        Canvas.MoveTo(IX, P1.Y);
        Canvas.LineTo(IX-1, P1.Y);
        SetCursorPos(PT.X + IX-1, PT.Y + P1.Y);
        Sleep(Wait);
        Application.ProcessMessages;
      end;
    end
  end
  else
  begin
    T := DY / DX;
    Canvas.Pen.Color := Cl;
    if (DX>=DY) then
    begin
      if (P1.X<P2.X) then
      begin
        for IX := P1.X to P2.X {-1} do
        begin
          P0.X := IX;
          P0.Y := P1.Y + Round((IX - P1.X) * T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          SetCursorPos(PT.X + P0.X, PT.Y + P0.Y);
          Sleep(Wait);
          Application.ProcessMessages;
        end;
      end
      else
      begin
        for IX := P1.X downto P2.X {-1} do
        begin
          P0.X := IX;
          P0.Y := P1.Y + Round((IX - P1.X) * T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          SetCursorPos(PT.X + P0.X, PT.Y + P0.Y);
          Sleep(Wait);
          Application.ProcessMessages;
        end;
      end;
    end
    else
    begin
      if (P1.Y<P2.Y) then
      begin
        for IY := P1.Y to P2.Y {-1} do
        begin
          P0.Y := IY;
          P0.X := P1.X + Round((IY - P1.Y) / T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          SetCursorPos(PT.X + P0.X, PT.Y + P0.Y);
          Sleep(Wait);
          Application.ProcessMessages;
        end;
      end
      else
      begin
        for IY := P1.Y downto P2.Y {-1} do
        begin
          P0.Y := IY;
          P0.X := P1.X + Round((IY - P1.Y) / T);
          Canvas.MoveTo(P1.X, P1.Y);
          Canvas.LineTo(P0.X, P0.Y);
          SetCursorPos(PT.X + P0.X, PT.Y + P0.Y);
          Sleep(Wait);
          Application.ProcessMessages;
        end;
      end;
    end;
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  PT:     TPoint;
begin
  PT := Form1.Img.ClientToScreen(Point(0, 0));
  Draw_Canvas_Line(Img.Canvas, Point(8,8), Point(8,120), clBlack, PT, 48);
  Draw_Canvas_Line(Img.Canvas, Point(8,120), Point(112,96), clBlack, PT, 48);
  Draw_Canvas_Line(Img.Canvas, Point(112,96), Point(120,8), clBlack, PT, 128);
  Draw_Canvas_Line(Img.Canvas, Point(120,8), Point(8,8), clBlack, PT, 48);
end;
Avatar of Delene_Heukelman
Delene_Heukelman

ASKER

No, possibly I did not explain it very well, or I am too inexperienced. I need the mouse coordinates captured to an array while the drawing of the line takes place. The user sees the line that is moving left over the screen, and tries to follow it with the mouse. I want to be able to compare the actual line and the path the mouse followed, so both must be in an array.

I tried to interrupt the line drawing procedure after each section of the stepped line is drawn, with the timer enabled, to capture the onmousemove coordinates and return to the drawing procedure, but when I disable the timer only the last coordinates are available. It seems to destroy the values of the variables when terminated.

I used a global array to save the mouse coordinates in, and still when the timer is disabled, the array does not contain the mousevalues displayed from the array while the timer is enabled. How do I keep the array values after the timer is disabled?
SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot this




  private
    { Private declarations }
    DrawNum: Integer;
    aryPoints: Array of T2Points;