void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect){ if(ACol == 2) StringGrid1->Options >> goEditing; else StringGrid1->Options << goEditing;}
void __fastcall TForm1::StringGrid2DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State){ TStringGrid* Grid = static_cast<TStringGrid*>(Sender); switch(ACol) { case 0: case 1: Grid->Canvas->Brush->Color = (TColor)0x080FF80; // заливка в зависимости от столбца break; case 2: Grid->Canvas->Brush->Color = clMoneyGreen; break; case 3: Grid->Canvas->Brush->Color = clSkyBlue; break; default: Grid->Canvas->Brush->Color = Grid->Color; } if (Grid->Cells[ACol][ARow].IsEmpty()) Grid->Canvas->Brush->Color = clPurple; // заливка пустых ячеек Grid->Canvas->FillRect(Rect); // применить заливку if (State.Contains(gdFixed)) { // обработка фиксированных ячеек Grid->Canvas->Brush->Color = (TColor)0x080FF80; // заливка Grid->Canvas->FillRect(Rect); Grid->Canvas->Font->Color = clBlue; // цвет шрифта (синий) Grid->Canvas->Font->Style = TFontStyles() << fsBold; // жирный шрифт } else { Grid->Canvas->Font->Color = clBlack; // цвет шрифта (черный) Grid->Canvas->Font->Style = TFontStyles(); // обычный шрифт } if (State.Contains(gdFocused)) { Grid->Canvas->DrawFocusRect(Rect); // если фокус, рисуем прямоугольник } if (State.Contains(gdSelected)) { // если выделена ячейка // Grid->Canvas->Font->Color = clRed; Grid->Canvas->Brush->Color = clYellow; Grid->Canvas->FillRect(Rect); // Grid->Canvas->Font->Color = clFuchsia; Grid->Canvas->Font->Style = TFontStyles() << fsBold; } // рисуем текст int n = (State.Contains(gdFixed)) ? 4 : 5; // оффсет для фиксированных ячеек Grid->Canvas->TextOut(Rect.Left + n, Rect.Top + n, Grid->Cells[ACol][ARow]); // вывод текста ячейки}
String text = sgEnergy->Cells[ACol][ARow]; //Рисуем фиксированные ячейки: if(State.Contains(gdFixed)) { sgEnergy->Canvas->Font->Color = clBlue; //Цвет шрифта (синий) sgEnergy->Canvas->Font->Style = TFontStyles() << fsBold; //Жирный шрифт sgEnergy->Canvas->Font->Size = 10; //Размер шрифта } //Выравниваем текст по центру: sgEnergy->Canvas->TextOut(Rect.Left + (Rect.Width() - sgEnergy->Canvas->TextWidth(text))/2, Rect.Top + (Rect.Height() - sgEnergy->Canvas->TextHeight(text))/2, text);
#include ... // Обработчик OnDrawCellTStringGrid *myGrid = (TStringGrid*)Sender;String text = myGrid->Cells[ACol][ARow]; //Рисуем фиксированные ячейки:if(State.Contains(gdFixed)){ myGrid->Canvas->Font->Color = clBlue; //Цвет шрифта (синий) myGrid->Canvas->Font->Style = TFontStyles() << fsBold; //Жирный шрифт myGrid->Canvas->Font->Size = 10; //Размер шрифта GraphUtil::GradientFillCanvas(myGrid->Canvas, myGrid->GradientStartColor, myGrid->GradientEndColor, Rect, gdVertical);}else // Не фиксированная ячейка, заполняется без градиента{ myGrid->Canvas->FillRect(Rect);} // Выравниваем текст по центру: myGrid->Canvas->TextOut(Rect.Left + (Rect.Width() - myGrid->Canvas->TextWidth(text))/2, Rect.Top + (Rect.Height() - myGrid->Canvas->TextHeight(text))/2, text);
//*** РИСОВАНИЕ ТАБЛИЦЫvoid __fastcall TMainForm::sgEnergyDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State){ TStringGrid *Grid = (TStringGrid*)Sender; //--- Сохраняем текст ячейки ----------------------------------------------------------------------------------------------------------------------- String text = Grid->Cells[ACol][ARow]; //--- Если рисуется фиксированная ячейка ----------------------------------------------------------------------------------------------------------- if(State.Contains(gdFixed)) { GradientFillCanvas(Grid->Canvas, Grid->GradientStartColor, //Градиентно заливаем ячейку Grid->GradientEndColor, Rect, gdVertical); Grid->Canvas->Font->Color = clBlue; //Устанавливаем цвет шрифта Grid->Canvas->Font->Style = TFontStyles() << fsBold; //Устанавливаем начертание шрифта Grid->Canvas->Font->Size = 10; //Устанавливаем размер шрифта } //--- Если рисуется обычная ячейка, заливаем ее цветом по умолчанию -------------------------------------------------------------------------------- else Grid->Canvas->FillRect(Rect); //--- Выводим текст в ячеку по центру Grid->Canvas->TextOut(Rect.Left+(Rect.Width()-Grid->Canvas->TextWidth(text))/2, Rect.Top+(Rect.Height()-Grid->Canvas->TextHeight(text))/2, text); }
void __fastcall TForm1::sgEnergyDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State){ TStringGrid *Grid = (TStringGrid*)Sender; String text = Grid->Cells[ACol][ARow]; // Рисуем фиксированные ячейки: if(State.Contains(gdFixed)) { GradientFillCanvas(Grid->Canvas, Grid->GradientStartColor, Grid->GradientEndColor, Rect, gdVertical); Grid->Canvas->Font->Color = clBlue; Grid->Canvas->Font->Style = TFontStyles() << fsBold; Grid->Canvas->Font->Size = 10; } else // Вот этот кусок { if(State.Contains(gdSelected)) { Grid->Canvas->Brush->Color = clSkyBlue; // Или любой другой цвет } Grid->Canvas->FillRect(Rect); // это выполнится и для обычной и для выбранной ячейки // но не для Fixed } // Выравниваем текст по центру: Grid->Canvas->TextOut( Rect.Left + (Rect.Width() - Grid->Canvas->TextWidth(text))/2, Rect.Top + (Rect.Height() - Grid->Canvas->TextHeight(text))/2, text );}
else Grid->Canvas->FillRect(Rect);
Grid->Canvas->TextOut( Rect.Left + (Rect.Width() - Grid->Canvas->TextWidth(text))/2, Rect.Top + (Rect.Height() - Grid->Canvas->TextHeight(text))/2, text
void __fastcall TMainForm::sgEnergyDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State){ TStringGrid *Grid = (TStringGrid*)Sender; //--- Сохраняем текст ячейки ----------------------------------------------------------------------------------------------------------------------- String text = Grid->Cells[ACol][ARow]; //--- Если рисуется фиксированная ячейка ----------------------------------------------------------------------------------------------------------- if(State.Contains(gdFixed)) { GradientFillCanvas(Grid->Canvas, Grid->GradientStartColor, //Градиентно заливаем ячейку Grid->GradientEndColor, Rect, gdVertical); Grid->Canvas->Font->Color = clBlue; //Устанавливаем цвет шрифта Grid->Canvas->Font->Style = TFontStyles() << fsBold; //Устанавливаем начертание шрифта Grid->Canvas->Font->Size = 10; //Устанавливаем размер шрифта } //--- Если рисуется выделенная ячейка -------------------------------------------------------------------------------------------------------------- if(State.Contains(gdSelected)) { Grid->Canvas->Brush->Color = Grid->Color; Grid->Canvas->Font->Color = Grid->Font->Color; } //--- Заливаем ячейку цветом ----------------------------------------------------------------------------------------------------------------------- Grid->Canvas->FillRect(Rect); //--- Выводим текст в ячеку по центру с цветом в соответствии с флагом "Записать накопленную энергию" ---------------------------------------------- if(!chbEnergy->Checked || (ACol == 2 & !chbTariff2->Checked) || (ACol == 3 & !chbTariff3->Checked) || (ACol == 4 & !chbTariff4->Checked)) Grid->Canvas->Font->Color = clGrayText; DrawText(Grid->Canvas->Handle, text.w_str(), -1, &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);}
void __fastcall TForm1::SumRow(int Row){ int sum(0); for (int i=0; i<Grid1->ColCount-1; i++) sum += StrToInt(Grid1->Cells[i][Row]); Grid1->Cells[Grid1->ColCount-1][Row] = IntToStr(sum);}
sum += StrToIntDef(Grid1->Cells[i][Row], 0);