mfccedit

MFC, Is there any handy way to get the contents of an edit box within its member function?


I'm customizing an edit box by inheriting a new class from CEdit based on MFC. And I suppose to access its contents (text) inside its member funtions. I wonder if there is any handier/safer/faster way to read and write the string, than calling the public funtion GetWindowText()?


Solution

  • For a single-line edit control the only way to get to the text stored in the control is to call GetWindowText (after an optional call to GetWindowTextLength). MFC's CWnd implementation provides two GetWindowText overloads, one of which taking a reference to a CString. This overload certainly meets the "handy" and "safe" categories by taking on the responsibility of managing memory for you. As for "fast", well, heap allocations are inherently costly; the actual copy itself is blazingly fast.

    Multi-line edit controls provide additional APIs to directly access the in-memory representation of the control's text. The edit control messages EM_GETHANDLE and EM_SETHANDLE allow a client to gain read-only access to the text or change the memory location used by the control to store its text, respectively. They are exposed as the CEdit member functions GetHandle and SetHandle.

    The MFC documentation for either suggests that the DS_LOCALEDIT dialog style were required for them to work. While that may have been the case in 16-bit Windows, I don't believe this is true today.