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 

How to get rid of flick from derived TStringGrid class ?

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





PostPosted: Wed Dec 17, 2003 5:12 am    Post subject: How to get rid of flick from derived TStringGrid class ? Reply with quote



How to get rid of flick from derived TStringGrid class ?

Hi All:
I using Borland C++ Builder 6.0 enterprise edition to derived an class
name TStringGridBS from TStringGrid, and override DrawCell function
for my custom draw cell, but when I use this derived component, I find it
continuous flicking and I have no any solution to solved even after
search on Google. The following code are my derived class, if you have any
good idea to get rid of continuous flick, Please help me! Thanks a lot.

//File: TStringGridBS.h
//Code:
//--------------------------------------------------------------------------
-

#ifndef StringGridBSH
#define StringGridBSH

//--------------------------------------------------------------------------
-
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <Grids.hpp>

//--------------------------------------------------------------------------
-


//--------------------------------------------------------------------------
-
class PACKAGE TStringGridBS : public TStringGrid
{
private:
TColor FGridBodayColor;
TColor __fastcall GetMainStyle();
void __fastcall SetMainStyle(TColor value);

protected:
virtual void __fastcall DrawCell(int ACol, int ARow, const
Types::TRect &ARect, TGridDrawState AState);

public:
__fastcall TStringGridBS(TComponent* Owner);

__published:
__property TColor GridBodayColor = { read = GetMainStyle, write =
SetMainStyle, default = clWhite };
};

//--------------------------------------------------------------------------
-
#endif
//--------------------------------------------------------------------------
-

//File:TStringGridBS.cpp
//Code:
//--------------------------------------------------------------------------
-

//--------------------------------------------------------------------------
-

#include <vcl.h>

#pragma hdrstop

#include "TStringGridBS.h"
#pragma package(smart_init)

//--------------------------------------------------------------------------
-
// ValidCtrCheck is used to assure that the components created do not
have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TStringGridBS *)
{
new TStringGridBS(NULL);
}

//--------------------------------------------------------------------------
-
__fastcall TStringGridBS::TStringGridBS(TComponent* Owner) :
TStringGrid(Owner)
{
this->Font->Name = "Fixdays";
this->FixedCols = 0;
FGridBodayColor = clWhite;
}

//--------------------------------------------------------------------------
-
namespace Stringgridbs
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TStringGridBS)};
RegisterComponents("ASIAINFO", classes, 0);
}
}

//--------------------------------------------------------------------------
-
// implement custom draw cell

//--------------------------------------------------------------------------
-
void __fastcall TStringGridBS::DrawCell(int ACol, int ARow, const
Types::TRect &ARect, TGridDrawState AState)
{
// call parent class draw cell function
TStringGrid::DrawCell(ACol, ARow, ARect, AState);

RECT rcCell = {ARect.left, ARect.top, ARect.right, ARect.bottom};
switch (ARow)
{
case 0: //draw the first row
{
this->Font->Size = 14;
this->Font->Style = TFontStyles() << fsBold;
this->Font->Color = TColor(RGB(39, 80, 166));
this->Canvas->Brush->Color = TColor(RGB(119, 159, 225));
}
break;

default: //draw the other row
{
this->Font->Size = 12;
this->Font->Style = TFontStyles() >> fsBold;
this->Font->Color = TColor(RGB(0, 0, 0));

// Draw the Selected Cell
if (this->Selection.Top == ARow)
{
this->Canvas->Brush->Color = TColor(RGB(255, 153, 0));
}
else // Draw the normal Cell
{
this->Canvas->Brush->Color = this->FGridBodayColor;
}
}
break;
}

// Fill Grid background color and draw the cell text
this->Canvas->FillRect(ARect);
::DrawText(
this->Canvas->Handle,
this->Cells[ACol][ARow].c_str(),
this->Cells[ACol][ARow].Length(),
&rcCell,
DT_CENTER | DT_SINGLELINE | DT_VCENTER
);

if (this->OnDrawCell)
{
this->OnDrawCell(this, ACol, ARow, ARect, AState);
}
}


//--------------------------------------------------------------------------
-
void __fastcall TStringGridBS::SetMainStyle(TColor value)
{
FGridBodayColor = value;
}


