Autor Beitrag
JustusJonas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Fr 14.10.05 11:15 
Hallo Leute,

ich wollte einen Zeilenumbruch in meinem StringGrid. Das habe ich folgendermaßen gelöst:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TfrmZeiterfassung.sgStatistikDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
Var SText: String;   
begin
if gdfixed in state then sgStatistik.canvas.brush.Color := sgStatistik.FixedColor
                       else sgStatistik.canvas.brush.Color := sgStatistik.Color;
   sgStatistik.canvas.FillRect(rect);  

  SText := sgStatistik.Cells[aCol,aRow];
  DrawText (
   sgStatistik.Canvas.Handle,
   PCHar (SText),   
   Length (SText),
   Rect,   
   DT_WordBreak or DT_Center
   );   
end;


Ich möchte aber gerne jede Zelle oder Reihe oder Zeile einzeln ansprechen können.
Wie funktioniert das?

Greetz
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Fr 14.10.05 11:26 
user profile iconJustusJonas hat folgendes geschrieben:
Ich möchte aber gerne jede Zelle oder Reihe oder Zeile einzeln ansprechen können.

Sorry, keine Ahnung was du genau meinst.

Lg Martin
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Fr 14.10.05 11:39 
user profile iconMartin1966 hat folgendes geschrieben:
user profile iconJustusJonas hat folgendes geschrieben:
Ich möchte aber gerne jede Zelle oder Reihe oder Zeile einzeln ansprechen können.

Sorry, keine Ahnung was du genau meinst.

Lg Martin


Ich möchte z. B. die erste Zeile (in der die Überschrift steht) fettgedruckt machen.
Dann möachte ich die erste Spalte linksbündig und alle anderen Spalten zentriert haben.
Außerdem sollte jede zweite Zeile eine andere Farbe haben.

Greetz
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Fr 14.10.05 11:59 
rechtsbündig :

ausblenden Delphi-Quelltext
1:
  DrawText(sg.Canvas.Handle, PChar(st), StrLen(PChar(st)),Rect,DT_RIGHT);					


farbig / fette Überschrift :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure Tfrm.sgDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  inherited;
  if (ARow >= sg.FixedRows) and (ACol >= sg.FixedCols) then begin
    canvas.Font.Color:=clwhite;
    canvas.brush.Color := clBlue;
  end
  else
    canvas.font.Style := fsBold;
  canvas.TextOut(rect.Left,rect.bottom-canvas.Textheight('TEST')-1,'TEST');
end;

_________________
Gruß
Hansa
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 14.10.05 12:05 
Hallo,

das geht so in etwa in Deine Richtung:
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:
procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
                                     Rect: TRect; State: TGridDrawState);
const
farbe1:TColor=clwhite;
farbe2:TColor=$00B1EBFC;
farbe3:TColor=clBtnFace;
farbe4:TColor=$00E2F8FE;

  procedure zelle(f:TColor;ausr:uint);
  begin
    with (Sender as TStringGrid) do
    begin
      canvas.brush.color:=f;
      canvas.fillrect(rect);
      inc(rect.left,2);
      dec(rect.right,2);
      drawtext(canvas.handle,pchar(cells[acol,arow]),length(cells[acol,arow]),
               rect,DT_SINGLELINE or ausr);
    end;
  end;

begin
with (Sender as TStringGrid) do
  begin
  if (arow < Fixedrows) or (acol < Fixedcols) then //feste Zeile,Spalten
    zelle(farbe3,DT_CENTER or DT_TOP) //grau
    else//restliche Zellen
      begin
      if copy(cells[acol,0],1,1) = ' ' then //wenn Zahlenspalte
        if arow mod 2 =0 then //jede zweite Zeile
          if state = [gdSelected] then
            zelle(farbe2,DT_RIGHT or DT_VCENTER)//dunkeler Gelb
          else
            zelle(farbe4,DT_RIGHT or DT_VCENTER)//hellgelb
          else
          if state = [gdSelected] then
            zelle(farbe2,DT_RIGHT or DT_VCENTER)//dunkeler Gelb
            else
              zelle(farbe1,DT_RIGHT or DT_VCENTER)//weiß

        else
          if arow mod 2 =0 then
          if state = [gdSelected] then
            zelle(farbe2,DT_LEFT or DT_VCENTER)//dunkeler Gelb
          else
            zelle(farbe4,DT_LEFT or DT_VCENTER)//hellgelb
            else
            if state = [gdSelected] then
              zelle(farbe2,DT_LEFT or DT_VCENTER)//dunkeler Gelb
              else
                zelle(farbe1,DT_LEFT or DT_VCENTER)//weiß
      end;//Ende: if (arow < Fixedrows) or (acol < Fixedcols) then
  end;//Ende: with (Sender as TStringGrid) do
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Mo 17.10.05 06:12 
user profile iconLannes hat folgendes geschrieben:
Hallo,

