RichEdit border problem (using API, not MFC)

Olympus
Olympus used Ask the Experts™
on
I would like to increase the borders (more specifically, the spacing between text and the sides) of my dynamically created richedit control, much like the TRichEdit.BorderWidth property in Delphi. I have tried, according to the API docs, sending the EM_SETPARAFORMAT message with the wBorder and wBorderWidth fields filled out in the PARAFORMAT2 structure to no avail. As a matter of fact, no matter what values I plug in, nothing seems to happen. Anyone else run into this sort of thing?

     g_hRichEdit = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, 0, WS_VISIBLE | ES_MULTILINE |
          ES_READONLY | WS_CHILD | WS_VSCROLL | ES_AUTOVSCROLL | ES_NOHIDESEL, 0, 0, 0, 0, g_wMain, 0, g_hInstance, 0);
     SendMessage(g_hRichEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
     PARAFORMAT2 pf2;

     memset(&pf2, 0, sizeof(pf2));
     pf2.cbSize = sizeof(pf2);
     pf2.dwMask = PFM_BORDER;
     pf2.wBorderSpace = 0;
     pf2.wBorderWidth = 5000;
     pf2.wBorders = 0x20 | 0x400 | 0x5000; //

     SendMessage(g_hRichEdit, EM_SETPARAFORMAT, 0, (LPARAM)&pf2);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
To set the formatting rectangle of a RichEdit control you can sent the EM_SETRECT (or EM_SETRECTNP) message (processed only by multiline controls) to it. This allows to define not only right and left bordrers but top and bottom ones as well.

The code below will increase the distance between the text and the border of the RichEdit by 20 pix on each side.

RECT rc;
::SendMessage(m_hWndRichEdit, EM_GETRECT, 0, (LPARAM)&rc);
rc.left   += 20;
rc.top    += 20;
rc.right  -= 20;
rc.bottom -= 20;
::SendMessage(m_hWndRichEdit, EM_SETRECT, 0, (LPARAM)&rc);
     
If you need to set the left and right margins only you can use the EM_SETMARGINS message:

::SendMessage(m_hWndRichEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(20, 20));


See the MSDN to find more about these messages.

Author

Commented:
Many thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial