Paragraph Formatting

In a rich edit control, the user can also set the attributes of entire paragraphs, including alignment (left-justified, centered, or right-justified), tab stops, indention, and numbering.

You can apply paragraph formatting to text in a rich edit control by using the EM_SETPARAFORMAT message. As with all good Windows APIs, you use the EM_GETPARAFORMAT message to find out the current paragraph formatting. Both messages use the PARAFORMAT structure to specify paragraph attributes.

The RICHED sample supports all three paragraph alignment options, as the following code demonstrates. It's interesting to note that it takes more code to ensure that the buttons are “pressed” and the menu items are checked than it does to actually set the paragraph format.

VOID AlignCmd (HWND hWnd, HWND hWndRichEdit, int iAlign)
{
PARAFORMAT pf;

// Fill out the PARAFORMAT structure with the mask and size.
pf.cbSize = sizeof (pf);
pf.dwMask = PFM_ALIGNMENT;

switch (iAlign)
{
case IDM_ALIGNLEFT:
pf.wAlignment = PFA_LEFT;
§
}

// Set the new paragraph alignment.
SendMessage (hWndRichEdit, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
// Reset the dirty bit.
SendMessage (hWndRichEdit, EM_SETMODIFY, (WPARAM)TRUE, OL);
}

Figure 5-5 shows you the look that is produced by centering text in the RICHED sample.

 

Figure 5-5.

Centered paragraphs in the RICHED sample.