什么是使用CFontDialog公共对话框的正确方法?
我最近更改了MFC应用程序的窗口范围和视图端口之间的关系,此后,每次更改我的应用程序字体大小时,即使选择了所有字体中最小的字体。 (编辑:我注意到,CFontDialog::GetSize()
返回的大小是在对话框中选择的大小的十倍。这是一种常见的行为吗?如果不是什么可以使对话返回这样的值?虽然我不确定,但它似乎这个乘法的大小似乎是我的问题。我怎么得到CFontDialog::GetSize()
返回实际选定的大小,如果确实是问题?)什么是使用CFontDialog公共对话框的正确方法?
我做错了什么?什么是使用CFontDialog
的正确方法?
下一个显示的是字体更改代码的一个片段:
CClientDC dc(pView);
pView->OnPrepareDC(&dc)
pLastFont = pLastText->GetFont();
oldColor = pLastText->GetColor();
LOGFONT logFont = (LOGFONT) (*pLastFont);
CFontDialog fontDialog(&logFont);
CSize szPrevSize;
//Some missing codes
MyFigure *pMyFigure;
if(dynamic_cast<MyTextFigure*>(pMyFigure) != NULL)
{
if(dynamic_cast<MyTextBoxFigure*>(pLastText) != NULL)
{
MyTextBoxFigure *pTextBox = dynamic_cast<MyTextBoxFigure*>(pLastText);
szPrevSize = pTextBox->GetTextSize();
}
}
else if(dynamic_cast<MyTableFigure*>(pMyFigure) != NULL)
{
MyTableFigure *pTableFigure = dynamic_cast<MyTableFigure*>(pMyFigure);
TCell *pCell = (TCell *)*pTableFigure;
szPrevSize = pCell->GetTextSize();
}
fontDialog.m_cf.rgbColors = (COLORREF) oldColor;
if (fontDialog.DoModal() == IDOK)
{
fontDialog.GetCurrentFont(&logFont);
MyFont newFont = (MyFont) logFont;
MyColor newColor = (MyColor) fontDialog.GetColor();
pMyFigure->SetFont(newFont, &dc);
pMyFigure->SetColor(newColor);
}
请注意,MyFont
围绕LOGFONT
结构的包装和具有能够使转换为LOGFONT
操作。另请注意,MyFigure
类的成员函数SetFont
设置了一个MyFont
数据成员。
以下代码显示了如何设置视图端口和窗口范围之间的关系。
void CDisplayView::OnInitialUpdate()
{
CRect rcClient;
GetClientRect(&rcClient);
CClientDC dc(this);
dc.SetMapMode(MM_ISOTROPIC);
CSize szWindow(m_pAppDoc->GetZoomRatio() * SCALE_RATIO * dc.GetDeviceCaps(HORZRES),m_pAppDoc->GetZoomRatio() * SCALE_RATIO * dc.GetDeviceCaps(VERTRES));
CSize szViewport(dc.GetDeviceCaps(HORZRES),dc.GetDeviceCaps(VERTRES));
dc.SetWindowExt(szWindow);
dc.SetViewportExt(szViewport);
dc.DPtoLP(&rcClient);
//And so on
}
ZoomRatio
是1,SCALE_RATIO
是1.2
赋值运算符是:
MyFont& MyFont::operator=(const MyFont& font)
{
if (this != &font)
{
m_logFont = font.m_logFont;
}
return *this;
}
P.S. 代码的摘要是:
LOGFONT OldlogFont ;
LOGFONT newLogFont;
COLLORREF OldColorref;
COLORREF newColorref;
CFontDialog fontDialog(&OldlogFont);
fontDialog.m_cf.rgbColors = oldColorref;
if (fontDialog.DoModal() == IDOK)
{
fontDialog.GetCurrentFont(&OldlogFont);
newLogFont = OldlogFont;
newColorref = fontDialog.GetColor();
}
这里的重点是LOGFONT
结构。 用户选择的值由旧的logfont获得,然后分配给新的logfont。
出于调试目的,我检索所选字体的大小和震惊地发现,这是十次,我吃午饭了CFontDialog
用在我的应用程序后,选择大小即
LOGFONT OldlogFont ;
LOGFONT newLogFont;
COLLORREF OldColorref;
COLORREF newColorref;
CFontDialog fontDialog(&OldlogFont);
fontDialog.m_cf.rgbColors = oldColorref;
if (fontDialog.DoModal() == IDOK)
{
fontDialog.GetCurrentFont(&OldlogFont);
newLogFont = OldlogFont;
newColorref = fontDialog.GetColor();
int iFontSize = fontDialog.GetSize();
//when I selected a font size of 10 from the dialog, what I get here in my code is 100.
}
这是我该怎么做:
void CExportSettingsDlg::OnBtnSelectFont()
{
CClientDC dc(this);
m_plfCurrentFont->lfHeight =
-MulDiv(m_plfCurrentFont->lfHeight, dc.GetDeviceCaps(LOGPIXELSY), 72);
CMyFontDialog dlgFont(m_plfCurrentFont);
dlgFont.m_cf.Flags |= CF_NOSCRIPTSEL;
dlgFont.m_cf.rgbColors = m_crCurrentFontColour;
if(dlgFont.DoModal() == IDOK)
{
m_plfCurrentFont->lfHeight = -(dlgFont.GetSize()/10);
// We must retrieve the font colour into the correct variable.
// The logfont is already correct because we passed in the
// logfont pointer into the font dialogue.
switch(m_iFontType)
{
case FONT_TITLE:
m_sSettings.crTitleFontColour = dlgFont.GetColor();
break;
case FONT_HEADING:
m_sSettings.crHeadingFontColour = dlgFont.GetColor();
break;
case FONT_GENERAL:
m_sSettings.crGeneralFontColour = dlgFont.GetColor();
break;
case FONT_EVENT:
m_sSettings.crEventFontColour = dlgFont.GetColor();
break;
case FONT_NOTES:
m_sSettings.crNotesFontColour = dlgFont.GetColor();
break;
}
// Update display
SetSampleFont();
// Html Preview
UpdatePreviewHtml();
}
}
如果它不回答你的问题,我会删除答案。请注意,我如何在代码的开头设置字体大小。
如果您GetSize在MSDN上阅读了它指出:
字体的大小,在一个点的十分之一
为什么您将GetSize()重新生成的值除以10? – user2779124
好的。我在下面看到您的评论。感谢您的解决方案。 – user2779124
@ user2779124不客气。如果你点击链接,你可以阅读更多。 –
难道是完全不合理的怀疑某种缺陷躲在里面' MyFont newFont =(MyFont)logFont;'? – IInspectable
我不认为这是错误,但我会添加代码相关的MyFont成员,以防我失去一些东西。 – user2779124
什么是'pLastText'?并且'pMyFigure-> SetFont(newFont,&dc);''这一行没有意义,因为'pMyFigure'不是一个指针,基本上没有足够的信息来理解你的代码在做什么,你正在使用未声明的变量和undefined类和方法我们不知道他们在做什么以及它们应该如何使用 –