Board index » cppbuilder » Problems assigning the font at runtime of my control

Problems assigning the font at runtime of my control


2004-04-29 11:42:42 PM
cppbuilder10
Hello,
I 've derived a TColorButton from TCustomControl so far so good. I have a problem setting the Font of the control during runtime. I inserted the statement "Canvas->Font->Assign(Font);
" in the Paint() method. It works during designtime I can change the font params but during runtime it does not react.
The caption is shown with a normal initial font.
Regards
Roland
void __fastcall TColorButton::Paint(void)
{
.......
//Canvas->Font->Style = Font->Style;
nTw = Canvas->TextWidth(FCaption);
nTh = Canvas->TextHeight(FCaption);
Canvas->Font->Assign(Font);
Canvas->Brush = FBrush;
Canvas->TextOutA( nDownDiff+(w - nTw)/2, nDownDiff+(h - nTh)/2,FCaption);
}
 
 

Re:Problems assigning the font at runtime of my control

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

void __fastcall TColorButton::Paint(void)
{
.......


//Canvas->Font->Style = Font->Style;
nTw = Canvas->TextWidth(FCaption);
nTh = Canvas->TextHeight(FCaption);
These should be called after you assign the font, otherwise the calculations
are based on
the original font.
You could also use TextExtent to get both dimensions at once.
Quote
Canvas->Font->Assign(Font);
Where is Font coming from? Are you sure that it has been assigned a value
before this is called?
Also, you may need a Repaint () call after making changes to Font. It
sounds like Font is a property
so you can put the Repaint () into the SetFont () or whatever you call the
write method for it.
Quote
Canvas->Brush = FBrush;
Canvas->TextOutA( nDownDiff+(w - nTw)/2, nDownDiff+(h -
nTh)/2,FCaption);
}
Aloha
Bill
 

Re:Problems assigning the font at runtime of my control

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
Quote
Where is Font coming from?
TCustomControl inherits the Font property from TControl.
Quote
Also, you may need a Repaint () call after making changes to Font.
The VCL already handles that internally for the native Font property.
Quote
It sounds like Font is a property so you can put the Repaint ()
into the SetFont () or whatever you call the write method for it.
If, by chance, a custom property is being created rather than using the
native Font property, then you also need an OnChange event hander for the
TFont object itself. Using a setter method of the Font property itself
won't catch all changes to the Font.
Gambit
 

{smallsort}

Re:Problems assigning the font at runtime of my control

Hello,
it is a little bit mysterious but I did not change something but now I can set the font during runtime. I still use:
Canvas->Font->Assign(Font);
Where Font comes from the inherited control.
Another problem is arisen: when I use this TColorButton at a brandnew (test-)app it works fine. Today I addded one property named FocusColor. Now in the app in which the control have to be inserted an exeption raises like "property FocusColor not found". I recompiled a several times the app and the control. I deleted the *.obj, *.tds files manually but still the same result. In the case when the app crashes I also can not see all the blue dots at the right side of the IDE. Only a few dot are present at some incorrect places.
It seems that the IDE can not refresh or update the used compiled file. When I open the *.dfm file I can see the 'missing' property it is listed correct.
Any idea?
Roland
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Where is Font coming from?

TCustomControl inherits the Font property from TControl.

>Also, you may need a Repaint () call after making changes to Font.

The VCL already handles that internally for the native Font property.

>It sounds like Font is a property so you can put the Repaint ()
>into the SetFont () or whatever you call the write method for it.

If, by chance, a custom property is being created rather than using the
native Font property, then you also need an OnChange event hander for the
TFont object itself. Using a setter method of the Font property itself
won't catch all changes to the Font.


Gambit


 

Re:Problems assigning the font at runtime of my control

"roland" < XXXX@XXXXX.COM >wrote in message
Quote
Today I addded one property named FocusColor. Now in the
app in which the control have to be inserted an exeption raises
like "property FocusColor not found".
The app is probably still trying to use an older version of the component.
Did you actually reinstall the component into the IDE fresh after
recompiling it? That way it could be linking to the updated binary package.
Quote
I recompiled a several times the app and the control. I deleted the
*.obj, *.tds files manually but still the same result. In the case when
the app crashes I also can not see all the blue dots at the right side
of the IDE. Only a few dot are present at some incorrect places.
Which also suggests the code and binaries are out of sync with each other.
Gambit
 

Re:Problems assigning the font at runtime of my control

