Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:43:50+00:00 2026-06-17T11:43:50+00:00

Say, if I’m creating a dialog window from a resource in a C++/MFC project

  • 0

Say, if I’m creating a dialog window from a resource in a C++/MFC project with Visual Studio, I can change the dialog box font and size from the resource editor. My question is how to do the same from a program?

Here’s a couple of screenshots:

Regular size:
enter image description here

Size 14:
enter image description here

PS. I can imagine that once the dialog window is created one won’t be able to change the font size, but what about before it’s created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:43:51+00:00Added an answer on June 17, 2026 at 11:43 am

    Wow, I had no idea that it’s so complicated. Here’s the solution I came up with to change font size and font face. It works for any dialog without resizing of individual dialog controls:

    enter image description here

    For MFC project:

    //Header .h file
    static INT_PTR OpenDialogWithFont(CWnd* pParentWnd, LPCTSTR lpszResourceID, LPCTSTR pstrFontFaceName = NULL, WORD wFontPtSz = 0, BOOL* pbOutResultFontApplied = NULL);
    static BYTE* AdvanceThrough_sz_Or_Ord(BYTE* pData);
    static BYTE* AdvanceThrough_String(BYTE* pData, CString* pOutStr = NULL);
    

    Then the implementation itself:

    INT_PTR OpenDialogWithFont(CWnd* pParentWnd, LPCTSTR lpszResourceID, LPCTSTR pstrFontFaceName, WORD wFontPtSz, BOOL* pbOutResultFontApplied)
    {
        //Open dialog box with the 'lpszResourceID'
        //'pParentWnd' = parent window class
        //'pstrFontFaceName' = Font face name to use, or NULL to use original font
        //'wFontPtSz' = point size of the font, or 0 to use original font size
        //'pbOutResultFontApplied' = if not NULL, receives TRUE if font was applied, or FALSE if dialog was shown with original font
        //RETURN:
        //      = One of the values returned by CDialog::DoModal
        INT_PTR nResDlg = -1;
    
        BOOL bAppliedFont = FALSE;
        BYTE* pCNewData = NULL;
    
        LPCTSTR m_lpszTemplateName = MAKEINTRESOURCE(lpszResourceID);
        HINSTANCE hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
        if(hInst)
        {
            HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
            HGLOBAL hDialogTemplate = LoadResource(hInst, hResource);
            if(hDialogTemplate)
            {
                LPCDLGTEMPLATE lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
                DWORD dwszDialogTemplate = SizeofResource(hInst, hResource);
                if(lpDialogTemplate &&
                    dwszDialogTemplate)
                {
                    //Template to use
                    LPCDLGTEMPLATE lpDialogTemplateToUse = lpDialogTemplate;
    
                    //See if it's an extended dialog structure
                    DLGTEMPLATEEX_PART1* pDTX1 = (DLGTEMPLATEEX_PART1*)lpDialogTemplate;
                    if(pDTX1->signature == 0xFFFF &&
                        pDTX1->dlgVer == 1)
                    {
                        //Now get thru variable length elements
                        BYTE* pData = (BYTE*)(pDTX1 + 1);
    
                        //sz_Or_Ord menu;
                        pData = AdvanceThrough_sz_Or_Ord(pData);
    
                        //sz_Or_Ord windowClass;
                        pData = AdvanceThrough_sz_Or_Ord(pData);
    
                        //title
                        CString strTitle;
                        pData = AdvanceThrough_String(pData, &strTitle);
    
                        //Now pointsize of the font
                        //This member is present only if the style member specifies DS_SETFONT or DS_SHELLFONT.
                        if(pDTX1->style & (DS_SETFONT | DS_SHELLFONT))
                        {
                            //Font size in pts
                            BYTE* pPtr_FontSize = pData;
                            WORD ptFontSize = *(WORD*)pData;
                            pData += sizeof(WORD);
    
                            WORD wFontWeight = *(WORD*)pData;
                            pData += sizeof(WORD);
    
                            BYTE italic = *(BYTE*)pData;
                            pData += sizeof(BYTE);
    
                            BYTE charset = *(BYTE*)pData;
                            pData += sizeof(BYTE);
    
                            //Font face name
                            CString strFontFaceName;
                            BYTE* pPtr_FontFaceName = pData;
                            pData = AdvanceThrough_String(pData, &strFontFaceName);
    
                            //Remember the end of the struct (that we care about)
                            BYTE* pPtr_EndStruct = pData;
    
                            //Get size of the end data chunk
                            int ncbszEndChunk = dwszDialogTemplate - (pPtr_EndStruct - (BYTE*)lpDialogTemplate);
                            if(ncbszEndChunk >= 0)
                            {
                                //Now we can modify the struct
    
                                //Get new type face name (or use the old one)
                                CString strNewFontFaceName = pstrFontFaceName ? pstrFontFaceName : strFontFaceName;
    
                                //Calculate the new struct size
                                int ncbSzNewData = dwszDialogTemplate - 
                                    strFontFaceName.GetLength() * sizeof(WCHAR) + 
                                    strNewFontFaceName.GetLength() * sizeof(WCHAR);
    
                                //Reserve mem
                                pCNewData = new BYTE[ncbSzNewData];
                                if(pCNewData)
                                {
                                    BYTE* pNewData = pCNewData;
    
                                    //Copy in chunks
                                    memcpy(pNewData, lpDialogTemplate, pPtr_FontFaceName - (BYTE*)lpDialogTemplate);
                                    pNewData += pPtr_FontFaceName - (BYTE*)lpDialogTemplate;
    
                                    //Then put our font face name
                                    memcpy(pNewData, strNewFontFaceName.GetString(), (strNewFontFaceName.GetLength() + 1) * sizeof(WCHAR));
                                    pNewData += (strNewFontFaceName.GetLength() + 1) * sizeof(WCHAR);
    
                                    //And add the ending chunk
                                    memcpy(pNewData, pPtr_EndStruct, ncbszEndChunk);
                                    pNewData += ncbszEndChunk;
    
                                    //Check memory allocation
                                    if(pNewData - pCNewData == ncbSzNewData)
                                    {
                                        //Are we setting the font size?
                                        if(wFontPtSz != 0)
                                        {
                                            WORD* pwFontSz = (WORD*)(pCNewData + (pPtr_FontSize - (BYTE*)lpDialogTemplate));
                                            if(*pwFontSz != wFontPtSz)
                                            {
                                                //Set flag
                                                bAppliedFont = TRUE;
                                            }
    
                                            //Set new font size
                                            *pwFontSz = wFontPtSz;
                                        }
    
                                        //Did we have a specified font face too
                                        if(pstrFontFaceName)
                                            bAppliedFont = TRUE;
    
                                        //Use our adjusted template
                                        lpDialogTemplateToUse = (LPCDLGTEMPLATE)pCNewData;
                                    }
                                    else
                                    {
                                        ASSERT(NULL);
                                    }
                                }
                            }
                        }
                    }
    
    
                    //Try to load it from the template
                    CDialog abt;
                    if(abt.InitModalIndirect(lpDialogTemplateToUse, pParentWnd))
                    {
                        //And show the modal dialog
                        nResDlg = abt.DoModal();
                    }
    
                }
            }
    
        }
    
    
        //Free memory
        if(pCNewData)
        {
            delete[] pCNewData;
            pCNewData = NULL;
        }
    
        if(pbOutResultFontApplied)
            *pbOutResultFontApplied = bAppliedFont;
    
        return nResDlg;
    }
    

    Custom struct definition:

    #pragma pack(push, 1) // exact fit - no padding
    struct DLGTEMPLATEEX_PART1{
      WORD      dlgVer;
      WORD      signature;
      DWORD     helpID;
      DWORD     exStyle;
      DWORD     style;
      WORD      cDlgItems;
      short     x;
      short     y;
      short     cx;
      short     cy;
    };
    #pragma pack(pop)
    

    Here’s aux methods for parsing variable-size members:

    BYTE* AdvanceThrough_sz_Or_Ord(BYTE* pData)
    {
        //'pData' = Points to a variable-length array of 16-bit elements that identifies a menu 
        //          resource for the dialog box. If the first element of this array is 0x0000, 
        //          the dialog box has no menu and the array has no other elements. If the first 
        //          element is 0xFFFF, the array has one additional element that specifies the 
        //          ordinal value of a menu resource in an executable file. If the first element 
        //          has any other value, the system treats the array as a null-terminated Unicode 
        //          string that specifies the name of a menu resource in an executable file.
        //RETURN:
        //      = Following address
        ASSERT(pData);
    
        WORD* pWArr = (WORD*)pData;
        if(*pWArr == 0x0000)
        {
            //No other elements
            pWArr++;
        }
        else if(*pWArr == 0xFFFF)
        {
            //Next element is menu ID
            pWArr++;
            pWArr++;
        }
        else
        {
            //Unicode ASIIZ string
            WCHAR z;
            do
            {
                z = *pWArr;
                pWArr++;
            }
            while(z != 0);
        }
    
        return (BYTE*)pWArr;
    }
    
    BYTE* AdvanceThrough_String(BYTE* pData, CString* pOutStr)
    {
        //'pData' = Points to null-terminated Unicode string
        //'pOutStr' = if not NULL, receives the string scanned
        //RETURN:
        //      = Pointer to the first BYTE after the string
        ASSERT(pData);
    
        WCHAR* pWStr = (WCHAR*)pData;
        WCHAR z;
        do
        {
            z = *pWStr;
            pWStr++;
        }
        while(z != 0);
    
        if(pOutStr)
        {
            int nLn = pWStr - (WCHAR*)pData;
            memcpy(pOutStr->GetBufferSetLength(nLn), pData, nLn * sizeof(WCHAR));
            pOutStr->ReleaseBuffer();
        }
    
        return (BYTE*)pWStr;
    }
    

    And here’s how you call it:

    BOOL bResAppliedFontCorrection;
    int nResultDlg = OpenDialogWithFont(this, 
        MAKEINTRESOURCE(IDD_ABOUTBOX),
        _T("Algerian"), 
        16, 
        &bResAppliedFontCorrection);
    

    For those who’s interested how it works, the method modifies the dialog template structure before creating the dialog and thus letting OS do all the font manipulations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say we've some strings like these and need the unit from them: 'rotate(90deg)' //
Say I have this anonymous function: (function(window){ var private = 'private msg'; function sayit()
Say I wanted to run 'User.all.each{|u| u.destroy}' from a shell script called killallusers.sh. How
Say I have a project that I am deploying at www.foo.com/path1/default.aspx and www.foo.com/path2/default.aspx What
Say from a PC, a SSH client will be sending commands (such as custom
Say I have this form : <form> <table> <tbody> <tr> <td style=font-family : Arial>
Say I have an input box with default text name in it. I would
Say I have the string User Name:firstname.surname contained in a larger string how can
Say I have a WPF dialog in which I have async event handlers that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.