Home > Software engineering >  MFC read TXT file
MFC read TXT file

Time:10-13

Click on the button in the menu, read the data in a TXT file, display to ID IDC_EDIT1 edit box, the code is as follows:
 void CTestApp: : On32771 () 
{CFileDialog fileDlg (TRUE);
If (fileDlg DoModal ()==IDOK)
{
Cstrings STR.
CFile f;
F.O pen (fileDlg GetFileName (), CFile: : modeReadWrite);
F.R ead (STR) GetBuffer (f.G etLength ()), f.G etLength ());
F. lose ();
GetDlgItem (IDC_EDIT1) - & gt; SetWindowText (STR);
}
}

Error as "GetDlgItem:" function does not accept a parameter and "SetWindowTextA:" function does not accept a parameter, how to solve

CodePudding user response:

Is a multiview project? Try to add # include "Resource. H"

CodePudding user response:

Your response function in the App, the App class not is a subclass of CDialog, so call GetDlgItem call Win32API interface, the first parameter is the handle to HWND, another also is such, how don't in the main dialog to do in class?

CodePudding user response:


Of the code you write is not standard?

CEdit * p=(CEdit *) GetDlgItem (IDC_EDIT1);
P - & gt; SetWindowText (STR);

Try line not line?

CodePudding user response:

refer to the second floor VisualEleven response:
your response function in the App, the App class not is a subclass of CDialog, so call GetDlgItem call Win32API interface, the first parameter is the handle to HWND, another also is such, how don't in the main dialog to do in class?

I understand what you said, I'll find someone reply to other posts: : SetWindowText (GetDlgItem (hWnd, IDC_EDIT1));
But I will not change, the first contact with MFC, complete novice, requirements are added to the menu

CodePudding user response:

The
reference 3 floor misa98 response:
non-standard code you write?

CEdit * p=(CEdit *) GetDlgItem (IDC_EDIT1);
P - & gt; SetWindowText (STR);

Try line not line?

I also am online file, the novice just contact, don't understand, tried it on what you have said, or not

CodePudding user response:

The HWND hDlg=AfxGetApp () - & gt; GetMainWnd () - & gt; GetSafeHwnd ();
If (hDlg==NULL) return;
HWND hEdit=GetDlgItem (hDlg, IDC_EDIT1);
If (hEdit==NULL) return;
SetWindowText (hEdit, STR);

Try

CodePudding user response:


Your project is built wrong!
You to create a dialog-based application, try again?

CodePudding user response:

Void CTestApp: : On32771 ()
Function position is wrong, add this event to CTestDlg, rather than CTestApp

CodePudding user response:

1. As 8 said,
2. In the app GetDlgItem need to get the handle to the window IDC_EDIT1 (if the window exists is likely to get into, otherwise an error),
3. Not recommended for use GetDlgItem directly in the app to SetWindowText, can GetActiveView () to invoke the SetWindowText function

Personal advice,

CodePudding user response:

refer to 7th floor misa98 response:
you project built wrong!
You to create a dialog-based application, try again?
is based on the dialog box

CodePudding user response:

refer to 6th floor ruanliao1914 response:
HWND hDlg=AfxGetApp () - & gt; GetMainWnd () - & gt; GetSafeHwnd ();
If (hDlg==NULL) return;
HWND hEdit=GetDlgItem (hDlg, IDC_EDIT1);
If (hEdit==NULL) return;
SetWindowText (hEdit, STR);

Try
thank you, tried to run, but open 1. TXT file, said cannot find the C: \ Users \ \ Documents \ Visual AD Studio2010 \ Projects \ Test \ Test \ 1. Test, is this why?

CodePudding user response:

IDC_EDIT1 is not in the App, so these problems
  • Related