//--------------------------------------------------------------------------
-
TColor __fastcall TStringGridBS::GetMainStyle()
{
return FGridBodayColor;
}
//--------------------------------------------------------------------------
-


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Dec 17, 2003 5:44 am    Post subject: Re: How to get rid of flick from derived TStringGrid class ? Reply with quote



"Mars" <Marslv_gzb (AT) 21cn (DOT) net> wrote


Quote:
this->Font->Size = 14;
this->Font->Style = TFontStyles() << fsBold;
this->Font->Color = TColor(RGB(39, 80, 166));
this->Canvas->Brush->Color = TColor(RGB(119, 159, 225));

You should not be changing the grid's Font property inside the DrawCell
event(). Internally, the Font property has an OnChange event handler
assigned to it that calls Invalidate() whenever the Font is altered. That
is why you are getting a lot of flickering. Instead, you should be
modifying just the Canvas's Font, not the grid's Font, ie:

this->Canvas->Font->Size = 14;
this->Canvas->Font->Style = TFontStyles() << fsBold;
this->Canvas->Font->Color = TColor(RGB(39, 80, 166));
//...

Quote:
if (this->OnDrawCell)
{
this->OnDrawCell(this, ACol, ARow, ARect, AState);
}

The inherited TStringGrid::DrawCell() (actually, TDrawGrid::DrawCell())
already triggers the OnDrawCell event. You are actually invoking the user's
OnDrawCell event twice for each drawing of a cell since you are invoking the
base class behavior before doing your own drawing, and then calling the
event handler again after you finish your drawing.

Quote:
void __fastcall TStringGridBS::SetMainStyle(TColor value)
{
FGridBodayColor = value;
}

Since you are changing a value that directly effects your drawing, you
should be calling Invalidate() so that teh grid can re-draw itself with the
new value as soon as possible:

void __fastcall TStringGridBS::SetMainStyle(TColor value)
{
if( FGridBodayColor != value )
{
FGridBodayColor = value;
Invalidate();
}
}


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Dec 17, 2003 5:44 am    Post subject: Re: How to get rid of flick from derived TStringGrid class ? Reply with quote




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


Quote:
Why were you calling that function? Remove it temporally and
see if this is the cause of the flicker.

It is not. See my other reply.


Gambit



Back to top
Mars
Guest





PostPosted: Wed Dec 17, 2003 8:51 am    Post subject: Re: How to get rid of flick from derived TStringGrid class ? Reply with quote

Thanks a lot to Remy Lebeau (TeamB)!!!
you are right, and the component work as I will now.
Thanks again!
I love Borland!!!


"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> дÈëÓʼþ
news:3fdfec07 (AT) newsgroups (DOT) borland.com...
Quote:

"Mars" <Marslv_gzb (AT) 21cn (DOT) net> wrote in message
news:3fdfe5d9$1 (AT) newsgroups (DOT) borland.com...

this->Font->Size = 14;
this->Font->Style = TFontStyles() << fsBold;
this->Font->Color = TColor(RGB(39, 80, 166));
this->Canvas->Brush->Color = TColor(RGB(119, 159, 225));

You should not be changing the grid's Font property inside the DrawCell
event(). Internally, the Font property has an OnChange event handler
assigned to it that calls Invalidate() whenever the Font is altered. That
is why you are getting a lot of flickering. Instead, you should be
modifying just the Canvas's Font, not the grid's Font, ie:

this->Canvas->Font->Size = 14;
this->Canvas->Font->Style = TFontStyles() << fsBold;
this->Canvas->Font->Color = TColor(RGB(39, 80, 166));
//...

if (this->OnDrawCell)
{
this->OnDrawCell(this, ACol, ARow, ARect, AState);
}

The inherited TStringGrid::DrawCell() (actually, TDrawGrid::DrawCell())
already triggers the OnDrawCell event. You are actually invoking the
user's
OnDrawCell event twice for each drawing of a cell since you are invoking
the
base class behavior before doing your own drawing, and then calling the
event handler again after you finish your drawing.

void __fastcall TStringGridBS::SetMainStyle(TColor value)
{
FGridBodayColor = value;
}

Since you are changing a value that directly effects your drawing, you
should be calling Invalidate() so that teh grid can re-draw itself with
the
new value as soon as possible:

void __fastcall TStringGridBS::SetMainStyle(TColor value)
{
if( FGridBodayColor != value )
{
FGridBodayColor = value;
Invalidate();
}
}


Gambit





Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) 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.