Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   onDrawCell überschreibt Caption vom Formular (https://www.delphipraxis.net/166449-ondrawcell-ueberschreibt-caption-vom-formular.html)

tofse 14. Feb 2012 10:17

onDrawCell überschreibt Caption vom Formular
 
Hallo,

ich steh total auf'm Schlauch. Was stimmt an diesem Code unten nicht?
Wenn ich das im onDrawCell eines TStringGrid's drinnen lasse, dann wird bei einem onSelectCell der Inhalt der Zelle in die Titelleiste des Formulars geschrieben.



Code:
procedure TForm.DrawTerminart(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var outRect:TRect;
begin
  if (ACol > 1) And (ARow >= 1) then
  begin
    with sender as TStringGrid do
    begin
      Text := Cells[ACol, ARow];
      outRect:=Rect;
      Canvas.Brush.Color:=clWhite;
      Canvas.Fillrect(Rect);
      Canvas.Font.Name:='Wingdings 2';
      Canvas.Font.Size:=14;
      if ACol=9 then
        Canvas.Font.Color:=clGray
      else
        Canvas.Font.Color:=clBlack;

      outRect.Top:=outRect.Top + 2;
      DrawText(Canvas.Handle,
           PChar(Text),
           length(Text),
           outRect,
           DT_Center
           or
           DT_SingleLine);
    end;
  end;
end;
Ich habe das schon zig Mal verwendet. Für mich der einzige Unterschied ist, dass ich das StringGrid erst zur Laufzeit erzeuge, aber auch hier fällt mir nichts auf... :(
Code:
  G:=TStringGrid.Create(TabControl);
  G.Parent:=TabControl;
  G.FixedRows:=1;
  G.FixedCols:=0;
  G.ColCount:=11;
  G.RowCount:=2;
  G.Top:=25;
  G.Left:=2;
  G.Cells[0,0]:='Bezeichnung';
  G.Cells[1,0]:='Dauer';
  G.Cells[2,0]:='Mo';
  G.Cells[3,0]:='Di';
  G.Cells[4,0]:='Mi';
  G.Cells[5,0]:='Do';
  G.Cells[6,0]:='Fr';
  G.Cells[7,0]:='Sa';
  G.Cells[8,0]:='So';
  G.ScrollBars:=ssVertical;
  G.Width:=TabControl.Width - 7;
  G.Height:=TabControl.Height - 25;
  G.ColWidths[0]:=310;
  G.ColWidths[1]:=60;
  G.ColWidths[2]:=20;
  G.ColWidths[3]:=20;
  G.ColWidths[4]:=20;
  G.ColWidths[5]:=20;
  G.ColWidths[6]:=20;
  G.ColWidths[7]:=20;
  G.ColWidths[8]:=20;
  G.ColWidths[9]:=20;
  G.ColWidths[10]:=0;
  G.OnDrawCell:=DrawTerminart;
  G.OnSelectCell:=SelectTerminart;
  G.OnKeyDown:=KeyDownTerminart;
  G.Options:=[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goTabs];
Grüße
Christof

DeddyH 14. Feb 2012 10:23

AW: onDrawCell überschreibt Caption vom Formular
 
Passiert das auch, wenn Du das with weglässt und den Rect-Parameter in z.B. ARect umbenennst?

tofse 14. Feb 2012 10:29

AW: onDrawCell überschreibt Caption vom Formular
 
Zitat:

Zitat von DeddyH (Beitrag 1150982)
Passiert das auch, wenn Du das with weglässt und den Rect-Parameter in z.B. ARect umbenennst?

ja, leider :-(

mkinzler 14. Feb 2012 10:36

AW: onDrawCell überschreibt Caption vom Formular
 
Dann versuch es mal ohne with z.B. in dem du eine gecastete loakle Variable verwendest

DeddyH 14. Feb 2012 10:38

AW: onDrawCell überschreibt Caption vom Formular
 
Sag ich doch :)

tofse 14. Feb 2012 10:41

AW: onDrawCell überschreibt Caption vom Formular
 
Zitat:

Zitat von mkinzler (Beitrag 1150991)
Dann versuch es mal ohne with z.B. in dem du eine gecastete loakle Variable verwendest

ne, klappt nicht

DeddyH 14. Feb 2012 10:53

AW: onDrawCell überschreibt Caption vom Formular
 
Was ist denn Text überhaupt? Fehlt da evtl. eine lokale Variable?

[edit]So sieht das bei mir aus, als sei es so gewollt:
Delphi-Quellcode:
procedure TForm.DrawTerminart(Sender: TObject; ACol, ARow: Integer;
  ARect: TRect; State: TGridDrawState);
var
  outRect: TRect;
  G: TStringGrid;
  Text: string; //<-- das machen wir mal lieber lokal
begin
  //und das with kommt auch sicherheitshalber weg
  G := Sender as TStringGrid;
  if (ACol > 1) And (ARow >= 1) then
    begin
      Text := G.Cells[ACol, ARow];
      outRect := ARect;
      G.Canvas.Brush.Color := clWhite;
      G.Canvas.Fillrect(ARect);
      G.Canvas.Font.Name := 'Wingdings 2';
      G.Canvas.Font.Size := 14;
      if ACol = 9 then
        G.Canvas.Font.Color := clGray
      else
        G.Canvas.Font.Color := clBlack;

      outRect.Top := outRect.Top + 2;
      DrawText(G.Canvas.Handle, PChar(Text), length(Text), outRect,
        DT_Center or DT_SingleLine);
    end;
end;
[/edit]

tofse 14. Feb 2012 11:26

AW: onDrawCell überschreibt Caption vom Formular
 
Zitat:

Zitat von DeddyH (Beitrag 1150996)
Was ist denn Text überhaupt? Fehlt da evtl. eine lokale Variable?

:thumb:

Achje, danke fürs mitschauen, also manchmal....:-D

ConnorMcLeod 14. Feb 2012 12:07

AW: onDrawCell überschreibt Caption vom Formular
 
Wenn Du durchsteppst, dann merkst Du, dass <Text> bei
Delphi-Quellcode:
Text := G.Cells[ACol, ARow];
auf die Form zugreift.


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:20 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz