Autor Beitrag
trm
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 491
Erhaltene Danke: 19

Windows 7x64
Delphi 7
BeitragVerfasst: Mo 26.04.10 16:06 
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:
      { Zeilenumbruch in einer Zelle erlauben }
              if CheckBox_Setup_AutosizeCells_MultiLine.Checked then
              begin
                DrawText(
                  TStringgrid(Sender).Canvas.Handle,
                  PCHar(Cells[Acol, ARow]),
                  Length(Cells[Acol, ARow]),
                  Rect,
                  DT_WordBreak or DT_NoClip or DT_NOPREFIX or DT_CalcRect 
                  );
                if Rect.Bottom - Rect.Top > RowHeights[ARow] then
                  RowHeights[ARow] := Rect.Bottom - Rect.Top;
                DrawText(
                  TStringgrid(Sender).Canvas.Handle,
                  PCHar(Cells[Acol, ARow]),
                  Length(Cells[Acol, ARow]),
                  Rect,
                  DT_WordBreak or DT_NoClip or DT_NOPREFIX or DT_CENTER
                  );
              end;
            end;
      { Zeilenumbruch in einer Zelle erlauben }


Das ist ein Ausschnitt meines Codes, den ich nutzte, um mehrzeiligen Text in einer Zelle im OnDrawCell eines TStringGrids auszugeben.
Leider wird aber hierbei der Text an die obere linke Ecke geheftet und nur im Block selbst mittig zentriert.
Wie ist es möglich, dass der Textblock nun in der Zelle zentriert ausgegeben wird?

Gruß
~Mathias
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: Mo 26.04.10 16:51 
Mit Canvas.TextWidth\TextHeight die Breite ausrechnen und dann X := (Width - TextWidth) div 2; nehmen. Für Vertikal analog.

_________________
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.
trm Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 491
Erhaltene Danke: 19

Windows 7x64
Delphi 7
BeitragVerfasst: Mo 26.04.10 18:35 
Huhu Benny,

sorry, aber Du hast wohl leider übersehen, dass es ein DrawText und kein TextOut ist.
Außerdem wird ein eventueller Zeilenumbruch mit dem ersten DrawText ermittelt.
Daher kann ich nicht auf TextWidth zurückgreifen.
In Rect aus dem ersten DrawText ist die Höhe des Texblockes gespeichert. Soweit, so gut.
Nun könnte ich ja Rect.Left sowie Rect.Top manuell vor dem zweiten DrawText festlegen.
Das habe ich schon versucht, bin jedoch gescheitert.

ausblenden Delphi-Quelltext
1:
2:
                  Rect.Left :=Rect.Left+((Rect.Right -Rect.Left ) div 2div 2;
                  Rect.Top:= Rect.Top+  ((Rect.Bottom -Rect.Top ) div 2div 2;
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: Mo 26.04.10 19:39 
Breite von Container minus Breite vom Text. Und das durch 2.

_________________
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.
trm Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 491
Erhaltene Danke: 19

Windows 7x64
Delphi 7
BeitragVerfasst: Mo 26.04.10 20:09 
Denkfehler meinerseits, Danke, Benny :)

Hier der fertige Code.


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:
      { Zeilenumbruch in einer Zelle erlauben }
              if CheckBox_Setup_AutosizeCells_MultiLine.Checked then
              begin
                DrawText(
                  TStringgrid(Sender).Canvas.Handle,
                  PCHar(Cells[Acol, ARow]),
                  Length(Cells[Acol, ARow]),
                  Rect,
                  DT_WordBreak
                  or DT_NoClip
                  or DT_NOPREFIX
                  or DT_CalcRect
                  );

                if Rect.Bottom - Rect.Top > RowHeights[ARow] then
                  RowHeights[ARow] := Rect.Bottom - Rect.Top;

                Rect.Left := Rect.Left + (((CellRect(Acol, ARow).Right - CellRect(Acol, ARow).Left) - (Rect.Right - Rect.Left)) div 2);
                Rect.Top := Rect.Top + (((CellRect(Acol, ARow).Bottom - CellRect(Acol, ARow).Top) - (Rect.Bottom - Rect.Top)) div 2);

                DrawText(
                  TStringgrid(Sender).Canvas.Handle,
                  PCHar(Cells[Acol, ARow]),
                  Length(Cells[Acol, ARow]),
                  Rect,
                  DT_WordBreak
                  or DT_NoClip
                  or DT_NOPREFIX
                  or DT_CENTER
//                  or DT_VCENTER
//                  or DT_INTERNAL
//                  or DT_HIDEPREFIX
                  );
              end;
            end;
      { Zeilenumbruch in einer Zelle erlauben }


Viele Grüße
~Mathias