Hello Remy,
Quote
The app is probably still trying to use an older version of>the component.
Did you actually reinstall the component into the IDE fresh>after recompiling it? That way it could be linking to the>updated>binary package.
Is it really necessary to uninstall and reinstall the component at any time I changed a little part of code?
I have done it and I also deleted the old binarys of both the control and the app to be on the save side and now it runs.
I've just some additional questions:
1)In which way can I install an eventhandler for OnChange of the property 'Enabled' which comes from the inherited control?
2)My control crashes at the OnClick Event if I have no Code in the resp. handler of the app. If I insert some dummy code it runs well.
Thanks for your advise
Roland
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"roland" < XXXX@XXXXX.COM >wrote in message
news:40928c2e$ XXXX@XXXXX.COM ...

>Today I addded one property named FocusColor. Now in the
>app in which the control have to be inserted an exeption raises
>like "property FocusColor not found".

The app is probably still trying to use an older version of the component.
Did you actually reinstall the component into the IDE fresh after
recompiling it? That way it could be linking to the updated binary package.

>I recompiled a several times the app and the control. I deleted the
>*.obj, *.tds files manually but still the same result. In the case when
>the app crashes I also can not see all the blue dots at the right side
>of the IDE. Only a few dot are present at some incorrect places.

Which also suggests the code and binaries are out of sync with each other.


Gambit


 

Re:Problems assigning the font at runtime of my control

"Roland" < XXXX@XXXXX.COM >wrote in message
Quote
Is it really necessary to uninstall and reinstall the component at any
time
I changed a little part of code?
Typically, yes. That is the only sure way to force the IDE to always use
the abslute latest files. If you can verify that you only have one copy of
the binaries on your entire machine, and never change the filenames of them,
then you can usually get away with not reinstalling the package each time.
But when you start making changes that effect the public interface of
classes that the IDE is trying to access, reinstalling is best.
Quote
1)In which way can I install an eventhandler for OnChange of
the property 'Enabled' which comes from the inherited control?
If you are trying to detect when the Enabled property is changed, you need
to intercept the CM_ENABLEDCHANGED message. When the TControl::Enabled
property is changed, the component sends this message back to inself so that
descendant classed can react to the change. To intercept the message, you
can either override the inherited WndProc() method or use a MESSAGE_MAP.
Quote
2)My control crashes at the OnClick Event if I have no Code
in the resp. handler of the app. If I insert some dummy code it
runs well.
Your description is too vague. Please show your actual code.
Gambit
 

Re:Problems assigning the font at runtime of my control

Hello,
Quote
>2)My control crashes at the OnClick Event if I have no Code
>in the resp. handler of the app. If I insert some dummy code it
>runs well.
Your description is too vague. Please show your actual code.
Here is piece of the haeder file. So you can see I do nothing with the OnClick Event.
//.....
public:
__fastcall TColorButton(TComponent* Owner);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_GETDLGCODE, TWMNoParams, WMGetDlgCode);
VCL_MESSAGE_HANDLER(WM_SETFOCUS, TWMNoParams, WMSetFocus);
VCL_MESSAGE_HANDLER(WM_KILLFOCUS, TWMNoParams, WMKillFocus);
VCL_MESSAGE_HANDLER(WM_LBUTTONDOWN, TWMLButtonDown, WMLButtonDown);
VCL_MESSAGE_HANDLER(WM_LBUTTONUP, TWMNoParams, WMLButtonUp);
VCL_MESSAGE_HANDLER(CM_ENABLEDCHANGED, TMessage, CMEnabledChanged );
END_MESSAGE_MAP(TCustomControl );
__published:
__property TColor BackGround = {read=GetBackGroundColor, write=SetBackGroundColor};
__property TColor FocusColor = {read=GetFocusColor, write=SetFocusColor};
__property AnsiString Caption = {read=FCaption, write=SetCaption};
__property bool WantArrowKeys = {read=FbArrowKeys, write=SetArrowKeys};
__property Enabled ;
__property Visible ;
__property Height ;
__property Width ;
__property TabStop ;
__property TabOrder ;
__property Tag ;
__property Font ;
__property Cursor ;
__property OnDblClick ;
__property OnClick ;
__property OnMouseDown ;
__property OnMouseMove ;
__property OnMouseUp ;
__property OnKeyDown ;
__property OnKeyPress ;
__property OnKeyUp ;
__property OnEnter ;
__property OnExit ;
//............
But in my app where I use the control I must insert a dummy like this.
void __fastcall TForm1::ColorButton1Click(TObject *Sender)
{
int i = 0;
}
If I don't do it, the app crashes.
Regards
Roland
 

Re:Problems assigning the font at runtime of my control

"Roland" < XXXX@XXXXX.COM >wrote in message
Quote
Here is piece of the haeder file.
Please show the code that is actually crashing. You probably have a piece
of code that is trying to call the OnClick handler without checking whether
it is NULL or not first.
Gambit