0

First time post, so forgive any improper etiquette i may bring.

My title briefly describes the problem i have. I have a school project where I start with a MFC application with a single document view and i need to display just a simple text onto my view BUT this text comes from a simple INI file.

LPCTSTR path = _T("C:\\Users\\Steve\\Documents\\WhereDataIs.ini");
TCHAR INIValue[256];
GetPrivateProfileString(_T("SETUP"), _T("Introduction"), _T("File Could Not Be Found"), INIValue, 256, path);

Okay now my ini file just contains this...

[SETUP]

Introduction = Here is your data

Up to this point, everything is going well. The next step is to grab the 'static text box' i created in the resource editor and change it to the data from the ini which is just 'Here is your data'.

SetDlgItemText(StringToChange, INIValue);

and this works perfect. So i managed to change what i wanted as desired but now comes the hard part. I need to bold the entire string before using

SetDlgItemText(StringToChange, INIValue);

I have run into so many problems in the last 5 days. I have tried everything i have run across on google and stack overflow. I ran into trying to use a RTF control? trying to convert to html bolding it and then coming back? issues with 8bit or 16bit? unicode or something along those lines. i've ran into macros like _T("some string") that apparently doesn't take a variable of type string. Nothing has worked for me. What i really could use is some kind of example, it doesn't have to relate to mine at all. Something i can work with a base from. And before you link me to another website or a previous post - i guarantee you I've already looked at it and tried it. I'd really like someone to post a small part of code to help get me started.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Steven V
  • 65
  • 5

1 Answers1

1

You need to use font with bold style to achieve what you need:

 m_font.CreateFont(16, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, DEFAULT_CHARSET,
 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
 DEFAULT_PITCH | FF_SWISS, _T("MS Sans Serif"));
 m_YourStaticControl.SetFont(&m_font); 

make CFont m_font; a member of your class (put it in header file).

If you want more flexibility use RichEdit control. Here is helper function that appends text using different styles:

void AddToRichText(CRichEditCtrl &rEdit, const CString& sNewText, COLORREF color, BOOL bBold, BOOL bUnderline, BOOL bItalic)
{
    int iTotalTextLength = rEdit.GetWindowTextLength();

    rEdit.SetSel(iTotalTextLength, iTotalTextLength);

    CHARFORMAT cf;
    cf.cbSize = sizeof(CHARFORMAT);
    cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD | CFM_ITALIC;

    DWORD dwEffects = CFE_AUTOCOLOR;
    if (!bBold)
        dwEffects |= CFE_BOLD;

    if (!bUnderline)
        dwEffects |= CFE_UNDERLINE;

    if (!bItalic)
        dwEffects |= CFE_ITALIC;

    cf.dwEffects = (unsigned long)~dwEffects;

    cf.crTextColor = color;
    rEdit.SetSelectionCharFormat(cf);

    rEdit.ReplaceSel(sNewText);
    rEdit.HideSelection(TRUE, FALSE);
}
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • non-RichText: you will need to add the code in `YourDlg::OnInitDialog()` after the call to 'CDialog[Ex]::OnInitDialog()'; the call to set the font would be `StringToChange.SetFont(&m_font);` – Edward Clements May 25 '16 at 14:33
  • Okay so the first option worked wonders! it took the whole thing and made it bold. Is there any way to make just a word bold instead of the whole control? considering that we're setting the WHOLE control with that kind of font, i would think not. I am currently looking into a RichEdit Control so i can play around with the code you provided. – Steven V May 26 '16 at 17:56
  • @StevenV For regular edit box you would have to make it owner draw. – Andrew Truckle May 28 '16 at 22:26