Board index » cppbuilder » Creating CheckBox in TStringGrid cell

Creating CheckBox in TStringGrid cell


2003-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);
}
 
 

Re:Creating CheckBox in TStringGrid cell

"Tracy Stevens" < XXXX@XXXXX.COM >wrote:
Quote
[...] I've copied the code exactly as it exists in the
Journal (October 2000), but will reprint it below in case it
helps.
Are you sure 'exactly'?? <g>
You probably missed it because you've been staring at it all
day. First, I see no code that sets the object to indicate
that a CheckBox should be drawn. Second, I see a conflict with
how you're storing and retrieving the CheckBox state. In the
Get/SetCheckState, you have mixed the HIWORD and the LOWORD.
By the process of ellimination, I have determined that
SetCheckState is in error and should use HIWORD.
As a note - the names of the functions are so similar that it's
confusing as to what they actually. I had to actually read the
code within it to decide what function it was. I've also made
some changes to the OnDrawCell which you are always free to
disregard in whole or in part.
~ JD
void __fastcall TFormUserTables::SGM_GDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *SGrid = static_cast<TStringGrid*>(Sender);
// you don't need assert here
assert(SGrid != NULL);
// the reason for assigning the canvas to a TCanvas variable
// is to save on typing and improve readability
TCanvas *pCanvas = SGrid->Canvas;
RECT RText = static_cast<RECT>(Rect);
if( State.Contains(gdFixed) )
{
pCanvas->Brush->Color = SGrid->FixedColor;
pCanvas->Font->Color = SGrid->Font->Color;
}
else if( State.Contains( gdSelected ) )
{
pCanvas->Brush->Color = clHighlight;
pCanvas->Font->Color = clHighlightText;
}
else
{
pCanvas->Brush->Color = SGrid->Color;
pCanvas->Font->Color = SGrid->Font->Color;
}
pCanvas->Font = SGrid->Font;
pCanvas->FillRect( Rect );
if( State.Contains( gdFocused ) ) DrawFocusRect( pCanvas->Handle, &Rect );
else if( State.Contains(gdFixed) ) Frame3D( pCanvas, Rect, clBtnHighlight, clBtnShadow, 1 );
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( pCanvas->Handle, &RCell, DFC_BUTTON, state);
Rect.left = RText.right;
}
Rect.left += 2;
DrawText( pCanvas->Handle, SGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, DT_LEFT | DT_SINGLELINE | DT_VCENTER );
}
 

Re:Creating CheckBox in TStringGrid cell

Indeed.
I assumed the GetCheckBox() condition would result in all cells instead of
none. Customizing that requirement was what I overlooked.
Thanx.
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

Are you sure 'exactly'?? <g>

You probably missed it because you've been staring at it all
day. First, I see no code that sets the object to indicate
that a CheckBox should be drawn. Second, I see a conflict with
how you're storing and retrieving the CheckBox state. In the
Get/SetCheckState, you have mixed the HIWORD and the LOWORD.
By the process of ellimination, I have determined that
SetCheckState is in error and should use HIWORD.
 

{smallsort}