【问题标题】:How can I change the default CDialog font for a non-modal dialog?如何更改非模式对话框的默认 CDialog 字体?
【发布时间】:2011-03-18 00:16:32
【问题描述】:

必须关闭所有对话框控件的默认字体的“ClearType”属性。可以通过设置为一个控件做到这一点

logfont.lfQuality = ANTIALIASED_QUALITY

有很多建议如何对模态对话框(http://neelaakash.wordpress.com/2007/12/31/change-default-dialog-font-of-cdialog/ 和其他)执行相同的操作,但对于非模态对话框应该这样做(使用 new 和 Create(...) 方法实例化)。我自己也试过这样做:

覆盖'Create'方法,并修改对话框模板:

BOOL CActivationChildDialogLicenseInfo::Create(UINT nIDTemplate, 
                                               CWnd* pParentWnd)
{
  CDialogTemplate dlt;
  int nResult;
  // load dialog template
  if (!dlt.Load(MAKEINTRESOURCE(nIDTemplate))) return -1;
  // set your own font, for example “Arial”, 10 pts.
  dlt.SetFont(L"Arial", 12);
  // 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 = CActivationChildDialog::Create(nIDTemplate, pParentWnd);
  // unlock memory object
  GlobalUnlock(dlt.m_hTemplate);

  return nResult ;
}

似乎这个方法什么都不做(它被称为,我已经通过在里面放置断点进行了检查)。 我试过打电话

nResult = CActivationChildDialog::Create(NULL, pParentWnd);

...但是得到了很多 ASSERT。

我也尝试过覆盖“OnSetFont”方法:

void CActivationChildDialogLicenseInfo::OnSetFont(CFont *pFont)
{
    CActivationChildDialog::OnSetFont(pFont);

    LOGFONT logfont;
    pFont->GetLogFont(&logfont);
    LOGFONT logfont2=logfont;
    pFont->DeleteObject();

    logfont2.lfItalic = true;
    logfont2.lfQuality = ANTIALIASED_QUALITY;
    pFont->CreateFontIndirect(&logfont2);
}

这会在运行时导致 ASSERT 并导致使用非常大的字体(丢失默认字体设置,不接受新的指定设置)...我不知道为什么。

请告知,如何更改将被所有对话框控件“继承”的默认对话框字体?

非常感谢。

【问题讨论】:

    标签: c++ winapi mfc


    【解决方案1】:

    首先:简单、可靠的方法是创建对话框,然后将WM_SETFONT(或调用SetFont())发送到对话框和其中的每个控件。我将在下面向您展示如何做到这一点,但首先,这就是您已经尝试过的两种策略没有(也不能)奏效的原因:

    修改对话框模板

    首先,如果您希望使用已加载的对话框模板,您应该调用 CDialog::CreateIndirect()

    但不要打扰。对话框模板仅包含面名称和点大小 - 它确实允许您指定其他 LOGFONT 值,例如 lfQuality。如果它确实,你可以简单地在你的资源定义中指定它并避免编写任何运行时代码!

    拦截WM_SETFONT

    理论上,您可以完成这项工作。但这不实用。您的代码有几个问题:首先,您必须为每个子控件截取此消息,以便它执行任何有用的操作:对话框本身可能不会呈现任何文本。但更糟糕的是,您将 original 字体传递给基类(将其传递给默认窗口过程,该过程将其存储在内部以供以后使用)然后立即销毁它 - 这意味着对话框(以及使用该字体的所有其他内容,包括所有子控件)将尝试使用虚假字体绘制文本,并因此恢复为默认字体。最后,您正在创建一个附加到由 MFC 创建和销毁的临时对象 (pFont) 的新字体 - 在内部,您正在使用的 CFont 对象将与字体句柄分离并销毁,从而将句柄泄漏到什么都没有使用的字体对象。

    泄漏抽象:关于 HFONT 和 CFont 的说明

    HFONT 是 Windows 用来表示字体对象的句柄类型。像大多数 GDI 一样,有创建字体的特定函数,以及用于销毁它们的通用 DeleteObject() 函数。

    CFont 是 HFONT 的轻量级包装类。 CFont 实例可以与现有的 HFONT 连接和分离,或用于创建新的。如果 CFont 实例在其解构函数执行时仍附加到 HFONT,它将调用DeleteObject() 来销毁底层对象。在内部,MFC 在调用各种消息处理程序(例如 OnSetFont)时使用临时 CFont 实例,这些实例与 HFONT 连接和分离。值得记住的是,在内部,Windows 对 CFont 一无所知,单个 HFONT 在任何给定时间点都可能属于 0 个或多个 CFont 实例。

    关于字体和 WM_SETFONT 的说明

    当您创建新字体时 - 无论它是否包含在 CFont 对象中 - 您都是该字体的所有者,您有责任在使用完毕后销毁它。 Passing it to WM_SETFONT (CWnd::SetFont()) doesn't change ownership! 这实际上非常有用,因为它允许您将相同的字体传递给多个窗口,而不必担心哪个窗口会破坏它 - 您仍然是所有者,因此您可以(并且必须)自己破坏它(一次没有窗口仍在使用它)。

    最后——如何在对话框及其所有子项上快速创建和设置字体

    所以你现在应该有足够的背景来理解必要的步骤:

    1. 创建对话框
    2. 创建所需的字体
    3. 将字体传递给对话框及其子对话框(通过发送 WM_SETFONT 消息,或通过调用 CWnd::SetFont... 本身发送 WM_SETFONT 消息)。
    4. 当对话框被销毁时,也要销毁你的字体。

    示例

    // define this as a class member - class destructor then handles step four!
    CFont m_nonCleartypeFont; 
    
    BOOL CActivationChildDialogLicenseInfo::Create(UINT nIDTemplate, 
                                                   CWnd* pParentWnd)
    {
      // step one: create dialog normally
      BOOL nResult = CActivationChildDialog::Create(nIDTemplate, pParentWnd);
    
      // step two: create custom font
      // relying on destructor to destroy font once we're done with it
      // so be careful to only create it once!
      if ( NULL == m_nonCleartypeFont.m_hObject )
      {
        CFont* pOriginalFont = GetFont(); // use template font as... template!
        
        // pull information from original font  
        LOGFONT logfont;
        pOriginalFont->GetLogFont(&logfont);
    
        // make font adjustments: 
        // specify italics
        logfont.lfItalic = true;
        // and non-cleartype antialiasing
        logfont.lfQuality = ANTIALIASED_QUALITY;
        
        // create our font based on adjusted information
        m_nonCleartypeFont.CreateFontIndirect(&logfont);
      } 
      
      // step three: set our custom font on the dialog and all children
      @987654326@(&m_nonCleartypeFont, FALSE);
      // Send message to quickly set this font for all children. 
      // See documentation for @987654327@
      // - this is actually the example given!
      @987654328@(@987654329@,
          (WPARAM)m_nonCleartypeFont.m_hObject,
          MAKELONG(FALSE, 0), 
          FALSE);
      
      return nResult;
    }

    【讨论】:

    • 我想添加一条评论,有时 submodals 没有得到这个“设置给它们”,我不知道为什么 - 一个简单的“看到”的方法是通过在调试构建,然后查看所有未设置斜体的对话框
    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多