Board index » cppbuilder » Creating CheckBox in TStringGrid cell
Tracy Stevens
![]() CBuilder Developer |
Creating CheckBox in TStringGrid cell2003-08-06 04:38:08 AM cppbuilder112 I want to have a CheckBox in some cells of a TStringGrid. I referenced a Developer's Journal article about this exact functionality, but I haven't been able to get it to work. It compiles and runs and draws the text and edges correctly, but it does not add any CheckBoxes. I've copied the code exactly as it exists in the Journal (October 2000), but will reprint it below in case it helps. Thanx, Tracy bool __fastcall TFormUserTables::GetCheckState(TStringGrid &AGrid, int ACol, int ARow) { return HIWORD(reinterpret_cast<long>(AGrid.Objects[ACol][ARow])); } //-------------------------------------------------------------------------- - void __fastcall TFormUserTables::SetCheckState(TStringGrid &AGrid, int ACol, int ARow, bool AChecked) { long data = reinterpret_cast<long>(AGrid.Objects[ACol][ARow]); AGrid.Objects[ACol][ARow] = reinterpret_cast<TObject*>(MAKELONG(LOWORD(data), AChecked)); } //-------------------------------------------------------------------------- - bool __fastcall TFormUserTables::GetCheckBox(TStringGrid &AGrid, int ACol, int ARow) { return LOWORD(reinterpret_cast<long>(AGrid.Objects[ACol][ARow])); } //-------------------------------------------------------------------------- - void __fastcall TFormUserTables::SetCheckBox(TStringGrid &AGrid, int ACol, int ARow, bool AShow, bool AChecked) { AGrid.Objects[ACol][ARow] = reinterpret_cast<TObject*>(MAKELONG(AShow, false)); SetCheckState(AGrid, ACol, ARow, AChecked); } //-------------------------------------------------------------------------- - void __fastcall TFormUserTables::SGM_GDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State) { TStringGrid *SGrid = static_cast<TStringGrid*>(Sender); assert(SGrid != NULL); TCanvas *SGCanvas = SGrid->Canvas; SGCanvas->Font = SGrid->Font; RECT RText = static_cast<RECT>(Rect); const AnsiString text(SGrid->Cells[ACol][ARow]); const bool fixed = State.Contains(gdFixed); const bool focused = State.Contains(gdFocused); bool selected = State.Contains(gdSelected); if (!SGM_G->Options.Contains(goDrawFocusSelected)) selected = selected && !focused; if (fixed) { SGCanvas->Brush->Color = SGM_G->FixedColor; SGCanvas->Font->Color = clBtnText; SGCanvas->FillRect(Rect); Frame3D(SGCanvas, Rect, clBtnHighlight, clBtnShadow, 1); } else if (selected) { SGCanvas->Brush->Color = clHighlight; SGCanvas->Font->Color = clHighlightText; SGCanvas->FillRect(Rect); } else { SGCanvas->Brush->Color = SGM_G->Color; SGCanvas->Font->Color = SGM_G->Font->Color; SGCanvas->FillRect(Rect); } if (focused) DrawFocusRect(SGCanvas->Handle, &RText); RText.left += 2; RText.top += 2; DrawText(SGCanvas->Handle, text.c_str(), text.Length(), &RText, DT_LEFT | DT_VCENTER | DT_SINGLELINE); if (GetCheckBox(*SGrid, ACol, ARow)) { unsigned int state = DFCS_BUTTONCHECK; if (GetCheckState(*SGrid, ACol, ARow)) state = state | DFCS_CHECKED; RECT RCell = static_cast<RECT>(Rect); OffsetRect(&RCell, 2, .5 * (RCell.bottom - RCell.top)); RCell.right = RCell.left + GetSystemMetrics(SM_CXMENUCHECK); RCell.bottom = RCell.top + GetSystemMetrics(SM_CYMENUCHECK); RCell.top -= .5 * (RCell.bottom - RCell.top) + 2; DrawFrameControl(SGM_G->Canvas->Handle, &RCell, DFC_BUTTON, state); RText.left = RText.right; } RText.left += 2; RText.top += 2; DrawText(SGCanvas->Handle, text.c_str(), text.Length(), &RText, DT_LEFT | DT_VCENTER | DT_SINGLELINE); } |