How exactly can you make bold text in a richedit control?

I have been trying for the past few hours trying to figure out why the text in my Richedit Window won't go Bold. I have tried a number of different combinations with the dwMask member but with not avail.

Here is my code..
1
2
3
4
5
6
7
8
9
10
11
12
void ChangeFontToBold(HWND richedit)
{
	CHARFORMAT boldfont;

	boldfont.cbSize = sizeof(CHARFORMAT);	
	boldfont.crTextColor = RGB(0, 0, 0);	
	boldfont.dwMask = CFM_COLOR | CFM_SIZE | CFM_BOLD | CFM_CHARSET;
	boldfont.yHeight = 200;					
	boldfont.dwEffects = CFE_BOLD;			/* Text will be BOLD*/
	boldfont.bCharSet = ANSI_CHARSET;		
	SendMessage(richedit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&boldfont);
}


I am using this function to send a message with all the details of the CHARFORMAT structure and i specified CFE_BOLD for the dwEffects member and I also specified the CFM_BOLD value in the dwMask member.

Even after doing this, the text in my RichEditWindow wont go bold.

Can anyone give me a few hints on what exactly is wrong with my function
Last edited on
Your function works as-is for me.

I used RichEdit 4.1 (i.e. window class "RICHEDIT50W", DLL "MSFTEDIT.DLL")

Andy
Hmm i am using RichEdit 4.1 as well. (with MSFTEDIT_CLASS as the window class)

My rich edit control is not making the text bold at all.... But for some reason, every time i remove the bCharSet Member from my function, the current text in the rich edit control becomes bold but once i start typing... the new text becomes normal while other text stays bold

Why does that happen?



P.S When you used the function, did the text became really bold? or only slightly?
Last edited on
I think i fixed the problem but the solution seems to confuse me a little.

I used the CFM_FACE value in the dw.Mask member and the text became bold..

even thought i am not specifying a value for the szFaceName member
It's a bit worrying that you need to include CFM_FACE to get it to work.

But I do see you're doing more than setting the weight to bold. Doe you code work if you just do obld, without the color, size, or character set?

