Autor Beitrag
Skywalker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 04.12.02 17:47 
Hi Leute,

hab mal wieder ein Problem.

Ich möchte in einem StringGrid-Feld mit den Ausmassen 10 x 24 einzelne Felder mit der Farben ROT und GRÜN belegen. Dieses soll nach einem Klick auf einen SpeedButton geschehen.

Ich hab so einige Dinge ausprobiert, aber irgendwie kriege ich den richtigen Dreh nicht hin.

_________________
CU

Skywalker

:nixweiss:
Nobody is perfect!!!!!
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mi 04.12.02 22:16 
hallo skywalker!
ich habe dein problem so verstanden - hoffentlich ist es das, was du haben wolltest :)

ausblenden volle Höhe 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:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
unit Mainprog;

interface

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

type
  TColorField = Array[1..10,1..25] of byte;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    SpeedButton1: TSpeedButton;
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    ColorField: TColorField;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  close;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  aCol,aRow: SmallInt;
begin
  // ColorField mit 0 vorbelegen
  FillChar(ColorField,SizeOf(ColorField),0);
  // Eingabe und Tabulator im Grid zulassen
  StringGrid1.Options:= StringGrid1.Options +
    [goTabs, goEditing];
end;


procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  aCol,aRow: SmallInt;
begin
  // Muster bestehen aus 1 und 2 in ColorField generieren
  for aRow:= 1 to 25 do
    for aCol:= 1 to 10 do
      if ((aRow+aCol) mod 2)= 0 then
        ColorField[aCol,aRow]:= 1
      else
        ColorField[aCol,aRow]:= 2;

  // Neuzeichnen des Grids veranlassen
  StringGrid1.Repaint;
end;


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Farbe: byte;
  TextToShow: String;
begin
  // wenn fixierte Zelle, dann kein umfärben notwendig
  if gdFixed in State then Exit;

  with sender as TStringGrid do begin
    // Farbe aus ColorField wählen
    Farbe:= ColorField[aCol,aRow];
    // Text in Zelle merken
    TextToShow:= Cells[aCol,aRow];
    // Malfarbe auf weiss stellen
    Canvas.Brush.Color:= clWhite;
    // wenn Eintrag in ColorField 1 oder 2, dann Farbe
    // auswählen
    case Farbe of
      1: Canvas.Brush.Color:= clRed;
      2: Canvas.Brush.Color:= clGreen;
    end;
    // Bereich Rect einfärben
    Canvas.FillRect(Rect);
    //  zwischengespeicherten Text ausgeben
    Canvas.TextOut(Rect.Left+2, Rect.Top+2, TextToShow);
  end;
end;


end.


mfg, stefan
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Do 05.12.02 00:13 
Skywalker hat folgendes geschrieben:
Ich hab so einige Dinge ausprobiert, aber irgendwie kriege ich den richtigen Dreh nicht hin.

Dann poste doch mal dieses "Ding" :-D Ich denke dann können wir Dir besser helfen!

Gruß
TINO
Skywalker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Do 05.12.02 11:07 
Sorry ab ich übersehen :cry:

ich hab den Vorschlag von grayfox in meinem Programm mal versucht umzusetzen.


ausblenden volle Höhe 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:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
unit frm_sensorentest;

interface


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

type
  TTor = Array[0..9,0..24] of byte;

type
  TSensortest = class(TForm)
//    StringGrid1: TStringGrid;
    Label2: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    SpeedButton1: TSpeedButton;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations } 
    Tor: TTor;
    StringGrid1: TStringGrid;
  public
    { Public declarations } 
  end;

var
  Sensortest: TSensortest;
//  tor : array [0..24]of array [0..10] of TTorDaten;

implementation

{$R *.dfm}

uses Main;


procedure TSensortest.Button1Click(Sender: TObject);
begin
  close;
end;


procedure TSensortest.FormCreate(Sender: TObject);
//var
  //aCol,aRow: SmallInt;
begin
  // ColorField mit 0 vorbelegen
  FillChar(Tor,SizeOf(Tor),0);
  // Eingabe und Tabulator im Grid zulassen
  StringGrid1.Options:= StringGrid1.Options +
    [goTabs, goEditing]; 
end;

procedure TSensortest.SpeedButton1Click(Sender: TObject);
var 
  aCol,aRow: SmallInt;
begin 
  // Muster bestehen aus 1 und 2 in ColorField generieren
  for aRow:= 0 to 24 do
    for aCol:= 0 to 9 do
      if ((aRow+aCol) mod 2)= 0 then 
        Tor[aCol,aRow]:= 1
      else
        Tor[aCol,aRow]:= 2;

  // Neuzeichnen des Grids veranlassen
  StringGrid1.Repaint; 
end;

procedure TSensortest.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Farbe: byte;
  TextToShow: String;
begin
  // wenn fixierte Zelle, dann kein umfärben notwendig
  if gdFixed in State then Exit;

  with sender as TStringGrid do begin
    // Farbe aus ColorField wählen 
    Farbe:= Tor[aCol,aRow];
    // Text in Zelle merken 
    TextToShow:= Cells[aCol,aRow];
    // Malfarbe auf weiss stellen
    Canvas.Brush.Color:= clWhite;
    // wenn Eintrag in ColorField 1 oder 2, dann Farbe 
    // auswählen
    case Farbe of 
      1: Canvas.Brush.Color:= clRed;
      2: Canvas.Brush.Color:= clGreen; 
    end;
    // Bereich Rect einfärben 
    Canvas.FillRect(Rect);
    //  zwischengespeicherten Text ausgeben 
    Canvas.TextOut(Rect.Left+2, Rect.Top+2, TextToShow);
  end; 
end;

end.


Es läuft an sich schon, aber es wirft mir noch ne Exception raus, weil er noch Daten von einem anderen FRM benötigt (3 x Label).

Datenauswertung aus dem zweiten Programm:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
.
.
ByteToInt(Daten[0],Daten[1],TmpInt);
Label2.Caption := IntToStr(TmpInt);
ByteToInt(Daten[2],Daten[3],TmpInt);
Label4.Caption := IntToStr(TmpInt);
ByteToInt(Daten[4],Daten[5],TmpInt);
Label6.Caption := IntToStr(TmpInt);
.
.


Die Daten die mein Sensortest bekommt sich Integer-Werte, die aus den drei Labels übergeben werden und dann im Raster des StringGrids mit Farben dargestellt werden sollen.

Vielleicht könnt Ihr mir da noch mal ein bißchen helfen.

Danke, schon mal im voraus.

_________________
CU

Skywalker

:nixweiss:
Nobody is perfect!!!!!