我最近更改了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.
}
難道是完全不合理的懷疑某種缺陷躲在裏面' MyFont newFont =(MyFont)logFont;'? – IInspectable
我不認爲這是錯誤,但我會添加代碼相關的MyFont成員,以防我失去一些東西。 – user2779124
什麼是'pLastText'?並且'pMyFigure-> SetFont(newFont,&dc);''這一行沒有意義,因爲'pMyFigure'不是一個指針,基本上沒有足夠的信息來理解你的代碼在做什麼,你正在使用未聲明的變量和undefined類和方法我們不知道他們在做什麼以及它們應該如何使用 –