BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Minor problem in draw cell event handler in TStringGrid

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Ted Byers
Guest





PostPosted: Thu Nov 27, 2003 5:06 pm    Post subject: Minor problem in draw cell event handler in TStringGrid Reply with quote



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


Back to top
JD
Guest





PostPosted: Thu Nov 27, 2003 5:47 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote



"Ted Byers" <r.ted.byers (AT) sympatico (DOT) ca> 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


Back to top
JD
Guest





PostPosted: Thu Nov 27, 2003 6:09 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote




"Ted Byers" <r.ted.byers (AT) sympatico (DOT) ca> 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


Back to top
Hans Galema
Guest





PostPosted: Thu Nov 27, 2003 6:16 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote

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.


Back to top
Ted Byers
Guest





PostPosted: Thu Nov 27, 2003 6:45 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote

"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> wrote

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.



Back to top
Ted Byers
Guest





PostPosted: Thu Nov 27, 2003 6:52 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote

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


Back to top
Hans Galema
Guest





PostPosted: Thu Nov 27, 2003 7:27 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote

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.


Back to top
Ted Byers
Guest





PostPosted: Thu Nov 27, 2003 7:29 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote


"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> wrote

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



Back to top
JD
Guest





PostPosted: Thu Nov 27, 2003 7:32 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote


"Ted Byers" <r.ted.byers (AT) sympatico (DOT) ca> 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


Back to top
Ted Byers
Guest





PostPosted: Thu Nov 27, 2003 7:34 pm    Post subject: Re: Minor problem in draw cell event handler in TStringGrid Reply with quote


"JD" <nospam (AT) nospam (DOT) com> wrote

Quote:

"Ted Byers" <r.ted.byers (AT) sympatico (DOT) ca> 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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.