EDIT: this question is significantly edited based on further investigation of the problem.
I am working on a program API that displays programmatically created rich edit controls. It allows the caller to specify whether to use plain text or rich text like this:
void SetUseRichText(bool state)
{
DWORD textMode = SendDlgItemMessageW(GetWindowHandle(), GetControlID(), EM_GETTEXTMODE, 0, 0);
if (state)
{
textMode &= ~TM_PLAINTEXT;
textMode |= TM_RICHTEXT;
}
else
{
textMode &= ~TM_RICHTEXT;
textMode |= TM_PLAINTEXT;
}
SendDlgItemMessageW(GetWindowHandle(), GetControlID(), EM_SETTEXTMODE, textMode, 0);
}
The primary goal of this setting is to prevent text in the control from picking up the formatting of text pasted into the control. It would be nice if the program itself could modify the formatting beyond the font (which it can and does modify).
I have run into an issue where I would like to be able to control the line spacing of the text in the control. I have this code to do it:
PARAFORMAT2 pf{};
pf.cbSize = sizeof(pf);
pf.dwMask = PFM_LINESPACING;
pf.bLineSpacingRule = value ? 3 : 0; // use twips, but no smaller than single-space
pf.dyLineSpacing = std::lround(value * 20.0);
SendDlgItemMessageW(GetWindowHandle(), GetControlID(), EM_SETPARAFORMAT, 0, reinterpret_cast<LPARAM>(&pf));
The problem is that if the caller has set the control to TM_PLAINTEXT, then EM_SETPARAFORMAT does not work. (The SendMessage returns 0.) I guess my quesion is multi-part.
- Is there a way to control the line spacing of a
TM_PLAINTEXTrich edit control? Perhaps some way that is not using messages specific to the control? - Given that
TM_PLAINTEXTandTM_RICHTEXTare bit-values, what does it mean of both are set? (Or if neither are set?) - Should I take a different approach to prevent formatting from being pasted in?
About setting
TM_PLAINTEXTandTM_RICHTEXT.I found a mistake If you want to set
TM_RICHTEXTandTM_PLAINTEXTon text, you need useCHARFORMAT2W.PARAFORMAT2just set the paragraph formatting. I already tested it.If you set
CHARFORMAT2W. It always displays character formatting of a rich edit control. Whatever you set one of them, both of them and none.It also can change the line-spacing when richtext and plaintext exist.
Here is my code, hope it helpful.(Modified)
Edit:New rules as you said:
Also modified the code.