Autor Beitrag
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mi 05.07.06 17:22 
Hallo !

Wie komme ich denn an das OnDraw Event eines Labels heran ?
Wenn ich etwas auf den Canvas des Labels zeichne, dann verschwindet das, wenn ich mein Formular minimiere und wieder anzeigen lasse.
Ich brauche also irgendwo ein Ereignis, dass mir sagt, wann ich das Label neu überschrieben muss.

PS: Es muss leider ein Label sein, ich würde sonst schon meine eigene Kompnente basteln.

Danke.
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 05.07.06 18:27 
Hallo matze,

ich ahbe gerade mal den Aufbau/Funktionswiese eines Labels mir angeguckt (geht ja schnell, also kann ich so etwas mal nebenbei schnell machen als Aufwärmung :mrgreen: ). Und zwar bin ich darauf gestoßen, dass das Label von TCustomLabel abgeleitet wurde (beim Label werden nur noch die Eigenschaften veröffentlicht, ein größerer Unterschied besteht nicht). Also durch eine kleine Vererbung kannst du es machen, um an OnPaint oder OnDrawText zu kommen (letzteres wird durch ersteres Aufgerufen).

Ich habe dir mal eine kleine funktionsfähige Demo geschrieben. Ein Screeni davon stelle ich hier jetzt mal nicht rein, kann man ja ganz einfach selber ausführen ;):

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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  type
    TDrawLabel = class(TCustomLabel)
    protected
      procedure Paint; override;
  end;


var
  Form1: TForm1;
  TestLabel: TDrawLabel;

implementation

{$R *.dfm}

procedure TDrawLabel.Paint;
begin
  inherited;
  Canvas.Brush.Color:=clRed;
  Canvas.Pen.Color:=clBlue;
  Canvas.Rectangle(0, 5, 50, 10);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  TestLabel.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  TestLabel:=TDrawLabel.Create(Self);
  with TestLabel do
  begin
    Caption:='Hallo, ich bin ein Testbutton, der übermalt wird';
    Left:=10;
    Top:=5;
    Parent:=Form1;
    Visible:=true;
  end;
end;

end.


Heiko
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mi 05.07.06 18:57 
Hallo Heiko.

Das ist ja genau das was ich brauche. Super.
Nur leider muss ich dazu alle meine Labels auf dem Formular in neue Custom Labels umwandeln. Kann man das auch irgendwie zur Laufzeit machen, oder so, dass es ein bisschen komforabel geht. Ich möchste eigenlich nicht wirklich eine neue Komponente installieren müssen...
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 05.07.06 19:06 
Du kannst auch Labels nehmen, aber ich glaube, ohne Vererbung wirst du das nicht hinbekommen. Allerdings kannst du es ditr leicht machen, in dem du das so wie du es haben willst, als Komponente in die IDE einfügst und dann vorm öffnen deines Projektes mit dem NotePad einfach alle TLabels gegegen z.B. MyOwnDrawLabel tauschst. Somit hast du nur paar Sekunden zu tun.

Falls du etwas findest um Vererbung zur Laufzeit oder so etwas zu machen, oder Messages abfangen vom Label ohne Vererbung, sags mir mal. Das hätte ich schon seeehr oft gebrauchen können ;).

Heiko
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Do 06.07.06 16:59 
jo danke für deine tipps. mal sehen, wie ich das machen werde....
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 06.07.06 17:40 
Aos, vergiss nicht: du musst das mitm Notepad in deiner Klasse (Standard: TForm1) und in der DFM machen. Ansonsten könnte es Fehlermeldungen haggeln beim compilieren ;).