Programación General > C++ Builder

 Color De Celdas En Stringgrid

(1/1)

jumanor:
necesito cambiar el color de fondo de una determinada celda en un StringGrid

gracias por su apoyo

_Viktor:
Creo que esto te servira, lo que hace es pintar las FixedRows y FixedColumms en Negro y la celda seleccionada en Amarillo, va en el evento OnDrawCell del StringGrid.



--- Código: Text ---void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,      int ARow, TRect &Rect, TGridDrawState State){TStringGrid* Grilla = static_cast<TStringGrid*>(Sender);TCanvas *canvas = dynamic_cast<TStringGrid*>(Sender)->Canvas;TRect r(Rect);canvas->Font=Grilla->Font; //Aqui pinta las celdas Fixedif(ACol==0 || ARow==0){  canvas->Brush->Color = clBlack;}  //Aqui las Seleccionadas.if(State.Contains(gdSelected)){      canvas->Font->Color = clWindow;      canvas->Brush->Color = clYellow;}canvas->FillRect(r);DrawText(canvas->Handle, StringGrid1->Cells[ARow][ACol].c_str(), StringGrid1->Cells[ARow][ACol].Length(), &r,DT_LEFT);} 
O si lo que quieres es cambiar el color de una celda de acuerdo a su contenido puedes hacer esto:


--- Código: Text ---//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)        : TForm(Owner){StringGrid1->Cells[2][1] = "rojo";StringGrid1->Cells[3][1] = "verde";StringGrid1->Cells[2][2] = "celeste";StringGrid1->Cells[2][3] = "Azul";}//--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,      int ARow, TRect &Rect, TGridDrawState State){if ((ACol>0) && (ARow>0)){  if (StringGrid1->Cells[ACol][ARow] == "rojo")    StringGrid1->Canvas->Brush->Color = clRed;  else if (StringGrid1->Cells[ACol][ARow] == "verde")    StringGrid1->Canvas->Brush->Color = clGreen;  else if (StringGrid1->Cells[ACol][ARow] == "celeste")    StringGrid1->Canvas->Brush->Color = clAqua;  else if (StringGrid1->Cells[ACol][ARow] == "Azul")    StringGrid1->Canvas->Brush->Color = clBlue;  else    StringGrid1->Canvas->Brush->Color = clBlack;  StringGrid1->Canvas->FillRect(Rect); StringGrid1->Canvas->TextOut(Rect.Left,Rect.Top,StringGrid1->Cells[ACol][ARow]);  }}//--------------------------------------------------------------------------- 

Haber si era lo que necesitabas.
Saludos!!!

Navegación

[0] Índice de Mensajes

Ir a la versión completa