The following code behaves as expected for me (I used it as an excuse to check I still knew how to use CreateDialogIndirect, to avoid the need for an .RC file. Starting point is MSDN example here, plus bug fixes:
Using Dialog Boxes / Creating a Template in Memory
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644996%28v=vs.85%29.aspx#template_in_memory )

Andy

PART 1 OF 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// dlg_test.cpp

#define WINVER 0x0600
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <tchar.h>

//#define SET_OTHER_STUFF

#define IDC_TEST      1001
#define IDC_RICHEDIT  1002
#define IDC_STATIC      -1

HWND g_hDlg = NULL;
bool g_isBold = false;

HWND CreateTestDialog(HINSTANCE hinst);

INT_PTR CALLBACK TestDialogProc(HWND, UINT, WPARAM, LPARAM);

const TCHAR initdata[] =
_T("The original specification for rich edit controls is Microsoft Rich Edit 1.0;")
_T(" the current specification is Microsoft Rich Edit 4.1. Each version of rich edit")
_T(" is a superset of the preceding one, except that only Asian builds of Microsoft")
_T(" Rich Edit 1.0 have a vertical text option. Before creating a rich edit control,")
_T(" you should call the LoadLibrary function to verify which version of Microsoft")
_T(" Rich Edit is installed.");

int APIENTRY
_tWinMain(HINSTANCE hInstance,
          HINSTANCE hPrevInstance,
          LPTSTR    lpCmdLine,
          int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    LoadLibrary(_T("MSFTEDIT.DLL"));

    g_hDlg = CreateTestDialog(hInstance);
    ShowWindow(g_hDlg, nCmdShow);

    int ret = 0;
    MSG msg = {0};
    while(ret = GetMessage(&msg, NULL, 0, 0))
    {
        if(ret == -1)
            return -1;

        if(!IsDialogMessage(g_hDlg, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}

void ChangeFontToBold(HWND richedit)
{
    CHARFORMAT boldfont;
    boldfont.cbSize = sizeof(CHARFORMAT);
#ifndef SET_OTHER_STUFF
    boldfont.dwMask = CFM_BOLD;
#else
    boldfont.dwMask = CFM_COLOR | CFM_SIZE | CFM_BOLD | CFM_CHARSET;
    boldfont.crTextColor = RGB(255, 0, 0);
    boldfont.yHeight = 300;
    boldfont.bCharSet = OEM_CHARSET;
#endif
    boldfont.dwEffects = CFE_BOLD;    /* Text will be BOLD*/
    SendMessage(richedit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&boldfont);
}

void ChangeFontToNormal(HWND richedit)
{
    CHARFORMAT boldfont;
    boldfont.cbSize = sizeof(CHARFORMAT);
#ifndef SET_OTHER_STUFF
    boldfont.dwMask = CFM_BOLD;
#else
    boldfont.dwMask = CFM_COLOR | CFM_SIZE | CFM_BOLD | CFM_CHARSET;
    boldfont.crTextColor = RGB(0, 0, 0);
    boldfont.yHeight = 200;
    boldfont.bCharSet = ANSI_CHARSET;
#endif
    boldfont.dwEffects = 0;    /* Text will be normal*/
    SendMessage(richedit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&boldfont);
}

LPWORD lpwAlign(LPWORD lpIn, ULONG dw2Power = 4)
{
    ULONG ul = (ULONG)lpIn;
    ul += dw2Power-1;
    ul &= ~(dw2Power-1);
    return (LPWORD)ul;
}

INT_PTR CALLBACK
TestDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
        {
            HWND hwndEdit = GetDlgItem(hDlg, IDC_RICHEDIT);
            SetWindowText(hwndEdit, initdata);
            return (INT_PTR)FALSE;
        }

        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return (INT_PTR)TRUE;
        }

        case WM_COMMAND:
            if ((LOWORD(wParam) == IDCLOSE) || (LOWORD(wParam) == IDCANCEL))
            {
                DestroyWindow(hDlg);
                return (INT_PTR)TRUE;
            }
            else if (LOWORD(wParam) == IDC_TEST)
            {
                HWND hwndEdit = GetDlgItem(hDlg, IDC_RICHEDIT);
                if(g_isBold)
                {
                    ChangeFontToNormal(hwndEdit);
                    g_isBold = false;
                    SetDlgItemText(hDlg, IDC_TEST, _T("Bold"));
                }
                else
                {
                    ChangeFontToBold(hwndEdit);
                    g_isBold = true;
                    SetDlgItemText(hDlg, IDC_TEST, _T("Normal"));
                }
            }
            break;

        default:
            { }
    }

    return (INT_PTR)FALSE;
}
Last edited on
PART 2 OF 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//IDD_TEST DIALOGEX 0, 0, 174, 195
//STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
//CAPTION "Test Dialog"
//FONT 8, "MS Shell Dlg", 400, 0, 0x1
//BEGIN
//    DEFPUSHBUTTON   "Close",IDCLOSE,103,170,50,14
//    PUSHBUTTON      "Test",IDC_TEST,43,170,50,14
//    CTEXT           "RichEdit Control",IDC_STATIC,16,5,201,8
//    CONTROL         "",IDC_RICHEDIT,"RichEdit50W", ES_MULTILINE | ES_WANTRETURN
//                    | WS_BORDER | WS_TABSTOP,16,24,138,134
//END
HWND CreateTestDialog(HINSTANCE hinst)
{
    HGLOBAL hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
    if (!hgbl)
        return NULL;

    HWND hwnd = NULL;

    LPWORD lpw = NULL;

    // Define a dialog box
    {
        LPDLGTEMPLATE lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
        lpdt->style = DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU;
        lpdt->dwExtendedStyle = 0;
        lpdt->cdit = 4; // Number of controls
        lpdt->x  = 0;   lpdt->y  = 0;
        lpdt->cx = 174; lpdt->cy = 195;

        lpw = (LPWORD)(lpdt + 1);
        *lpw++ = 0; // No menu
        *lpw++ = 0; // Predefined dialog box class (by default)

        LPWSTR lpwsz1 = (LPWSTR)lpw;
        int nchar1 = MultiByteToWideChar(CP_ACP, 0, "Test Dialog", -1, lpwsz1, 50);
        lpw += nchar1;

        *lpw++ = 8; // font size
        LPWSTR lpwsz2 = (LPWSTR)lpw;
        int nchar2 = MultiByteToWideChar(CP_ACP, 0, "MS Shell Dlg", -1, lpwsz2, 50);
        lpw += nchar2;
    }

    // Define a Close button
    {
        lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
        LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
        lpdit->dwExtendedStyle = 0;
        lpdit->x  = 103; lpdit->y  = 170;
        lpdit->cx = 50;  lpdit->cy = 14;
        lpdit->id = IDCLOSE; // Close button identifier

        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080; // Button class

        LPWSTR lpwsz = (LPWSTR)lpw;
        int nchar = MultiByteToWideChar(CP_ACP, 0, "Close", -1, lpwsz, 50);
        lpw += nchar;
        *lpw++ = 0; // No creation data
    }

    // Define a test button (displays Bold (to start with) or Normal
    {
        lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
        LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
        lpdit->dwExtendedStyle = 0;
        lpdit->x  = 43; lpdit->y  = 170;
        lpdit->cx = 50; lpdit->cy = 14;
        lpdit->id = IDC_TEST; // Test button identifier

        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080; // Button class

        LPWSTR lpwsz = (LPWSTR)lpw;
        int nchar = MultiByteToWideChar(CP_ACP, 0, "Bold", -1, lpwsz, 50);
        lpw += nchar;
        *lpw++ = 0; // No creation data
    }

    // Define a static text control.
    {
        lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
        LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
        lpdit->dwExtendedStyle = 0;
        lpdit->x  = 16;  lpdit->y  = 5;
        lpdit->cx = 101; lpdit->cy = 8;
        lpdit->id = IDC_STATIC;

        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0082; // Static class

        LPWSTR lpwsz = (LPWSTR)lpw;
        int nchar = MultiByteToWideChar(CP_ACP, 0, "RichEdit Control", -1, lpwsz, 50);
        lpw += nchar;
        *lpw++ = 0; // No creation data
    }

    // Define a RichEdit control.
    {
        lpw = lpwAlign(lpw); // Align DLGITEMTEMPLATE on DWORD boundary
        LPDLGITEMTEMPLATE lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->style = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP
            | ES_MULTILINE | ES_WANTRETURN;
        lpdit->dwExtendedStyle = 0;
        lpdit->x  = 16;  lpdit->y  = 24;
        lpdit->cx = 138; lpdit->cy = 134;
        lpdit->id = IDC_RICHEDIT;

        lpw = (LPWORD)(lpdit + 1);
        LPWSTR lpwsz2 = (LPWSTR)lpw;
        int nchar2 = MultiByteToWideChar(CP_ACP, 0, "RichEdit50W", -1, lpwsz2, 50);
        lpw += nchar2;

        LPWSTR lpwsz = (LPWSTR)lpw;
        int nchar = MultiByteToWideChar(CP_ACP, 0, "RichEdit Control", -1, lpwsz, 50);
        lpw += nchar;
        *lpw++ = 0; // No creation data
    }

    GlobalUnlock(hgbl);

    hwnd = CreateDialogIndirect(hinst, (LPDLGTEMPLATE)hgbl, NULL, TestDialogProc);

    GlobalFree(hgbl);

    return hwnd; 
}
Last edited on
Topic archived. No new replies allowed.