Autor Beitrag
Stread
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 188

Win 7
Delphi XE
BeitragVerfasst: Fr 14.05.10 15:08 
Hi
Ich erzeuge per ButtonClick 4 Shapes welches sich auf den Rand eines Panels legen.
Das habe ich hin bekommen.

Wenn ich nun einen anderen Button klicke sollen sie sich als kleines Quadrat in der Mitte anordnen, also ihre Position und Größe ändern. Ich muss ja nicht neue Shapes erstellen sondern kann die alten ja weiterverwenden ?
Sonst soll alles gleich bleiben, Farbe und OnMouseEnter Ereignis.

Wie bekomme ich das hin ohne nochmals alle Shapes neu zu machen?


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure MyShape1Mouse (Sender: TObject);
  procedure MyShape2Mouse (Sender: TObject);
  procedure MyShape3Mouse (Sender: TObject);
  procedure MyShape4Mouse (Sender: TObject);

    { Public-Deklarationen }
  end;

var
  Form12: TForm12;

  MyShape1,MyShape2,MyShape3,MyShape4:TShape;

implementation



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:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
procedure TForm12.BTCreateClick(Sender: TObject);
begin
MyShape1:=TShape.Create(self);
MyShape1.Parent:=Panel1;
MyShape1.Height:=30;
MyShape1.Width:=700;
MyShape1.Brush.Color:=clHotLight;
MyShape1.Show;
MyShape1.OnMouseEnter:=MyShape1Mouse;


MyShape2:=TShape.Create(self);
MyShape2.Parent:=Panel1;
MyShape2.Height:=30;
MyShape2.Width:=700;
MyShape2.Top:=420;
MyShape2.Brush.Color:=clHotLight;
MyShape2.Show;
MyShape2.OnMouseEnter:=MyShape2Mouse;

MyShape3:=TShape.Create(self);
MyShape3.Parent:=Panel1;
MyShape3.Height:=390;
MyShape3.Width:=30;
MyShape3.Top:=30;
MyShape3.Brush.Color:=clHotLight;
MyShape3.Show;
MyShape3.OnMouseEnter:=MyShape3Mouse;

MyShape4:=TShape.Create(self);
MyShape4.Parent:=Panel1;
MyShape4.Height:=390;
MyShape4.Width:=30;
MyShape4.Left:=670;
MyShape4.Top:=30;
MyShape4.Brush.Color:=clHotLight;
MyShape4.Show;
MyShape4.OnMouseEnter:=MyShape4Mouse;


end;

procedure TForm12.MyShape1Mouse(Sender: TObject);
begin
  if Sender=MyShape1 then
  begin
   Application.MessageBox('Blau 1', 'Verloren', MB_ICONSTOP);
  end;
end;


procedure TForm12.MyShape2Mouse(Sender: TObject);
begin
  if Sender=MyShape2 then
  begin
   Application.MessageBox('Blau 2', 'Verloren', MB_ICONSTOP);
  end;
end;

procedure TForm12.MyShape3Mouse(Sender: TObject);
begin
  if Sender=MyShape3 then
  begin
   Application.MessageBox('Blau 3', 'Verloren', MB_ICONSTOP);
  end;
end;

procedure TForm12.MyShape4Mouse(Sender: TObject);
begin
  if Sender=MyShape4 then
  begin
   Application.MessageBox('Blau 4', 'Verloren', MB_ICONSTOP);
  end;
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm12.BTDeleteClick(Sender: TObject);

begin

MyShape1.Visible:=FALSE;
MyShape2.Visible:=FALSE;
MyShape3.Visible:=FALSE;
MyShape4.Visible:=FALSE;

end;



Wahrscheinlich habe ich eh zu viel Code :?
Gehts auch einfacher und leicht verständlich?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 14.05.10 15:26 
Lagere das eigentliche Erstellen der Shapes in FormCreate aus.

Danach kannst du in den ButtonClick-Events dich allein auf die Positionierung beschränken.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Webo
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 577
Erhaltene Danke: 14

Win 7, Debian
C# (Visual Studio 2013), PHP, C, C++ (Eclipse, KDevelop)
BeitragVerfasst: Fr 14.05.10 15:33 
user profile iconStread hat folgendes geschrieben Zum zitierten Posting springen:
Wenn ich nun einen anderen Button klicke sollen sie sich als kleines Quadrat in der Mitte anordnen, also ihre Position und Größe ändern. Ich muss ja nicht neue Shapes erstellen sondern kann die alten ja weiterverwenden ? [...] Wie bekomme ich das hin ohne nochmals alle Shapes neu zu machen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
MyShape4.Height:=390;
MyShape4.Width:=30;
MyShape4.Left:=670;
MyShape4.Top:=30;


Du gibst dir die Antwort doch schon quasi selbst. Natürlich musst du keine neuen nehmen, die "alten" kannst du ja einfach in der Größe und Lage verändern.
Zu deiner Schlussfrage: Lagere z.B. die Erzeugung und die Größenzuordnung in eigene Prozeduren aus, so kann man auch bei einem weiteren Button die Position der Shapes viel leichter ändern.

_________________
Man kann nur das aus dem Ärmel schütteln, was man auch vorher reingesteckt hat.
Stread Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 188

Win 7
Delphi XE
BeitragVerfasst: Sa 15.05.10 21:52 
Danke. Hat funktioniert.

Da mein Shape nur 10 Breit ist, ist es natürlich sehr schmal. Wenn ich mit der Maus das Shape berühre (das auf einem Panel liegt) soll eine Aktion ausgeführt werden.

Problem ist wenn die Maus schnell drüber geht, passiert nichts.
Kann man das so hinbekommen dass man schaut wo sich die Maus befindet und wenn sie über einer bestimmten Waagrechten und/ oder senkrechten Koordinate ist, dass dann die Aktion ausgelöst wird?

Danke


Edit:

ich habe es nun so gemacht. Allerdings ist das Problem dass es bei einer schnellen Mausbewegung nicht ausgelöst wird immer noch da.

Außerdem funktioniert diese Methode bei großen Shapes nicht. Die Koordinaten werden über den Shapes nicht aktualisiert.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
PanelMousePosition: TPoint;

procedure TForm12.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 PanelMousePosition:=Panel1.ScreenToClient(Mouse.CursorPos);
 Edit4.Text:=IntToStr(PanelMousePosition.X);
 Edit5.Text:=IntToStr(PanelMousePosition.Y);

 if PanelMousePosition.X <= 10 then
 begin
   EditVerloren.Text:='Links kleiner 10';
 end;

 if PanelMousePosition.Y <= 10 then
 begin
   Editverloren.Text:='Oben kleiner 10';
 end;

end;