Autor Beitrag
AlexII
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 180



BeitragVerfasst: Sa 17.05.08 11:57 
Bei mir wird die entsprechende Zelle markiert, wie kann ich sie aber färben, denn ich bin ja nicht in dem Ereignis OnDrawCell? Wie übergebe ich die Variablen i und j nach OnDrawCell um dort, wie schon gesagt, die Zelle zu färben?


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
if Copy((Cells[i,j]),1,u)=s1 then
   begin
   Edit4.Color:=clWindow;
   StringGrid1.Selection:=TGridRect(Rect(i,j,i,j));
   Exit;
   end;
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Sa 17.05.08 12:12 
Diese Methode wird für jede Zelle aufgerufen, du musst also dort den aktuellen Index abfragen

_________________
Markus Kinzler.
Jerk
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 251

Vista Ultimate, Ubuntu
Turbo Delphi 2006
BeitragVerfasst: So 18.05.08 10:02 
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:
  MarkX :  Integer;
  MarkY :  Integer;

...
procedure TForm1.FormCreate(Sender: TObject);
begin
 MarkX:= -1;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  var s : string;
begin
if MarkX > -1 then
 with Sender As TStringGrid do
 begin
  Rect := CellRect(MarkX,Marky);
  s := Cells[MarkX, MarkY];
  Canvas.Brush.Color := clgreen;
  Canvas.FillRect(Rect);
  DrawText(Canvas.Handle, PChar(s), Length(s), Rect, DT_LEFT);
 end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
 MarkX := ACol;
 Marky := ARow;
end;

end.


Das sollte klappen
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: So 18.05.08 10:41 
Hallo,

wenn ich Dich richtig verstanden habe, soll die aktuell selektierte Zelle Deinen Wünschen entsprechend eingefärbt werden.

Wie schon user profile iconmkinzler richtig ausgeführt hat, OnDrawcell wird für jede Zelle aufgerufen die neu gezeichnet werden muss. Wenn eine Zelle selektiert wird muss auch neu gezeichnet werden. Den Index der Zelle liefert Dir OnDrawCell mit den Prozedur-Variablen aCol und aRow. Über die Variable State kann der Zustand der Selectierung abgefragt und dann entsprechend reagiert werden.

z.B. so wird die selektierte Zelle jeweils rot hinterlegt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.StringGridDrawCell(Sender: TObject; aCol, aRow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if gdSelected in State then
    begin
    StringGrid.Canvas.Brush.Color := clRed;
    StringGrid.Canvas.FillRect(Rect);
    StringGrid.Canvas.TextOut(Rect.Left +2,Rect.Top +2,StringGrid.Cells[aCol, aRow]);
    end;
end;

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



BeitragVerfasst: So 18.05.08 20:54 
user profile iconLannes hat folgendes geschrieben:
Wenn ich Dich richtig verstanden habe, soll die aktuell selektierte Zelle Deinen Wünschen entsprechend eingefärbt werden.


Ne ich meinte in meinem Code wird sie markiert, will aber anstatt einer Markierung färben. Welche Celle das sein muss, sagen die Variablen i und j.
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: So 18.05.08 22:51 
Hallo,

dann prüf doch ob die Koordinaten der aktuell zu zeichnende Zelle den in i und j hinterlegten Koordinaten entspricht.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.StringGridDrawCell(Sender: TObject; aCol, aRow: Integer;  
  Rect: TRect; State: TGridDrawState);  
begin  
  if (i = aCol) and (j = aRow) then  
    begin  
    StringGrid.Canvas.Brush.Color := clRed;  
    StringGrid.Canvas.FillRect(Rect);  
    StringGrid.Canvas.TextOut(Rect.Left +2,Rect.Top +2,StringGrid.Cells[aCol, aRow]);  
    end;  
end;

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



BeitragVerfasst: Mo 19.05.08 12:14 
Soweit bin ich auch gekommen, aber die Frage war - "Wie übergebe ich die Variablen i und j nach OnDrawCell um dort, wie schon gesagt, die Zelle zu färben?"
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mo 19.05.08 12:23 
Gar nicht, den die Methode wird als Callback verwendet. Es wird aber eine Referenz auf das Grid übergeben, dort könntest du die Werte ablegen. (Abgeleitete Gridklasse, Kodierung im Tag, ...). Wo kommen die Koordinaten her?

_________________
Markus Kinzler.
passiv
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 270

WIN XP Professional
Delphi 7 Enterprise, Java (Eclipse), Python 2.5, Django 1.1
BeitragVerfasst: Mo 19.05.08 12:28 
entweder du deklarierst i und j global, was sicher die geschicktere methode wäre.
oder aber du rufst selbst das drawcell-ereignis auf:
ausblenden Delphi-Quelltext
1:
StringGridDrawCell(stringgrid1, i, j, StringGrid1.CellRect(i,j), [gdSelected]);					

und setzt dann eine (zum beispiel) globale boolean-variable die repräsentiert, ob die zelle irgendwie anders gefärbt werden soll. oder du entscheidest über State, was wie gehört. (dann entsprechen ACol und ARow übrigens i und j)

@mkinzler: ich denke nicht dass AlexII (den fragen nach) das verstehen wird ^^

mfg
Fabs

_________________
Die Probleme, die es in der Welt gibt, sind nicht mit der gleichen Denkweise zu lösen, die sie erzeugt hat.
Albert Einstein