Autor Beitrag
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Sa 01.05.10 15:51 
Hallo,

also ich bin momentan dabei ein kleines Zeichenprogramm zu entwickeln.
Ich habe ähnlich wie Paint eine Toolbar in der ich Buttons für die jeweiligen Zeichenoperationen habe.
(siehe Anhang)

Nun habe ich für jede Zeichenoperation eine eigene Klasse geschrieben, wenn der Button geklickt wird und im folgenden dann der erste Klick auf der Paintbox getätigt wird, soll überprüft werden welche Zeichenoperation gewählt wurde.
Bisher habe ich es so gelöst:
Wenn der jeweilige Button gewählt wird, wird ein String mit dem Namen des Typen in eine globale Variable "typ" geschrieben.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.btnLineClick(Sender: TObject);
begin
  typ := 'Line';
end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if typ = 'Line' then
  begin
      MyLine := TLine.Create;
      MyLine.SetColor(ColorDialog1.Color);
      MyLine.SetPointP1(x,y);
  end;
end;


Ich finde das sehr unpraktisch, habt ihr einen Vorschlag für eine bessere Möglichkeit zu überprüfen welche Zeichenoperation gewählt wurde?

Gruß Dude566
Einloggen, um Attachments anzusehen!
_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 01.05.10 16:07 
Eine Möglichkeit wäre, bei Klick auf btnLine das OnMouseDown von Paintbox1 zu ändern. Ob das sinnvoller ist, weiß ich aber auch nicht. ;-)

_________________
We are, we were and will not be.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Sa 01.05.10 16:10 
Wie meinst du das Verändern? Nenne doch bitte mal ein Beispiel. ;)

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Sa 01.05.10 16:12 
Naja, dem MouseDown einfach eine neue Procedure zuweisen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.btnLineClick(Sender: TObject);
begin
  PaintBox1.OnMouseDown := LineMouseDown;
end;


lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 01.05.10 16:12 
Anhand des Bildes habe ich das Programm mal erweitert, damit klarer wird, was ich meine:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
procedure TForm1.btnLineClick(Sender: TObject);
begin
  PaintBox1.OnMouseDown := PaintBox1MouseDownLine;
end;

procedure TForm1.btnSquareClick(Sender: TObject);
begin
  PaintBox1.OnMouseDown := PaintBox1MouseDownSquare;
end;

