2024年2月7日发(作者:)

记事本程序的VC++编程实例

LT

记事本的VC++编程实例

1.用VC++新建一个exe程序,起名叫“Txt”

按如下选择

点击“完成”,弹出提示,再点击“确定”。

2.删除预设控件,添加1个编辑框控件,2个按钮控件,2个静态文本控件,如下图:

3.对添加的控件进行设置

右击Button1,选择属性,进行如下更改:

选择“IDC_OPENPATH”,点击Add Variable,弹出对话框,进行如下设置:

选择“IDC_SAVEPATH”,点击Add Variable,弹出对话框,进行如下设置:

5.添加成员函数并处理单击事件

在类向导中第一个选项卡,

点击“Add Function”,弹出对话框,点“确定”,再点“Edit Code”,进入代码编辑界面,如下:

添加如下代码:

(上为图片,不可复制,复制请移步附录1)

在类向导中如下状态:

点击“Add Function”,弹出对话框,点“确定”,再点“Edit Code”,进入代码编辑界面,如下:

添加如下代码:

(上为图片,不可复制,复制请移步附录2)

6.运行效果

点击“打开”

点击“保存”

OK了,效果还行~

附录1

void CTxtDlg::OnOpen()

{

// TODO: Add your control notification handler

code here

CFileDialog

dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,

"All Files(*.TXT)|*.TXT||",AfxGetMainWnd());

CString strPath,strText="";

if(l() == IDOK)

{

strPath = hName();

m_dowText(strPath);

CFile file(strPath,CFile::modeRead);

char read[10000];

(read,10000);

for(int i=0;i

{

strText += read[i];

}

();

m_dowText(strText);

}

}

附录2

void CTxtDlg::OnSave()

{

// TODO: Add your control notification handler

code here

CFileDialog

dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,

"All Files(*.TXT)|*.TXT||",AfxGetMainWnd());

CString strPath,strText="";

char write[10000];

if(l() == IDOK)

{

strPath = hName();

if((4) != ".TXT")

strPath += ".TXT";

m_dowText(strPath);

CFile

file(_T(strPath),CFile::modeCreate|CFile::modeWrite);

m_dowText(strText);

strcpy(write,strText);

(write,gth());

();

}

}