is it possible to set a cfont for all variables of each class used in
a MFC project?
class CMIAppView : public CFormView // CView
{
CFont m_font;
}
void CMIAppView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
m_font.CreatePointFont();
}
best regards
Hans
That's a bit of a vague question.
>class CMIAppView : public CFormView // CView
>{
>CFont m_font;
>
>}
>
>
>void CMIAppView::OnInitialUpdate()
>{
>CFormView::OnInitialUpdate();
>
>m_font.CreatePointFont();
>}
It looks like you want all the child controls of your CFormView
derived class to use the same font?
Try using:
SendMessageToDescendants( WM_SETFONT,
(WPARAM) m_Font.m_hObject );
Dave
thanks for your answer. I placed this function call in the mainframe
of my sdi application. Unfortunately, if I create a new wizard with
some dialogs (in the mainframe.cpp), the font change won`t work for
these dialogs.
dlgStartWiz.AddPage(&Page1, Page2::IDD);
dlgStartWiz.AddPage(&Page2, Page2::IDD);
dlgStartWiz.DoModal();
Do you know which steps I have to do get this working?
>Do you know which steps I have to do get this working?
You'd need to arrange for it to be done when each dialog (wizard page)
is initialised - you'd normally do it in WM_INITDIALOG (OnInitDialog)
processing.
Dave
If I got you right, then I have to setup the font for each dialog
separatly in the specific OnInitDialog? And it is not enough to do
that in the mainframe (OnCreate-method)?
Yes, that's correct.
Dave
ok thanks for your help. It`s working now, but the font is very grainy
(pixelated). Are there any recommendations??? Here are my font
settings
LOGFONT lF;
lF.lfHeight = 32;
lF.lfWidth = 0;
lF.lfWeight = FW_NORMAL;
lF.lfItalic = FALSE; //TRUE;
lF.lfUnderline = FALSE; //TRUE;
lF.lfStrikeOut = FALSE; //TRUE;
lF.lfEscapement = 0;
lF.lfOrientation = 0;
wcscpy(lF.lfFaceName,_T("Verdana"));
m_font.CreateFontIndirect(&lF);
BOOL fRedraw = TRUE;
SendMessageToDescendants( WM_SETFONT,(WPARAM) m_font.m_hObject );
best regards
Hans
Or is it much better to use the default system font?
I don't know why that should be - what does the same sized Verdana
font look like in other applications on your PC?
>Are there any recommendations?
In the LOGFONT, leave everything at 0 and just specify the minimum
attributes that you want - usually just the height and face name.
>Or is it much better to use the default system font?
I'd certainly recommend sticking with the system font. The only times
I've ever needed to use the technique of changing a dialog control's
font is perhaps to make some text bold.
Dave
>Hi,
>
>is it possible to set a cfont for all variables of each class used in
>a MFC project?
****
Variables are concept of the C and C++ language, and as such, do not have fonts.
****
>
>class CMIAppView : public CFormView // CView
>{
>CFont m_font;
>
>}
>
>
>void CMIAppView::OnInitialUpdate()
>{
>CFormView::OnInitialUpdate();
>
>m_font.CreatePointFont();
****
OK, so you've gotten a font. Now you have to apply it. Why not specify the font at
design time to the dialog editor?
Seriously, all you have to do to set the font for each control is find each control and
call SetFont on it. And hope that the control is large enough to show the text. How are
you dealing with making sure the controls are all the right size? That's much harder.
joe
****
>}
>
>
>best regards
>Hans
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
>On 13 Aug., 08:19, David Lowndes <Dav...@example.invalid> wrote:
>> >is it possible to set a cfont for all variables of each class used in
>> >a MFC project?
>>
>> That's a bit of a vague question.
>>
>> >class CMIAppView : public CFormView // CView
>> >{
>> >CFont m_font;
>>
>> >}
>>
>> >void CMIAppView::OnInitialUpdate()
>> >{
>> >CFormView::OnInitialUpdate();
>>
>> >m_font.CreatePointFont();
>> >}
>>
>> It looks like you want all the child controls of your CFormView
>> derived class to use the same font?
>>
>> Try using:
>>
>> SendMessageToDescendants( WM_SETFONT,
>> (WPARAM) m_Font.m_hObject );
>>
>> Dave
>
>thanks for your answer. I placed this function call in the mainframe
>of my sdi application. Unfortunately, if I create a new wizard with
>some dialogs (in the mainframe.cpp), the font change won`t work for
>these dialogs.
****
I have no idea what you mean by "create a new wizard with some dialogs" since all dialogs
are created by the dialog editor, and no wizards are involved. What is it you are trying
to ask?
*****
>
>
>dlgStartWiz.AddPage(&Page1, Page2::IDD);
>dlgStartWiz.AddPage(&Page2, Page2::IDD);
>dlgStartWiz.DoModal();
*****
You need to set all the controls in each of the pages. One way to do this is have the
OnInitDialog handler of the page ask its parent for a font handle (typically by doing
something like
HFONT font = (HFONT)GetParent()->SendMessage(UWM_QUERY_FONT);
and write a handler for the user-defined UWM_QUERY_FONT message that returns the HFONT.
You can then set the font in each of the child controls with a trivial loop. Of course,
you have to make sure the font works (is not too large for the controls), Which is why
you should do it in the dialog editor. Note that you cannot set the font in the controls
before the OnInitDialog handler of each page because the controls do not exist until that
point.
****
>
>Do you know which steps I have to do get this working?
*****
(a) make sure you know how to resize the controls so they can handle the font
(b) each page sets the font of each of its controls
(c) no page knows about any way to ask what the font is besides sending a message to its
parent.
joe
*****
thanks for your answer. Unfortunately, if I try to change the font in
the resource editor to (font: Verdana, font-size: 16pt).
After starting the app, the font looks good, but if I hover over one
of my radiobuttons (the font will change the color from white to
blue), the font looks terrible. Some parts of the font is blue, some
is white....
Even if I resize the control, so that the text will be able to fit in
twice.
Sounds like your font has ceased to exist - you need to ensure that
the font object exists for the lifetime of any controls that use it.
Dave
I`ve installed a public font object (CFont m_font;) in the specific
class; moreover I get the same error if I set the font using the
resource editor
You've got something odd happening in your code - quite what though I
don't know.
I suggest that you try an example project with just the font change
code in to reassure yourself that it's not the issue.
Dave
I got the error which was in my code;
By the way, a short question: if I want to use
SendMessageToDescendants( WM_SETFONT,(WPARAM) m_font.m_hObject );
for nearly every item in a dialog; how is it possible to define a new
font which should be only applied to some items?
When I use the same definition (only resize the font) and add the font
SetFont() to an item, nothing happens. If I don`t use
SendMessageToDescendants() the font will be shown as expected...
One short example:
OnInitDialog()
{
LOGFONT lF;
lF.lfHeight = 40;
lF.lfWidth = 0;
lF.lfWeight = FW_NORMAL;
lF.lfItalic = FALSE; //TRUE;
lF.lfUnderline = FALSE; //TRUE;
lF.lfStrikeOut = FALSE; //TRUE;
lF.lfEscapement = 0;
lF.lfOrientation = 0;
wcscpy(lF.lfFaceName,_T("Verdana"));
m_Statfont.CreateFontIndirect(&lF);
m_StatRefL.SetFont(&m_Statfont);
}
m_Statfont is a public member of the class.
best regards
Hans
Jolly good.
>By the way, a short question: if I want to use
>
>SendMessageToDescendants( WM_SETFONT,(WPARAM) m_font.m_hObject );
>
>for nearly every item in a dialog; how is it possible to define a new
>font which should be only applied to some items?
It isn't - you'd have to use a more selective approach and send the
WM_SETFONT message to only those controls you want to alter.
>When I use the same definition (only resize the font) and add the font
>SetFont() to an item, nothing happens. If I don`t use
>SendMessageToDescendants() the font will be shown as expected...
>
>One short example:
>
>OnInitDialog()
>{
>LOGFONT lF;
>lF.lfHeight = 40;
>lF.lfWidth = 0;
>lF.lfWeight = FW_NORMAL;
>lF.lfItalic = FALSE; //TRUE;
>lF.lfUnderline = FALSE; //TRUE;
>lF.lfStrikeOut = FALSE; //TRUE;
>lF.lfEscapement = 0;
>lF.lfOrientation = 0;
>wcscpy(lF.lfFaceName,_T("Verdana"));
>m_Statfont.CreateFontIndirect(&lF);
>
>m_StatRefL.SetFont(&m_Statfont);
>}
>
>m_Statfont is a public member of the class.
From what you've mentioned, it should work as far as I can see.
What is m_StatRefL ?
Dave
How is it possible to select all cstatic, cbuttons items in one
WM_SETFONT message?
> What is m_StatRefL ?
CStatic m_StatRefL;
You can't do it by magic. I tried to indicate that you'll have to
write code to do precisely what you want.
Dave
On Mon, 16 Aug 2010 08:51:34 -0700 (PDT), mfc <mfc...@googlemail.com> wrote:
>On 16 Aug., 13:17, David Lowndes <Dav...@example.invalid> wrote:
>> >I`ve installed a public font object (CFont m_font;) in the specific
>> >class; moreover I get the same error if I set the font using the
>> >resource editor
>>
>> You've got something odd happening in your code - quite what though I
>> don't know.
>>
>> I suggest that you try an example project with just the font change
>> code in to reassure yourself that it's not the issue.
>>
>> Dave
>
>I got the error which was in my code;
>
>By the way, a short question: if I want to use
>
>SendMessageToDescendants( WM_SETFONT,(WPARAM) m_font.m_hObject );
>
>for nearly every item in a dialog; how is it possible to define a new
>font which should be only applied to some items?
****
This would not be a good idea to do SendMessageToDescendants. All kinds of controls that
you may not want to change may change. Instead, write a loop
for(CWnd * w = GetWindow(GW_FIRST); w != NULL; w = w->GetWindow(GW_HWNDNEXT))
{
if(...whatever condition...)
w->SetFont(&m_font);
}
or whatever (exclusion vs. inclusion testing is your choice)
You have to know what windows you want to set; you can use GetWindowClass to handle
(include or exclude) all windows of a certain type (e.g., set the font for all edit
windows).
>
>When I use the same definition (only resize the font) and add the font
>SetFont() to an item, nothing happens. If I don`t use
>SendMessageToDescendants() the font will be shown as expected...
>
>One short example:
>
>OnInitDialog()
>{
>LOGFONT lF;
>lF.lfHeight = 40;
>lF.lfWidth = 0;
>lF.lfWeight = FW_NORMAL;
>lF.lfItalic = FALSE; //TRUE;
>lF.lfUnderline = FALSE; //TRUE;
>lF.lfStrikeOut = FALSE; //TRUE;
>lF.lfEscapement = 0;
>lF.lfOrientation = 0;
>wcscpy(lF.lfFaceName,_T("Verdana"));
>m_Statfont.CreateFontIndirect(&lF);
****
Did you check to see if this succeeded? I see no test here for success, which means that
you have no idea if it actually created the font.
****
>
>m_StatRefL.SetFont(&m_Statfont);
>}
>
>m_Statfont is a public member of the class.
****
There is no reason it needs to be a public member, because nobody outside the class needs
to use it, so this would be erroneous.
joe
****
>
>best regards
>Hans
****
There is no possible way this could ever be done. You can send a message to each control.
You could just create member control variables for each control and call the SetFont
method, or you could use the loop I showed in an earlier response. You have to do one
SetFont for each control.
****
>
>> What is m_StatRefL ?
>
>CStatic m_StatRefL;
***
Actually, this is not sufficient information; some forms of static controls do not have
font capability, for example, frame controls and rectangle controls. Only if they are
text static controls will SetFont work.
joe
how is it possible to acchieve this? After creating the font in the
mainframe, I`m not able to get the font handle by a SendMessage()?
[OnCreate() mainframe]
SendMessage(WM_SETFONT, (WPARAM)(HFONT)(m_font.m_hObject), 1);
class CMainFrame : public CFrameWnd
{
CFont m_font;
}
[OnInitDialog() CFormView]
CFont *pFont = GetParent()->GetFont();
The window is available and correct (CMainFrame), but GetFont() always
returns zero.
best regards
Hans
ON_REGISTERED_MESSAGE(UWM_QUERY_FONT, OnQueryFont)
LRESULT CMainFrame::OnQueryFont(WPARAM, LPARAM)
{
return (LRESULT)(HFONT)m_Font;
}
HFONT f = (HFONT)GetParent()->SendMessage(UWM_QUERY_FONT);
CFont font;
font.Attach(f);
....
font.Detach();
This assumes that the parent of the window is the class that has the font information and
the handler. Otherwise, replace GetParent() by an appropriate CWnd*.
joe