procedure TForm1.PaintBox1MouseDownLine(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
      MyLine := TLine.Create;
      MyLine.SetColor(ColorDialog1.Color);
      MyLine.SetPointP1(x,y);
end;
procedure TForm1.PaintBox1MouseDownSquare(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
      MySquare := TSquare.Create;
      MySquare.SetColor(ColorDialog1.Color);
      MySquare.SetPointP1(x,y);
end;

_________________
We are, we were and will not be.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Sa 01.05.10 16:16 
Habt vielen Dank, werde ich gleich mal ausprobieren. :)

Edit: Ich bekomme eine ungenügende External Forward Deklaration angezeigt.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseDownLine(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUpLine(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

procedure TForm1.btnLineClick(Sender: TObject);
begin
  PaintBox1.OnMouseDown := PaintBox1MouseDownLine;
end;

procedure TForm1.PaintBox1MouseDownLine(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MyLine := TLine.Create;
  MyLine.SetColor(ColorDialog1.Color);
  MyLine.SetPointP1(x,y);
end;

procedure TForm1.PaintBox1MouseUpLine(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MyLine.SetPointP2(x,y);
  try
    MyLine.Draw(PaintBox1.Canvas);
  finally
    MyLine.Free;
  end;
end;


Liegt ja wohl daran, dass ich keine Implementation für PaintBox1MouseDownLine und PaintBox1MouseUpLine habe, was soll ich tun?
Denn wenn ich eine Leere Implementation der beiden Methoden mache funktioniert es auch nicht, also was sollte darin stehen?

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Sa 01.05.10 17:45 
wo hast du die proceduren denn definiert?

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: So 02.05.10 19:59 
Im Implementation Teil. Wo denn auch sonst, deklariert sind sie unter private der Form-Klasse.

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
MaPsTaR
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90
Erhaltene Danke: 4

Win XP
Delphi 7 Enterprise
BeitragVerfasst: So 02.05.10 20:22 
Bei mir kommt dieser Fehler, wenn ich eine Prozedur zwar im Interface-Teil, aber nicht im Implementation-Teil stehen hab.
-> Fehlerzeile im Compilerfenster anklicken und "F1".
Ich würde vorschlagen, du schaust dir mal die Compiler-Fehlermeldung an, um herauszufinden, welche Prozedure gemeint ist.
Nach dem Code, den du geschickt hast, könnten es
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
sein.

_________________
Liebe Kinder, es stimmt ... solnage auch nur der ertse und lezte Bchutsabe rihctig ist und alle andreen Bcuhsatben irgendwie vorahnden sind,
dann knan man es dennonch lesen, also macht nur weiter so, wir verstehen euch schon
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 03.05.10 15:06 
Ja es betrifft diese 2 Methoden, und selbst wenn ich beide in den Implementation Teil schreibe tritt der Fehler auf.
Normal sollte diese Meldung ja auftreten wenn ich sie nicht implementiert hätte, was ich aber getan habe, wenn auch ohne Inhalt.

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 03.05.10 15:16 
Und bei der Implementation hast du bei den "leeren" Methoden auch ein TForm1 davor stehen?

Und leere Formular-Methoden werden auch schonmal gerne von der IDE beim compilieren gelöscht. Ein kleiner Kommentar oder ein einzelnes Exit; in der Methode verhindern das. ;-)

_________________
We are, we were and will not be.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 03.05.10 15:23 
Also in den letzten Tagen bin ich doch sehr durcheinander, habe tatsächlich den Objektnamen (TForm1) vergessen. :oops:

Problem gelöst!

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Mo 03.05.10 16:54 
Problem gelöst, aber nicht unbedingt nach Lehrbuch...

Für die Werkzeugauswahl hat man doch normalerweise eine Menge SpeedButtons, die man zu einer Gruppe zusammenfasst. Dann kann immer nur einer Down sein...

Im Zeichen-Event muss man dann nur noch prüfen, bei welchem denn nun Down gesetzt ist.

Also im Prinzip das Selbe, wie du auch schon gemacht hattest, nur mit "Bordmitteln". Außerdem hat der User so direktes Feedback, was er denn eigentlich für ein Werkzeug nutzt gerade.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Di 04.05.10 17:58 
Wie kann ich denn Speedbuttons zu einer Gruppe auf meiner Toolbar einrichten? Ich habe gerade mal zum Test einfach 2 auf die Toolbar gezogen, aber wenn ich einen anklicke wird er nicht "eingedrückt".
Wie stelle ich das an? Das wäre natürlich eine bessere Lösung denn so sieht der Benutzer auch gleich welche Zeichenoperation aktiviert ist.

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Di 04.05.10 18:41 
Schau dir mal die Property GroupIndex an.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Di 04.05.10 18:50 
Okay das klappt, allerdings können auch mehrere ausgewählt werden (gedrückt).

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Di 04.05.10 18:58 
Wenn Buttons den selben GroupIndex haben, kann immer nur einer davon ausgewählt sein.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 05.05.10 12:36 
Wie wäre es denn mit OOP zu arbeiten für den Zweck?

Einfach eine Elternklasse und für jede Zeichenoperation eine abgeleitete Klasse. Dann muss beim Umschalten der Zeichenoperation nur (z.B. über eine Factory-Klasse) die passende Zeichenklasse erzeugt werden.
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mi 05.05.10 15:32 
Was du jetzt im genauen mit "über eine Factory-Klasse" meinst ist mir nicht klar, ich habe doch für jede Zeichenoperation eine Klasse.
Alle diese Klassen sind von einer Klasse TGrafix abgeleitet, in der die Punkte etc enthalten sind.

Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
type TJNGrafix = class(TObject)
  private
    FColor : TColor;
    FPen : byte;
    p1, p2 : TPoint;
  public
    procedure SetColor (AFarbe : TColor);
    procedure SetPointP1 (x, y : integer);
    procedure SetPointP2 (x, y : integer);
    procedure SetPen (AWidth : byte);
end;

type TLine = class(TJNGrafix)
  private
  public
    procedure Draw (ACanvas : TCanvas);
end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
procedure TLine.Draw(ACanvas: TCanvas);
begin
  ACanvas.Pen.Color := FColor;
  ACanvas.Pen.Width := FPen;
  ACanvas.MoveTo(p1.X, p1.Y);
  ACanvas.LineTo(p2.X, p2.Y);
end;

// --- Line Drawing Procedures -------------------------------------------------
procedure TForm1.PaintBox1MouseDownLine(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MyLine := TLine.Create;
  MyLine.SetColor(ColorDialog1.Color);
  MyLine.SetPen(12);
  MyLine.SetPointP1(X,Y);
end;

procedure TForm1.PaintBox1MouseUpLine(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MyLine.SetPointP2(X,Y);
  try
    MyLine.Draw(PaintBox1.Canvas);
  finally
    MyLine.Free;
  end;
end;


Bin noch nicht so ganz fit in Sachen OOP.

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 05.05.10 20:06 
Dann ist es doch sehr sehr einfach: ;-)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
TForm1 = class(TForm)
private
  FPaintOperation: TJNGrafix;
end;

// und dann bei Umschaltung der Operation:
FPaintOperation := TLinePainter.Create;
// oder:
FPaintOperation := TRectPainter.Create;

// und dann einfach (ohne Unterscheidung der Zeichenoperationen):
procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FPaintOperation.SetColor(ColorDialog1.Color);
  FPaintOperation.SetPen(12);
  FPaintOperation.SetPointP1(X,Y);
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FPaintOperation.SetPointP2(X,Y);
  FPaintOperation.Draw(PaintBox1.Canvas);
end;
So musst du das Objekt nicht ständig neu erzeugen (sondern nur bei Auswahl einer anderen Form).

Und so nebenbei: Benenne deine Komponenten ordentlich... ;-)