Board index » cppbuilder » How to center the Text in a StringGrid

How to center the Text in a StringGrid


2004-04-05 11:11:49 PM
cppbuilder13
Hello,
does anyone know how to place the text of a grid field in it's center?
Best regards
Roland
 
 

Re:How to center the Text in a StringGrid

"Roland" < XXXX@XXXXX.COM >wrote:
Quote
does anyone know how to place the text of a grid field in it's center?
Set it's DefaultDrawing property to false and add an OnDrawCell event:
void __fastcall TForm1::StringGrid1DrawCell(TObject* Sender, int ACol, int ARow, const TRect &Rect, TGridDrawState State)
{
TStringGrid* pGrid = static_cast<TStringGrid*>( Sender );
TCanvas* pCanvas = pGrid->Canvas;
unsigned int Flags = DT_SINGLELINE | DT_VCENTER;
if( State.Contains(gdFixed) )
{
pCanvas->Brush->Color = pGrid->FixedColor;
pCanvas->Font->Color = pGrid->Font->Color;
if( you want the text centered ) Flags |= DT_CENTER;
else if( you want it right justified ) Flags |= DT_RIGHT;
else /* you want it left justified */ Flags |= DT_LEFT;
}
else
{
if( State.Contains( gdSelected ) )
{
pCanvas->Brush->Color = clHighlight;
pCanvas->Font->Color = clHighlightText;
// set the Flag variable
}
else
{
pCanvas->Brush->Color = pGrid->Color;
pCanvas->Font->Color = pGrid->Font->Color;
// set the Flag variable
}
}
pCanvas->FillRect( Rect );
DrawText( pCanvas->Handle, pGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, Flags );
}
~ JD
 

Re:How to center the Text in a StringGrid

Hello "JD"
it works well,
Thank you!
"JD" < XXXX@XXXXX.COM >wrote:
Quote

"Roland" < XXXX@XXXXX.COM >wrote:
>does anyone know how to place the text of a grid field in it's center?

Set it's DefaultDrawing property to false and add an OnDrawCell event:

void __fastcall TForm1::StringGrid1DrawCell(TObject* Sender, int ACol, int ARow, const TRect &Rect, TGridDrawState State)
{
TStringGrid* pGrid = static_cast<TStringGrid*>( Sender );
TCanvas* pCanvas = pGrid->Canvas;
unsigned int Flags = DT_SINGLELINE | DT_VCENTER;

if( State.Contains(gdFixed) )
{
pCanvas->Brush->Color = pGrid->FixedColor;
pCanvas->Font->Color = pGrid->Font->Color;
if( you want the text centered ) Flags |= DT_CENTER;
else if( you want it right justified ) Flags |= DT_RIGHT;
else /* you want it left justified */ Flags |= DT_LEFT;
}
else
{
if( State.Contains( gdSelected ) )
{
pCanvas->Brush->Color = clHighlight;
pCanvas->Font->Color = clHighlightText;
// set the Flag variable
}
else
{
pCanvas->Brush->Color = pGrid->Color;
pCanvas->Font->Color = pGrid->Font->Color;
// set the Flag variable
}
}

pCanvas->FillRect( Rect );
DrawText( pCanvas->Handle, pGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, Flags );
}

~ JD

 

{smallsort}