Board index » cppbuilder » TShape changing color at runtime

TShape changing color at runtime

I have a routine that changes the color of a TShape at runtime depending on
the status I pass to it:

<snip>

          if (thestatus == "OK")
          {
            CurrentShape->Brush->Color = clGreen;
            CurrentShape->Pen->Color = clGreen;
          }
          else if (thestatus == "Unknown")
          {
            CurrentShape->Brush->Color = clSilver;
            CurrentShape->Pen->Color = clSilver;
          }
          else if (thestatus == "Bad")
          {
            CurrentShape->Brush->Color = clMaroon;
            CurrentShape->Pen->Color = clMaroon;
          }
          Application->ProcessMessages();
<snip>

However when I run this as it changes the shapes color, they always appear
white during the transition. Why is this? Is there anyway to avoid it?

Many thanks,

Phil V
--

"If it is called software, why is it so hard?" - Unknown

 

Re:TShape changing color at runtime


Hi Phil,

Perhaps it would be a good idea to post all the relevant bits and pieces
to be able to spot the problem.

Rodolfo

Re:TShape changing color at runtime


Try calling CurrentShape->Invalidate() or CurrentShape->Repaint() instead of
Application->ProcessMessages().

Gambit

Quote
"Phil" <pvalent...@continental-microwave.co.uk> wrote in message

news:3d58d8f2$1@dnews...
Quote
> However when I run this as it changes the shapes color,
> they always appear white during the transition. Why is this?
> Is there anyway to avoid it?

Re:TShape changing color at runtime


There is no more relevent code.

The code goes round in a loop casting shapes to be CurrentShape, and then
altering the color. I think it is because some other section of code must be
doing something though, because when I try this with just 50 shapes on  a
form they change fine, but when I try it in my code it doesn't.

Phil

Quote
"Rodolfo Frino" <starcinema2...@iprimus.com.au> wrote in message

news:3d58e9be@dnews...
Quote
> Hi Phil,

> Perhaps it would be a good idea to post all the relevant bits and pieces
> to be able to spot the problem.

> Rodolfo

Re:TShape changing color at runtime


Ok, I think it is something to do with the way the shapes work, as I found
when I tried this simple program. On a form create as many TShapes as you
like, (I used circles as these really emphasise the point of this, but any
shape reacts the same). The names of the shapes should be left as Shape1,
Shape2, Shape3 etc.
Also Create 3 TButton objects on the form with the captions "Grey", "Green"
& "Red". Below is the code.

When run as normal speed the effect cannot be seen, presumable because the
CPU isn't busy enough to slow down, (if this is indeed a CPU / resources etc
problem).

To see the problem put a break point in ButtonClick Handler.
When the program runs click one of the buttons, and when the break point is
reached step through the code. It can be seen that as soon as the TShape has
it's color (pen or brush), changed it is replaced by a opaque white
rectangle / square that covers the TShapes bounding area. When the repaint
is called this is then replaced by the new coloured shape. I guess my
program is just so busy, (it is processing many SNMP communications) that
this momentary white square actually becomes visible to the eye even at run
speed. The only solution I can find is try and find an alternative, any
ideas anyone?

Thanks,

Phil V

// CODE FOLLOWS:
//--------------------------------------------------------------------------
-

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

Quote
}

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

void __fastcall TForm1::ButtonClick(TObject *Sender)
{
  if (TButton* CurrentButton = dynamic_cast<TButton*>(Sender))
  {
    TColor thecolor=NULL;

    if (CurrentButton->Caption=="Green")
    {
      thecolor = clGreen;
    }
    else if (CurrentButton->Caption=="Red")
    {
      thecolor = clMaroon;
    }
    else if (CurrentButton->Caption=="Grey")
    {
      thecolor = clSilver;
    }

    for (int x=1; x>0; x++)
    {
      if (TShape* CurrentShape =
dynamic_cast<TShape*>(Form1->FindComponent("Shape" + AnsiString(x))))
      {
        CurrentShape->Pen->Color = thecolor;
        CurrentShape->Brush->Color = thecolor;
        CurrentShape->Repaint();
      }
      else
      {
        break;
      }
    }
  }

Quote
}

//--------------------------------------------------------------------------
-
// CODE ENDS

<snip>

Quote
> when I run this as it changes the shapes color, they always appear
> white during the transition. Why is this? Is there anyway to avoid it?

<snip>

Re:TShape changing color at runtime


Please don't tell me I have stumped the group.

Is this just something I am going to have to accept?

Phil

Quote
"Phil" <pvalent...@continental-microwave.co.uk> wrote in message

news:3d58d8f2$1@dnews...
Quote
> I have a routine that changes the color of a TShape at runtime depending
on
> the status I pass to it:

> <snip>

>           if (thestatus == "OK")
>           {
>             CurrentShape->Brush->Color = clGreen;
>             CurrentShape->Pen->Color = clGreen;
>           }
>           else if (thestatus == "Unknown")
>           {
>             CurrentShape->Brush->Color = clSilver;
>             CurrentShape->Pen->Color = clSilver;
>           }
>           else if (thestatus == "Bad")
>           {
>             CurrentShape->Brush->Color = clMaroon;
>             CurrentShape->Pen->Color = clMaroon;
>           }
>           Application->ProcessMessages();
> <snip>

> However when I run this as it changes the shapes color, they always appear
> white during the transition. Why is this? Is there anyway to avoid it?

> Many thanks,

> Phil V
> --

> "If it is called software, why is it so hard?" - Unknown

Other Threads