CEdit selects everything when getting focus

Another way of achieving your goal is to prevent the contents from being selected. When navigating over controls in a dialog the dialog manager queries the respective controls about certain properties pertaining to their behavior. By default an edit control responds with a DLGC_HASSETSEL flag (among others) to indicate to the dialog manager that its contents should be auto-selected.

To work around this you would have to subclass the edit control and handle the WM_GETDLGCODE message to alter the flags appropriately. First, derive a class from CEdit:

class CPersistentSelectionEdit : public CEdit {
public:
    DECLARE_MESSAGE_MAP()
    afx_msg UINT OnGetDlgCode() {
        // Return default value, removing the DLGC_HASSETSEL flag
        return ( CEdit::OnGetDlgCode() & ~DLGC_HASSETSEL );
    }
};

BEGIN_MESSAGE_MAP( CPersistentSelectionEdit, CEdit )
    ON_WM_GETDLGCODE()
END_MESSAGE_MAP()

Next subclass the actual control. There are a number of ways to do this. To keep things simple just declare a class member m_Edit1 of type CPersistentSelectionEdit in your dialog class and add an appropriate entry in DoDataExchange:

// Subclass the edit control
DDX_Control( pDX, IDC_EDIT1, m_Edit1 );

At this point you have an edit control that doesn't have its contents auto-selected when navigated to. You can control the selection whichever way you want.


I don't think that such a style exists.
But you can add OnSetfocus handler with the wizard:

void CMyDlg::OnSetfocusEdit1() 
{
  CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
  e->SetSel(0); // <-- hide selection
}