Board index » cppbuilder » Minor problem in draw cell event handler in TStringGrid

Minor problem in draw cell event handler in TStringGrid


2003-11-28 01:06:53 AM
cppbuilder6
I have a grid with three columns, the left and right ones contain text and
the middle one contains fixed format numbers. Right adjusting the middle
column should line up the decimal places in the column and make the whole
grid look MUCH better.
Here is the body of the function I tried:
if ( ACol != 1 ) {
DrawText(AnalysisResultSG->Canvas->Handle,
AnalysisResultSG->Cells[ACol][ARow].c_str(),
-1,
&Rect,
DT_LEFT);
} else {
DrawText(AnalysisResultSG->Canvas->Handle,
AnalysisResultSG->Cells[ACol][ARow].c_str(),
-1,
&Rect,
DT_RIGHT);
}
For some reason, while the left column (ACol = 0) is drawn correctly, the
second column is drawn twice, once left adjusted and once right adjusted.
Worse, there is a little extra garbage (which looks like a line) drawn at
the end of the string in the right column (ACol = 2).
I do not understand why this occurs. After all, the logic is simple. If
we're not drawing in the middle column, drawn the text as you normally
would, and if we're drawing in the middle column, draw the text in the cell
right adjusted.
While I know that I'll need to modify the body of the second block in the
conditional statement so that the header for that column can be drawn
centred in the cell, getting that minor change is trivial compared to
getting the above conditional statement to work.
What did I miss?
Cheers,
Ted
 
 

Re:Minor problem in draw cell event handler in TStringGrid

"Ted Byers" < XXXX@XXXXX.COM >wrote:
Quote
[...] Right adjusting the middle column [...]
Like Hans said, make sure that the StringGrids' DefaultDrawing
property is set to false. Here's the rest:
void __fastcall TForm1::AnalysisResultDrawCell(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 = clBtnText;
Flags |= DT_CENTER;
}
else
{
if( State.Contains(gdFocused) )
{
pCanvas->Brush->Color = clHighlite;
pCanvas->Font->Color = clHighliteText;
}
else
{
pCanvas->Brush->Color = pGrid->Color;
pCanvas->Font->Color = pGrid->Font->Color;
}
if( ACol == 1 ) Flags |= DT_RIGHT;
else Flags |= DT_LEFT;
}
pCanvas->FillRect( R );
DrawText( pCanvas->Handle, pGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, Flags );
if( State.Contains(gdFocused) ) DrawFocusRect( pCanvas->Handle, &Rect );
}
~ JD
 

Re:Minor problem in draw cell event handler in TStringGrid

"Ted Byers" < XXXX@XXXXX.COM >wrote:
Quote
[...] Near the end, you have the statement
"pCanvas->FillRect( R );"
Type-o from cut&paste. It of course should be:
pCanvas->FillRect( Rect );
~ JD
 

{smallsort}

Re:Minor problem in draw cell event handler in TStringGrid

Ted Byers wrote:
Quote
Here is the body of the function I tried:
Is that (in) OnDrawCell() ? Is DefaultDrawing true/false ?
Quote
DrawText(AnalysisResultSG->Canvas->Handle,
AnalysisResultSG->Cells[ACol][ARow].c_str(),
-1,
&Rect,
DT_LEFT);
Where the output goes is determined by the values in Rect.
You did not show how you combined Rect with ACol,ARow.
Is ACol,ARow the same as Col,Row from OnDrawCell ?
It looks that DefaultDrawing == true and that from somewhere
else you paint to the Cell's to.
Hans.
 

Re:Minor problem in draw cell event handler in TStringGrid

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Ted Byers wrote:

>Here is the body of the function I tried:

Is that (in) OnDrawCell() ? Is DefaultDrawing true/false ?

Yes to both questions.
Should DefaultDrawing be false?
Quote
>DrawText(AnalysisResultSG->Canvas->Handle,
>AnalysisResultSG->Cells[ACol][ARow].c_str(),
>-1,
>&Rect,
>DT_LEFT);

Where the output goes is determined by the values in Rect.
This I knew.
Quote
You did not show how you combined Rect with ACol,ARow.

I thought that Rect, on entry, had the values appropriate for the current
cell. Is that not true?
Quote
Is ACol,ARow the same as Col,Row from OnDrawCell ?

The code I showed IS the body of the OnDrawCell event handler.
Cheers,
Ted
Quote
It looks that DefaultDrawing == true and that from somewhere
else you paint to the Cell's to.
But there is no other function I have written that paints to the cells.
That is why I am puzzled. I can't figure out why the text is being drawn
twice in the middle column.
 

Re:Minor problem in draw cell event handler in TStringGrid

Thanks JD.
In fact, you just answered one of the questions I just asked Hans.
The other is "Was I right in believing that Rect has the values appropriate
to the cell on entry into the function?"
I have a question about the code you graciously provided. Near the end, you
have the statement "pCanvas->FillRect( R );" In this, was R supposed to be
Rect, or did you intend to set R somehow, and use it for both FillRect and
DrawText?
Thanks again,
Ted
 

Re:Minor problem in draw cell event handler in TStringGrid

Ted Byers wrote:
Quote
>Is that (in) OnDrawCell() ? Is DefaultDrawing true/false ?
>
Yes to both questions.
Your answer is impossible.
Quote
Should DefaultDrawing be false?
Yes.
Quote
The code I showed IS the body of the OnDrawCell event handler.
Aha.
I hope your problem is solved now by JD.
Hans.
 

Re:Minor problem in draw cell event handler in TStringGrid

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Ted Byers wrote:

>>Is that (in) OnDrawCell() ? Is DefaultDrawing true/false ?
>>
>Yes to both questions.

Your answer is impossible.

But rue non-the-less. I had to manually set DefaultDrawing false.
Quote
[snip]
I hope your problem is solved now by JD.

Yes, it is.
Thanks,
Ted
 

Re:Minor problem in draw cell event handler in TStringGrid

"Ted Byers" < XXXX@XXXXX.COM >wrote:
Quote
[...] I also had to append a blank to the text in the middle
column because otherwise the text was drawn a little too
close to the line between the middle and right columns.
Don't do that because it changes the cell value. Simply alter
the Rect parameter:
if( ACol == 1 )
{
...
Rect.left -= 2;
}
~ JD
 

Re:Minor problem in draw cell event handler in TStringGrid

"JD" < XXXX@XXXXX.COM >wrote in message
Quote

"Ted Byers" < XXXX@XXXXX.COM >wrote:
>[...] Near the end, you have the statement
>"pCanvas->FillRect( R );"

Type-o from cut&paste. It of course should be:

pCanvas->FillRect( Rect );

I guessed as much, and on trying it, it now works as desired. But I made a
few minor changes. I did not want the focus rectangle to be drawn, and
apart from the fixed row, I wanted the same colours used for the background
and font, and I decided to centre the text in the right column since it
looks better that way. It contains units for the values provided in the
middle column, and when the abbreviation for these consists of only one
character, it just doesn't look right left adjusted. I also had to append a
blank to the text in the middle column because otherwise the text was drawn
a little too close to the line between the middle and right columns.
But the main thing is that the form and its contents now look good.
Thanks,
Ted