Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C++ Builder > Цвет выбранной ячейки StringGrid'а


Автор: pafflique 12.1.2010, 03:14
Закрашивает нужную ячейку, но та, что была закрашанна до этого, опять становится белой.. как запомнить все закрашивания??

Код

long left; long top;

void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{
    TRect A = StringGrid1->CellRect(ACol, ARow);
    left = A.left;
    top = A.top;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1Click(TObject *Sender)
{
    StringGrid1->Canvas->Brush->Color=clBlack;
    left = left / 25; top = top / 25;
    TRect A = StringGrid1->CellRect(left, top);
    StringGrid1->Canvas->FillRect(A);
}

Автор: Usper 12.1.2010, 09:21
Ну например так:
Код
#include <vector>
std::vector <std::vector <bool> > FGrid; //"матрица", в которой будем хранить свойство "покрашенности" ячейки
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
      StringGrid1->RowCount=10;
      StringGrid1->ColCount=10;
      FGrid.resize(StringGrid1->ColCount);
      for(unsigned int i=0; i<FGrid.size(); i++)
        FGrid[i].resize(StringGrid1->RowCount);
      for(unsigned int i=0; i<FGrid.size(); i++)
        for(unsigned int j=0; j<FGrid[i].size(); j++)
            FGrid[i][j]=false; //изначально все ячейки не покрашены
}

void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{
    FGrid[ACol][ARow]=!FGrid[ACol][ARow];
}
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
    if(FGrid[ACol][ARow])
    {
        StringGrid1->Canvas->Brush->Color=clBlack;
        StringGrid1->Canvas->FillRect(Rect);
    }
}

Может быть в StringGrid1SelectCell надо ещё добавить вызов StringGrid1DrawCell.

Автор: pafflique 12.1.2010, 13:52
Код

std::vector <std::vector <bool> > rects;

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    int n = StringGrid1->ColCount;
    int m = StringGrid1->RowCount;
    for (int j = 0; j < n; j++) {
        rects[j].resize(m);
    }


Вылетает по AccessViolation когда делаю ресайз...? Как быть?

Автор: smoke_man 12.1.2010, 14:01
Попробуй так:
Код

...................

    int n = StringGrid1->ColCount;
    int m = StringGrid1->RowCount;
    rects.resize(n);
    for (int j = 0; j < n; j++) 
        rects[j].resize(m); 
...................

Автор: artsb 12.1.2010, 14:16
ИМХО это всё извращение...

pafflique, опишите задачу подробнее. Может можно сделать всё проще.

Добавлено через 1 минуту и 3 секунды
Цитата(pafflique @  12.1.2010,  13:52 Найти цитируемый пост)
Вылетает по AccessViolation когда делаю ресайз...? Как быть? 

Вы не установили размерность первого вектора. smoke_man уже написал что делать.

Автор: THandle 12.1.2010, 14:22
pafflique, посмотри тут:

http://forum.vingrad.ru/index.php?showtopic=260454&view=findpost&p=1876421

Автор: pafflique 12.1.2010, 16:04
Всем спасибо! Все заработало, как я и хотел!  smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)