changing font in CEdit
-
Can anyone please explain me how to change font in CEdit. I tried with CWnd::SetFont() but it doesn't work.
You need to ensure that the font exists for the life of the CEdit control. Suppose you're changing the font in the OnInitDialog() handler for your class:
BOOL MyDialog::OnInitDialog()
{
//...
CFont myFont;
myFont.CreatePointFont(120,"Arial");
myEdit.SetFont(&myFont);
}This won't work, because as soon as the OnInitDialog function exits, the myFont variable is destroyed, which destroys the font and the edit control reverts to using the standard font. You fix this by making the myFont value a member of the dialog:
class MyDialog : public CDialog
{
//...
CFont myFont;
};
BOOL MyDialog::OnInitDialog()
{
//...
myFont.CreatePointFont(120,"Arial");
myEdit.SetFont(&myFont);
}Since the myFont variable is a member of the dialog class, it exists for as long as the dialog object, and therefore the edit control.
Software Zen:
delete this;
-
You need to ensure that the font exists for the life of the CEdit control. Suppose you're changing the font in the OnInitDialog() handler for your class:
BOOL MyDialog::OnInitDialog()
{
//...
CFont myFont;
myFont.CreatePointFont(120,"Arial");
myEdit.SetFont(&myFont);
}This won't work, because as soon as the OnInitDialog function exits, the myFont variable is destroyed, which destroys the font and the edit control reverts to using the standard font. You fix this by making the myFont value a member of the dialog:
class MyDialog : public CDialog
{
//...
CFont myFont;
};
BOOL MyDialog::OnInitDialog()
{
//...
myFont.CreatePointFont(120,"Arial");
myEdit.SetFont(&myFont);
}Since the myFont variable is a member of the dialog class, it exists for as long as the dialog object, and therefore the edit control.
Software Zen:
delete this;