问题
This is probably a pretty basic question, but I can't seem to get it. I am working on a visualC++ project and I basically want to get a string from a GUI and then use that as a filename. I have written the following thus far, where IDC_FILE_NAME is the ID of the edit control box but I'm not sure if that is even the way to accomplish this.
m_pFileName = (CEdit*)GetDlgItem( IDC_FILE_NAME );
CString fName =_T(" ");
GetDlgItemTextA(IDC_FILE_NAME, fName);
回答1:
but I'm not sure if that is even the way to accomplish this.
The answer is YES and NO. YES if properly used, NO, not as you do it. Do not use UNICLODE/ANSI specific versions of functions unless you want to force UNICODE or ANSI. Your code should look like:
CString csText;
GetDlgItemText(IDC_FILE_NAME, csText);
NOTE GetDlgItemText
回答2:
This has been tested with VS2015:
//
// Get string from CEdit m_ceDate;
// where
// DDX_Control(pDX, IDC_EDIT_DATE, m_ceDate);
char cdateBuf[128];
UINT nCountOfCharacters = GetDlgItemText(IDC_EDIT_DATE, cdateBuf, 16);
CString csDate = cdateBuf;
来源:https://stackoverflow.com/questions/10662946/visual-c-get-string-from-cedit