CFontDialog font size - Need Help!!!!!
-
In the OnBeginPrinting function of my view class I am displaying a CFontDialog dialog box. I want the default font to be Arial and the default size to be 8. When the dialog box displays, it a shows default size of 50. Here is the code. These are the first lines in the OnBeginPrinting function.
ASSERT(pDC->GetMapMode() == MM_TEXT);
LOGFONT lf;
::ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = -MulDiv(10, pDC->GetDeviceCaps(LOGPIXELSY), 72);
lstrcpy(lf.lfFaceName, _T("Arial"));
CFontDialog dlg(&lf, CF_PRINTERFONTS, pDC);
if (dlg.DoModal() == IDOK)
{
m_strFont = dlg.GetFaceName();
m_nPointSize = dlg.GetSize() / 10;
}The value returned by pDC->GetDeviceCaps(LOGPIXELSY) is 600. The computed value for lf.lfHeight when 8 points is used is -67. The resulting size value in the Select Font dialog is 50. Why is the Select Font dialog displaying as font size of 50 instead of 8? Here are the results from some other point sizes. input dialog box 8 50 10 62 12 75 14 88 20 125 Every example of code I can find looks just like the above. Why is this code not working? Thanks!
-
In the OnBeginPrinting function of my view class I am displaying a CFontDialog dialog box. I want the default font to be Arial and the default size to be 8. When the dialog box displays, it a shows default size of 50. Here is the code. These are the first lines in the OnBeginPrinting function.
ASSERT(pDC->GetMapMode() == MM_TEXT);
LOGFONT lf;
::ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = -MulDiv(10, pDC->GetDeviceCaps(LOGPIXELSY), 72);
lstrcpy(lf.lfFaceName, _T("Arial"));
CFontDialog dlg(&lf, CF_PRINTERFONTS, pDC);
if (dlg.DoModal() == IDOK)
{
m_strFont = dlg.GetFaceName();
m_nPointSize = dlg.GetSize() / 10;
}The value returned by pDC->GetDeviceCaps(LOGPIXELSY) is 600. The computed value for lf.lfHeight when 8 points is used is -67. The resulting size value in the Select Font dialog is 50. Why is the Select Font dialog displaying as font size of 50 instead of 8? Here are the results from some other point sizes. input dialog box 8 50 10 62 12 75 14 88 20 125 Every example of code I can find looks just like the above. Why is this code not working? Thanks!
Why do you think it is not working? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
In the OnBeginPrinting function of my view class I am displaying a CFontDialog dialog box. I want the default font to be Arial and the default size to be 8. When the dialog box displays, it a shows default size of 50. Here is the code. These are the first lines in the OnBeginPrinting function.
ASSERT(pDC->GetMapMode() == MM_TEXT);
LOGFONT lf;
::ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = -MulDiv(10, pDC->GetDeviceCaps(LOGPIXELSY), 72);
lstrcpy(lf.lfFaceName, _T("Arial"));
CFontDialog dlg(&lf, CF_PRINTERFONTS, pDC);
if (dlg.DoModal() == IDOK)
{
m_strFont = dlg.GetFaceName();
m_nPointSize = dlg.GetSize() / 10;
}The value returned by pDC->GetDeviceCaps(LOGPIXELSY) is 600. The computed value for lf.lfHeight when 8 points is used is -67. The resulting size value in the Select Font dialog is 50. Why is the Select Font dialog displaying as font size of 50 instead of 8? Here are the results from some other point sizes. input dialog box 8 50 10 62 12 75 14 88 20 125 Every example of code I can find looks just like the above. Why is this code not working? Thanks!
I have found the answer. lfHeight must be converted from print units to screen units because the CFontDialog class uses screen units. Here is the updated code:
ASSERT(pDC->GetMapMode() == MM_TEXT);
LOGFONT lf;
::ZeroMemory(&lf, sizeof(LOGFONT));
int nPrintDpi = pDC->GetDeviceCaps(LOGPIXELSY);
lf.lfHeight = -MulDiv(10, pDC->GetDeviceCaps(LOGPIXELSY), 72);// Convert lfHeight from print units to screen units
// because the CFontDialog class uses Screen units
HDC hDC = GetDC(m_pView->m_hWnd);
int nScreenDpi = GetDeviceCaps(hDC, LOGPIXELSY);
lf.lfHeight = MulDiv(lf.lfHeight, nScreenDpi, nPrintDpi);lstrcpy(lf.lfFaceName, _T("Arial"));
CFontDialog dlg(&lf, CF_PRINTERFONTS, pDC);
if (dlg.DoModal() == IDOK)
{
m_strFont = dlg.GetFaceName();
m_nPointSize = dlg.GetSize() / 10;
}