Board index » cppbuilder » Re: TSringGrid change colors of cells

Re: TSringGrid change colors of cells


2003-08-23 12:35:34 AM
cppbuilder16
"Nicolae Fieraru" < XXXX@XXXXX.COM >wrote in message
Quote
This would be good for me, I could pass the Sender, ACol,
ARow, and the State, but I have no idea what do I have to pass for Rect.
You should not call the OnDrawCell handler directly for what you are asking.
It will not do what you think it will. Instead, you need to store
persistent information about each cell separately and then let your
OnDrawCell handler query that information for each cell when needed and draw
them accordingly. You can use the grid's Objects[][] property for storing
the infomration, or better yet a multidimensional std::vector or
DynamicArray. For example:
struct CellInfo
{
TColor color;
// other information as needed
};
DynamicArray< DynamicArray<CellInfo>>CellsArr;
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
CellsArr.Length = StringGrid1->RowCount;
for(int x = 0; x < CellsArr.Length; x)
{
CellsArr[x].Length = StringGrid1->ColCount;
for(int y = 0; y < CellsArr[x].Length; ++y)
CellsArr[x][y].color = initial color;
}
}
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
StringGrid1->Canvas->Brush->Color = CellsArr[ACol][ARow].color;
StringGrid1->Canvas->FillRect(Rect);
// finish drawing the cell as needed...
}
void __fastcall TForm1::ChangeColor(int ACol, int ARow, TColor AColor)
{
CellsArr[ACol][ARow].color = AColor;
StringGrid1->Invalidate();
}
Gambit
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 8/14/03
 
 

Re:Re: TSringGrid change colors of cells

Hi Remy,
Thank you for the answer. I tried what you recommended and it works. Thank
you very much.
I will add the code here, I hope others will find it useful. With little
change can highlight a whole row or a column, and do other things.
//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int i, j;
struct CellInfo
{
TColor color;
int value;
};
DynamicArray< DynamicArray < CellInfo>>CellsArray;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner):
TForm(Owner)
{
i = 1;
j = 1;
CellsArray.Length = StringGrid1->RowCount;
for (int i=0; i<CellsArray.Length; i++)
{
CellsArray[i].Length = StringGrid1->ColCount;
for (int j=0; j<CellsArray[i].Length; j++)
{
CellsArray[i][j].value = j*5+i;
CellsArray[i][j].color = clWhite;
StringGrid1->Cells[i][j] = CellsArray[i][j].value;
}
}
CellsArray[2][2].color = clYellow; //this is the seed
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
StringGrid1->Canvas->Brush->Color = CellsArray[ACol][ARow].color;
StringGrid1->Canvas->FillRect(Rect);
AnsiString text = StringGrid1->Cells[ACol][ARow];
StringGrid1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::btnAutoClick(TObject *Sender)
{
ChangeColor();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ChangeColor() //this function goes through the
cells and if it encounters a yellow cell, it will change the color of
//the next cell
on diagonal to yellow - it is only a demo function
{
for(int i = 1; i < StringGrid1->RowCount-1; i++)
for(int j = 1; j < StringGrid1->ColCount-1; j++)
{
if(CellsArray[i][j].color == clYellow)
CellsArray[i+1][j+1].color = clYellow;
}
Edit1->Text = IntToStr(i);
i++;
StringGrid1->Invalidate();
Timer1->Enabled = false;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ChangeColorOneStep();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ChangeColorOneStep()
{
bool br = false;
for(int i = 1; i < StringGrid1->RowCount-1; i++)
{
for(int j = 1; j < StringGrid1->ColCount-1; j++)
{
if(CellsArray[i][j].color == clYellow && CellsArray[i+1][j+1].color
== clWhite)
{
CellsArray[i+1][j+1].color = clYellow;
br = true;
}
if(br) break;
}
if(br) break;
}
Edit1->Text = IntToStr(i);
i++;
StringGrid1->Invalidate(); //this function is necessary if calling
ChangeColorOneStep from a timer, otherwise the stringgrid won't be refreshed
}
void __fastcall TForm1::btnOnTimerClick(TObject *Sender)
{
Timer1->Enabled = true;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
ChangeColorOneStep();
}
//--------------------------------------------------------------------------
-