das geht so in etwa in Deine Richtung:
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:
procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
                                     Rect: TRect; State: TGridDrawState);
const
farbe1:TColor=clwhite;
farbe2:TColor=$00B1EBFC;
farbe3:TColor=clBtnFace;
farbe4:TColor=$00E2F8FE;

  procedure zelle(f:TColor;ausr:uint);
  begin
    with (Sender as TStringGrid) do
    begin
      canvas.brush.color:=f;
      canvas.fillrect(rect);
      inc(rect.left,2);
      dec(rect.right,2);
      drawtext(canvas.handle,pchar(cells[acol,arow]),length(cells[acol,arow]),
               rect,DT_SINGLELINE or ausr);
    end;
  end;

begin
with (Sender as TStringGrid) do
  begin
  if (arow < Fixedrows) or (acol < Fixedcols) then //feste Zeile,Spalten
    zelle(farbe3,DT_CENTER or DT_TOP) //grau
    else//restliche Zellen
      begin
      if copy(cells[acol,0],1,1) = ' ' then //wenn Zahlenspalte
        if arow mod 2 =0 then //jede zweite Zeile
          if state = [gdSelected] then
            zelle(farbe2,DT_RIGHT or DT_VCENTER)//dunkeler Gelb
          else
            zelle(farbe4,DT_RIGHT or DT_VCENTER)//hellgelb
          else
          if state = [gdSelected] then
            zelle(farbe2,DT_RIGHT or DT_VCENTER)//dunkeler Gelb
            else
              zelle(farbe1,DT_RIGHT or DT_VCENTER)//weiß

        else
          if arow mod 2 =0 then
          if state = [gdSelected] then
            zelle(farbe2,DT_LEFT or DT_VCENTER)//dunkeler Gelb
          else
            zelle(farbe4,DT_LEFT or DT_VCENTER)//hellgelb
            else
            if state = [gdSelected] then
              zelle(farbe2,DT_LEFT or DT_VCENTER)//dunkeler Gelb
              else
                zelle(farbe1,DT_LEFT or DT_VCENTER)//weiß
      end;//Ende: if (arow < Fixedrows) or (acol < Fixedcols) then
  end;//Ende: with (Sender as TStringGrid) do
end;


Sorry, das raffe ich nicht ganz.

Muss diese Funktion ins OnDrawCell?
Wie rufe ich diese Funktion auf?

Greetz
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 17.10.05 07:32 
Hallo,

dazu sind drei Schritte erforderlich:

1. Die Procedur nach implementation in die Unit kopieren
2. Den Procedurkopf ohne TForm1. vor private setzen
3. Dann im Objektinspektor neben OnDrawCell die Procedur GridDrawCell auswählen bzw. eintragen.

Die Procedur kannst Du auch mehreren StringGrids zuweisen.

In den FixedCols und -Rows werden die Einträge zentriert ausgerichtet.
Die Steuerung der Spalten mit Ausrichtung rechts habe ich
über ein führendes Leerzeichen in den Spaltenbeschriftungen realisiert.

Wenn Du nachfolgenden Code zum Testen nimmst, wird es Dir klar werden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);
begin
  StringGrid1.Cells[0,0] := 'Nr.';
  StringGrid1.Cells[1,0] := 'Artikel';
  StringGrid1.Cells[2,0] := ' Anzahl';//Leerzeichen wegen Ausrichtung
  StringGrid1.Cells[3,0] := ' à';
  StringGrid1.Cells[4,0] := ' Summe';
  StringGrid1.Cells[0,1] := '1';
  StringGrid1.Cells[1,1] := 'Eier';
  StringGrid1.Cells[2,1] := '120';
  StringGrid1.Cells[3,1] := '0,20 €';
  StringGrid1.Cells[4,1] := '24,00 €';
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Mo 17.10.05 09:47 
user profile iconJustusJonas hat folgendes geschrieben:
Muss diese Funktion ins OnDrawCell?

Ja.

user profile iconJustusJonas hat folgendes geschrieben:
Wie rufe ich diese Funktion auf?

Die Funktion brauchst du gar nicht aufrufen. Das ist meistens so bei Ereignissen von Komponenten. ;-)

