问题1:MFC列表框中内容不按照添加的顺序显示
解决:控件属性->行为->sort->false
设置编辑框变灰且不可输入:GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
清除编辑框中已经显示的内容:GetDlgItem(IDC_EDIT1)->SetWindowText(L"");
L””等于CString类型的NULL,不等于一般的NULL
控件变量:
control:代表这个控件
value:传值
获取列表框当前选项的下标(从0开始):control.GetCurSel();
读取文件:
CstdioFile file;
file.Open(L"F:\\ts.txt",CFile::modeRead);
CString strText = L"";
CString szLine = L"";
while (file.ReadString(szLine))
{
strText +=L"\n"+ szLine;
}
MessageBox(strText);
file.Close();
写入到文件:
CStdioFile file;
file.Open(L"F:\\ts.txt",CFile::modeCreate | CFile::modeReadWrite);//写入文件
CString str = L"hello!\nI amtalkingmute!";
file.WriteString(str);
file.Close();
CString和int的相互转化
CString->int:
int n;
CString str;
n = _ttoi(str);
int -> CString:
str.Format(L”%d”,n);
对话框函数
CFileDialog::CFileDialog
(
BOOL bOpenFileDialog,
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL
);
example:
CString filter;
filter="文本文档(*.txt)|*.txt|PDF文档(*.pdf)|*.pdf||";
CFileDialogdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,filter);
if(dlg.DoModal()==IDOK)
{
CString str;
str=dlg.GetPathName();
MessageBox(str);
}
dlg.Domodal():显示一个模态对话框,关闭对话框后DoModal才返回,返回值为按钮ID—IDOK,IDCANCEL
CFileDialog dlg(…,…,…,OFN_NOCHANGEDIR,…);
cfiledialog默认会改变路径当上一次选中的路径,通过设置默认路径和传入参数OFN_NOCHANGEDIR可以让文件每次都从默认路径打开
c源文件要包含自己的头文件,目的就是让编译器检查定义和声明的一致性。
头文件的一般格式:
#ifndef _TEST_H_
#define _TEST_H_
全局变量的声明
函数声明
#endif
问题:vs中调试c时调试框闪退
解决:1 添加代码system("pause");
2 ctrl +F5