Announcement

Collapse
No announcement yet.

im canvas zeichnen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • im canvas zeichnen

    hi leute...

    vor jahren habe ich mit dem c++ builder mal nen kleinen tool gebastelt...

    nun steh ich vor dem problem, dass ich nicht weiß, wie ich das entsprechend in delphi umwandeln kann...

    ich hoffe, es findet sich jemand, der das kann...

    hier der quelltext:
    Code:
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
      TPoint p1,p2,dp1,dp2,bs;
    
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
    {
            BorderStyle=bsNone;
            Position=poScreenCenter;
    
            p1.x=10;
            p1.y=100;
    
            p2.x=100;
            p2.y=10;
    
            dp1.x=5;
            dp1.y=10;
    
            dp2.x=10;
            dp2.y=5;
    
            bs.x=ClientWidth;
            bs.y=ClientHeight;
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
            Canvas->Pen->Color=RGB(rand()%255,rand()%255,rand()%255);
            Canvas->MoveTo(p1.x,p1.y);
            Canvas->LineTo(p2.x,p2.y);
    
            p1.x+=dp1.x;
            p1.y+=dp1.y;
            p2.x+=dp2.x;
            p2.y+=dp2.y;
    
            if(p1.x>bs.x || p1.x<10)
            {
              p2.x-=dp2.x;
              p2.y-=dp2.y;
              dp1.x=-dp1.x;
            }
            if(p1.y>bs.y || p1.y<10)
            {
              p2.x-=dp2.x;
              p2.y-=dp2.y;
              dp1.y=-dp1.y;
            }
            if(p2.x>bs.x || p2.x<10)
            {
              p1.x-=dp1.x;
              p1.y-=dp1.y;
              dp2.x=-dp2.x;
            }
            if(p2.y>bs.y || p2.y<10)
            {
              p1.x-=dp1.x;
              p1.y-=dp1.y;
              dp2.y=-dp2.y;
            }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift)
    {
            if(Key==VK_ESCAPE)
              Close();
    }
    //---------------------------------------------------------------------------
    ich danke schon jetzt für jegliche hilfe ;-)

    mfg
    ice

  • #2
    hi...

    probiers mal so...

    is zwar verbesserungsfähig...aber fürs erste entsprichts genau dem, was du in cpp hast...
    Code:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;
    
    type
      TForm1 = class(TForm)
        Timer1: TTimer;
        procedure FormKeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure Timer1Timer(Sender: TObject);
        procedure FormActivate(Sender: TObject);
      private
        { Private-Deklarationen }
      public
        { Public-Deklarationen }
      end;
    
    var
      Form1: TForm1;
      p1,p2,dp1,dp2,bs: TPoint;
    
    implementation
    
    {$R *.dfm}
    //------------------------------------------------------------------------------
    procedure TForm1.FormActivate(Sender: TObject);
    begin
            p1.x:=10;
            p1.y:=10;
    
            p2.x:=10;
            p2.y:=10;
    
            dp1.x:=5;
            dp1.y:=10;
    
            dp2.x:=10;
            dp2.y:=5;
    
            bs.x:=ClientWidth-10;
            bs.y:=ClientHeight-10;
    end;
    //------------------------------------------------------------------------------
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
            with Canvas do
            begin
              Pen.Color:=RGB(random(255),random(255),random(255));
              MoveTo(p1.x,p1.y);
              LineTo(p2.x,p2.y);
            end;
    
            p1.x:=p1.X+dp1.x;
            p1.y:=p1.y+dp1.y;
            p2.x:=p2.x+dp2.x;
            p2.y:=p2.y+dp2.y;
    
            if(p1.x>bs.x)or(p1.x<10)then
            begin
              p2.x:=p2.x-dp2.x;
              p2.y:=p2.y-dp2.y;
              dp1.x:=-dp1.x;
            end;
            if(p1.y>bs.y)or(p1.y<10)then
            begin
              p2.x:=p2.x-dp2.x;
              p2.y:=p2.y-dp2.y;
              dp1.y:=-dp1.y;
            end;
            if(p2.x>bs.x)or(p2.x<10)then
            begin
              p1.x:=p1.x-dp1.x;
              p1.y:=p1.y-dp1.y;
              dp2.x:=-dp2.X;
            end;
            if(p2.y>bs.y)or(p2.y<10)then
            begin
              p1.x:=p1.x-dp1.x;
              p1.y:=p1.y-dp1.y;
              dp2.y:=-dp2.y;
            end;
    end; 
    //------------------------------------------------------------------------------
    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    begin
            if(Key=VK_ESCAPE)then
              Close;
    end;
    //------------------------------------------------------------------------------
    end.

    Comment


    • #3
      danke für die schnelle antwort...

      und wie lässt sich das realisieren, dass nur eine linie angezeigt wird?

      mfg
      ice

      Comment


      • #4
        hi...

        setze am anfang im timer eine farbe deiner wahl fest und wiederhole dieses am ende des timers ;-)

        das wars schon...

        mfg
        twin4rider

        Comment

        Working...
        X