Die Komponente ruft selbst die Ereignisse auf. In diesem Fall wenn eine Zelle neu gezeichnet werden muss.

Lg Martin
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Do 20.10.05 10:41 
Super, vielen Dank!

Das hat jetzt geklappt. Nur noch eine Frage:
Wie mache ich jetzt die oberste Zeile (also meine FixedRow) mit Zeilenumbruch?
Das bekomme ich irgendwie nicht hin.

Greetz
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Do 20.10.05 15:25 
Hallo,

dann musst Du nochmal eine Unterscheidung zwischen FixedRows und FixedCols einbauen.
Ich habe mal die Änderungen vorgenommen und markiert:
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:
procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
                                     Rect: TRect; State: TGridDrawState);
const
farbe1:TColor=clwhite;
farbe2:TColor=$00B1EBFC;
farbe3:TColor=clBtnFace;
farbe4:TColor=$00E2F8FE;


  procedure zelle(f:TColor;ausr:uint);
  begin
    with (Sender as TStringGrid) do
    begin
      canvas.brush.color:=f;
      canvas.fillrect(rect);
      inc(rect.left,2);
      dec(rect.right,2);
      drawtext(canvas.handle,pchar(cells[acol,arow]),length(cells[acol,arow]),  
               rect,{DT_SingleLine or }ausr);
    end;
  end;


begin  
with (Sender as TStringGrid) do  
  begin
  if (arow < Fixedrows) or (acol < Fixedcols) then //feste Zeile,Spalten
    begin
    if arow < Fixedrows then
      zelle(farbe3,DT_WORDBREAK or DT_CENTER or DT_TOP) //grau
      else
        zelle(farbe3,DT_SINGLELINE or DT_RIGHT or DT_VCENTER) //grau
    end

    else//restliche Zellen
      begin
      if copy(cells[acol,0],1,1) = ' ' then //wenn Zahlenspalte
        if arow mod 2 =0 then //jede zweite Zeile  
          if state = [gdSelected] then
            zelle(farbe2,DT_SINGLELINE or DT_RIGHT or DT_VCENTER)//dunkeler Gelb
          else  
            zelle(farbe4,DT_SINGLELINE or DT_RIGHT or DT_VCENTER)//hellgelb
          else  
          if state = [gdSelected] then  
            zelle(farbe2,DT_SINGLELINE or DT_RIGHT or DT_VCENTER)//dunkeler Gelb
            else  
              zelle(farbe1,DT_SINGLELINE or DT_RIGHT or DT_VCENTER)//weiß

        else  
          if arow mod 2 =0 then  
          if state = [gdSelected] then
            zelle(farbe2,DT_SINGLELINE or DT_LEFT or DT_VCENTER)//dunkeler Gelb
          else
            zelle(farbe4,DT_SINGLELINE or DT_LEFT or DT_VCENTER)//hellgelb
            else  
            if state = [gdSelected] then
              zelle(farbe2,DT_SINGLELINE or DT_LEFT or DT_VCENTER)//dunkeler Gelb
              else
                zelle(farbe1,DT_SINGLELINE or DT_LEFT or DT_VCENTER)//weiß
      end;//Ende: if (arow < Fixedrows) or (acol < Fixedcols) then
  end;//Ende: with (Sender as TStringGrid) do  
end;

:gruebel: wenn ich mir das jetzt so anschaue, man könnte es bestimmt besser/anders strukturieren.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Fr 21.10.05 08:52 
Hab die Änderungen mal in meinen Quelltext eingefügt.
Es passiert überhaupt nix! :(

Greetz
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Fr 21.10.05 08:57 
Mein Source wurde wohl übersehen. Der geht zu 100 % und ist sehr kurz. Da läßt sich ein Schreibfehler viel besser lokalisieren.

_________________
Gruß
Hansa
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Fr 21.10.05 09:18 
user profile iconhansa hat folgendes geschrieben:
Mein Source wurde wohl übersehen. Der geht zu 100 % und ist sehr kurz. Da läßt sich ein Schreibfehler viel besser lokalisieren.


Nein, habe ich nicht übersehen. Aber

1. gab es beim compilieren einen Fehler und
2. hilft mir das auch nicht beim Zeilenumbruch in der Zelle weiter, oder?

Greetz
JustusJonas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Fr 21.10.05 09:22 
OK hab's hinbekommen.

Man muss wohl einfach DT_SINGLELINE durch DT_WORDBREAK ersetzen.

Greetz