2017-02-26 93 views
0

我最近更改了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. 
} 
+0

難道是完全不合理的懷疑某種缺陷躲在裏面' MyFont newFont =(MyFont)logFont;'? – IInspectable

+0

我不認爲這是錯誤,但我會添加代碼相關的MyFont成員,以防我失去一些東西。 – user2779124

+0

什麼是'pLastText'?並且'pMyFigure-> SetFont(newFont,&dc);''這一行沒有意義,因爲'pMyFigure'不是一個指針,基本上沒有足夠的信息來理解你的代碼在做什麼,你正在使用未聲明的變量和undefined類和方法我們不知道他們在做什麼以及它們應該如何使用 –

回答

0

這是我該怎麼做:

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上閱讀了它指出:

字體的大小,在一個點的十分之一

+0

爲什麼您將GetSize()重新生成的值除以10? – user2779124

+0

好的。我在下面看到您的評論。感謝您的解決方案。 – user2779124

+0

@ user2779124不客氣。如果你點擊鏈接,你可以閱讀更多。 –

相關問題