The default behavior for Edit and Rich Edit controls is to make the selection invisible when the control does not have the input focus and only make it visible when the control has the focus. The selection is not, however, changed. The ES_NOHIDESEL
style overrides this default behavior and causes the selection to always appear in the control, regardless of whether or not it has the focus. You have certainly seen this behavior before: it is what both Microsoft Word and Visual Studio do.
As such, your understanding of the SDK documentation is perfectly correct. Unfortunately, there is another aspect of the Rich Edit control's behavior that is getting in the way. Whenever an Edit or Rich Edit control hosted in a dialog box receives the focus, it automatically selects all of its text, obliterating the current caret position in the process. The ES_NOHIDESEL
does not have any effect on this behavior; it just changes whether or not the selection is visible when the control is unfocused. You can certainly override this select-all-on-focus behavior by doing as IInspectable suggested and subclassing the control to customize its handling of the WM_GETDLGCODE
message.
But there is a much simpler solution. Along the same lines as ES_NOHIDESEL
, you want to set the ES_SAVESEL
style for the control upon creation. Although you can set ES_NOHIDESEL
in the resource editor ("No Hide Selection"), there is no equivalent property for ES_SAVESEL
. You can manually add it to the RC file, but there's no guarantee that it won't be obliterated when Visual Studio regenerates that file.
Alternatively, you can send the Rich Edit control a EM_SETOPTIONS
message specifying the ECO_SAVESEL
option. In MFC, the SetOptions
member function wraps the sending of this message. For example, in your OnInitDialog
function, you might have the following:
m_myRichEditCtrl.SetOptions(ECOOP_OR, ECO_SAVESEL); // maintain selection across focus events