Steven V
Steven V

Reputation: 75

MFC SDI read from an INI and update view string while changing attributes

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.

Upvotes: 0

Views: 302

Answers (1)

Andrew Komiagin
Andrew Komiagin

Reputation: 6566

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);
}

Upvotes: 1

Related Questions