Board index » cppbuilder » TBrush question???

TBrush question???


2004-07-23 04:52:26 AM
cppbuilder27
Hi,
I have a 3 part question regarding TBrush.
PART 1);
I have a TButton called MyButton;
In RUNTIME, I tried to change color of MyButton by doing..
MyButton->Brush->Color = clRed;
MyButton->Brush->Style = bsSolid;
Problem: The button still remains of the default color
clBtnFace.
Can anyone explanin Why this happens? Does the button needs to
be "refreshed" some how to take the effect of the new color?
How can I get around fixing this problem?
2)
I have a TPageControl called MyPC. It has 3 pages Page1, Page2,
and Page3 (type TTabSheet)
Now, I did the same thing (as mentioned above) with the pages...
Page1->Brush->Color = clRed;
Page1->Brush->Style = bsSolid;
Page2->Brush->Color = clBlue;
Page2->Brush->Style = bsSolid;
Page3->Brush->Color = clYellow;
Page3->Brush->Style = bsSolid;
Problem: Say, Page1 was the ActivePage at run time, then it
won't show the new color (i.e Red) right away. Also, when I
click at different spots on the page, I see slight Red patches
at the places I clicked. Now, when I click on the tab of Page2,
I can see Blue. When I can click on the tab of Page3, I can
see Yellow. When, I click back on Page1 tab NOW I can see
RED.
Can some one explain why this happens? i.e. the active page
doesn't change the color right away, but when you go to a
different page and come back to the original page, then it
shows the new color. Why?
Again, how do I get around this problem? Does it require some
sort of "refreshing"?
3) This is very similar to parts 1 and 2.
I have a class...
class MySBtn : public TSpeedButton
{
private:
protected:
public:
__property Canvas;
__published:
};
Only purpose of MySBtn is to be able to access the Canvas
property of the TSpeedButton as you can see.
Now, I have a pSBtn of type MySBtn. I tried to color it by
doing the following...
pSBtn->Canvas->Brush->Color = clRed;
pSBtn->Canvas->Brush->Style = bsSolid;
pSBtn->Canvas->FillRect(Rect(0,0, pSBtn->Left + pSBtn->Width, pSBtn->Top + pSBtn->Height));
Why does the color of pSBtn does not change? How do I solve the
problem. I would like pSBtn to be Red.
NOTE: here, the pSBtn is on the form (and not in a group box or
anything, so it acts like a regular button and not a radio btn).
I chose TSpeedButton because I also use them in a GroupBox so
they act like Radio buttons (only one "pressed" at a time).
Whey I did the same thing as mentioned above when the button was
"pressed" in a groupbox (which has multiple MySBtns)it worked..
i.e. the button becomes Red. Does this mean that only when the
button is "pressed" (Down property is true) you can access the
Canvas property?
I will appreciate your help.
thanks,
Veebo
 
 

Re:TBrush question???

"Veebo" < XXXX@XXXXX.COM >wrote in message
Quote
Problem: The button still remains of the default color clBtnFace.
As well it should be.
Quote
Can anyone explanin Why this happens?
You cannot change the color of a TButton. It does not get its color from
the VCL at all, but instead is drawn by the OS itself, using the Control
Panel settings.
To do what you ask, you would have to give the TButton the BS_OWNERDRAW
style and then draw the button manually in response to the WM_DRAWITEM
message, which you will also have to intercept manually.
Otherwise, use a different button component that support user-defined
colors. Or alternatively use a TPanel instead with its Bevel properties
manipulated in the OnMouse... events.
Quote
Now, I did the same thing (as mentioned above) with the pages...
<snip>
i.e. the active page doesn't change the color right away,
but when you go to a different page and come back to the
original page, then it shows the new color. Why?
The brush color did not take effect right away because the page was not
redrawn right away. Changing the Brush does not trigger any notifications
to tell the control that it needs to redraw itself. You have to refresh it
manually, such as by calling the page's Invalidate() method. When you
change pages, they do get redrawn automatically.
Quote
pSBtn->Canvas->FillRect(Rect(0,0, pSBtn->Left + pSBtn->Width,
pSBtn->Top + pSBtn->Height));

Why does the color of pSBtn does not change?
Because you are not providing the correct values to Rect(), thus you are
producing a drawing rectangle that is probably outside the button's visiable
area. Get rid of the Left and Top values altogether:
pSBtn->Canvas->FillRect(Rect(0, 0, pSBtn->Width, pSBtn->Height));
Quote
I would like pSBtn to be Red.
Rather than calling FillRect(), just Invalidate() the button instead, and it
will redraw itself with the new Brush settings.
Gambit
 

Re:TBrush question???

Quote
to tell the control that it needs to redraw itself. You have to refresh it
manually, such as by calling the page's Invalidate() method. When you

thanks for your response. Invalidate() helped. However, I
still have one issue.
Here's what I'm doing in runtime...
In my PageControl I am
Assiging Page1, Page2, and Page3 to colors clRed, clYellow,
and clBlue respectively. Say, Page1 is the activepage.
Now, I change the form color to clGreen by doing..
this->Color = clGreen
this->Invalidate();
Now, when I click on the tabs of Page1, Page2, Page3, I see
green instead of red, yellow, and blue. Can you explain why?
I tried using Invalidate() and Repaint() for the TabSheets, but
it doesn't work.
I will appreciate your help.
cheers,
Veebo
 

{smallsort}

Re:TBrush question???

"Veebo" < XXXX@XXXXX.COM >wrote in message
Quote
this->Color = clGreen
this->Invalidate();
You don't need to call Invalidate() when changing the Color. The setter
method for Color calls Invalidate() internally.
Quote
Now, when I click on the tabs of Page1, Page2, Page3,
I see green instead of red, yellow, and blue. Can you
explain why?
You didn't reset the Brush for the pages after altering the form's Color.
When you changed the form's Color, it also changed the form's ParentColor
property to false. If the PageControl's ParentColor is true then it
detected that the form's ParentColor property was changed and then reset the
pages back to their default coloring accordingly.
Gambit
 

Re:TBrush question???

Quote
You didn't reset the Brush for the pages after altering the form's Color.
When you changed the form's Color, it also changed the form's ParentColor
property to false. If the PageControl's ParentColor is true then it
detected that the form's ParentColor property was changed and then reset the
pages back to their default coloring accordingly.


Gambit

thanks for the reply.
So, you're saying I need to re-do something like..
Page1->Brush->Color = clRed;
Page1->Brush->Style = bsSolid;
Page2->Brush->Color = clYellow;
Page2->Brush->Style = bsSolid;
Page3->Brush->Color = clBlue;
Page3->Brush->Style = bsSolid;
Is that how I reset the Brush?? What if the value of colors
are lost at that point? As in, say the user selected the
colors from a Color Dialog to set them as colors of Pages.
they could be lost.
Please respond.
thanks,
Veebo
 

Re:TBrush question???

"Veebo" < XXXX@XXXXX.COM >wrote in message
Quote
So, you're saying I need to re-do something like..
After you change the form's Color, you need to re-assign your custom Brush
Color values, yes. Just the Colors, though, as nothing else is being
altered.
Quote
Is that how I reset the Brush?? What if the value
of colors are lost at that point?
Then you will have to find a way to keep or determine the colors somehow.
Quote
As in, say the user selected the colors from a Color
Dialog to set them as colors of Pages. they could be lost.
Then it is your responsibility to store the values somewhere so that you can
retreive them again when needed without re-asking the user each time.
Gambit