Changing Dialog Font Programmatically
-
Hi, I am trying to change the dialog font programmatically. The dialog right now is created using a Resource file, and the font size and face is hard-coded. I would like to be able to change the font face and size in the OnInitDialog() function. So far this is what I have: HFONT hFont; LOGFONT lf; CFont pFont; short int fRedraw= FALSE; memset(&lf, 0, sizeof(LOGFONT)); hFont = (HFONT) GetStockObject( SYSTEM_FONT ); GetObject(hFont, sizeof(lf), &lf); pFont.CreateFontIndirect(&lf); this->SetFont(&pFont, true); SendMessageToDescendants(WM_SETFONT, (WPARAM)pFont.m_hObject, MAKELONG ((WORD) fRedraw, 0), TRUE); This is changing the font size and face for the CONTROLS of the dialog. However, it does not change anything elose besides that. I tried to do a this->SetFont, however, this does not help. I need the dialog itself to be resized accordingly, and also regular CText. When the font is changed in the resource file, I notice that the dialog size itself changes, however, this is not happening when I call SetFont or send a WM_SETFONT message. Can anyone help me? Thanks!!!
-
Hi, I am trying to change the dialog font programmatically. The dialog right now is created using a Resource file, and the font size and face is hard-coded. I would like to be able to change the font face and size in the OnInitDialog() function. So far this is what I have: HFONT hFont; LOGFONT lf; CFont pFont; short int fRedraw= FALSE; memset(&lf, 0, sizeof(LOGFONT)); hFont = (HFONT) GetStockObject( SYSTEM_FONT ); GetObject(hFont, sizeof(lf), &lf); pFont.CreateFontIndirect(&lf); this->SetFont(&pFont, true); SendMessageToDescendants(WM_SETFONT, (WPARAM)pFont.m_hObject, MAKELONG ((WORD) fRedraw, 0), TRUE); This is changing the font size and face for the CONTROLS of the dialog. However, it does not change anything elose besides that. I tried to do a this->SetFont, however, this does not help. I need the dialog itself to be resized accordingly, and also regular CText. When the font is changed in the resource file, I notice that the dialog size itself changes, however, this is not happening when I call SetFont or send a WM_SETFONT message. Can anyone help me? Thanks!!!
-
Do you want to change the main dialog of a dialog based app or a dialog that is part of a doc view app? Do you want to change the dialog after it is already created or have a different font at creation depending a factor such as screen resolution, etc.?
I've found the answer: int CSimpleDialog::DoModal() { CDialogTemplate dlt; int nResult; // load dialog template if (!dlt.Load(MAKEINTRESOURCE(CSimpleDialog::IDD))) return -1; // set your own font, for example "Arial", 10 pts. dlt.SetFont("Arial", 10); // get pointer to the modified dialog template LPSTR pdata = (LPSTR)GlobalLock(dlt.m_hTemplate); // let MFC know that you are using your own template m_lpszTemplateName = NULL; InitModalIndirect(pdata); // display dialog box nResult = CDialog::DoModal(); // unlock memory object GlobalUnlock(dlt.m_hTemplate); return nResult; }