通过对话框读写文件 [英] Read-write file through Dialog box

查看:53
本文介绍了通过对话框读写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是Visual c ++ MFC编程的新手. 而且我想制作一个基于对话框的应用程序,如果我按下一个按钮(假设读取"),则另一个对话框打开,并且在其编辑"框中,我将传递某些文件的路径(假设c:\ code \ project.txt)当我按下打开"按钮时,我可以在该对话框的编辑框"中显示该特定文件的所有内容.

我可以通过CFileDialog类打开路径,但是当我按OPEN按钮时,它不起作用.那么如何处理那件事.我想对WRITE按钮做同样的事情,在其中通过编辑框写入文件.

因此,请帮助我解决此问题.并帮助我学习如何管理除对话框之外的其他方法,以及如何通过对话框进行数据传输.

在此先谢谢您

Hello to all,

I am new to visual c++ MFC programming.
And I want to make dialog based application in which if I press a button(suppose "read") one another dialog box is open and in its Edit box I will pass path of some file(suppose c:\code\project.txt) and when I press OPEN button I can show all content of that particular file in Edit Box of that Dialog box.

I can open the path through CFileDialog class but when I press OPEN button it is not working. So how to manage that thing. And I want to do same thing for WRITE button in which write in file through edit box.

So help me solve this problem..And help me to learn how to manage more than dialog box and transfer data through that.

Thank you in advance

推荐答案

请阅读 [ ^ ]文章.希望对您有所帮助.
Please read this[^] article. I hope it helps.


CFileDialog无法打开您的文件.用于选择文件.将TRUE作为第一个参数传递时,该按钮被标记为打开"(文件打开"对话框).

调用文件对话框的DoModal()时,请检查返回值.如果它是IDOK,则通过GetPathName()成员函数获取文件名,然后使用编辑控件将其传递到对话框:
The CFileDialog does not open your file. It is used to select files. The button is labeled ''Open'' when passing TRUE as first parameter (File Open dialog).

When calling DoModal() of the file dialog, check the return value. If it is IDOK, get the file name by the GetPathName() member function and pass it to the dialog with the edit control:
CFileDialog FileDlg(TRUE, ...);
if (IDOK == FileDlg.DoModal())
{
    CShowFileDlg ShowDlg; // your dialog with the edit control
    // Pass file name to dialog.
    ShowDlg.m_strFileName = FileDlg.GetPathName();
    ShowDialog().DoModal();
}



应该使用对话框资源编辑器添加多行编辑控件来创建CShowFileDialog:



The CShowFileDialog should be created using the dialog resource editor adding a multi line edit control:

class CShowFileDialog : public CDialog
{
...
public:
    CEdit m_edit; // the edit control (must be added using resource editor)
    CString m_strFileName;
...
};

...
DDX_Control(pDX, IDC_EDIT1, m_edit);
...
BOOL CShowFileDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // - Open file m_strFileName.
    //   Choose you file open/read method.
    CString str;
    // - Read content into to string.
    // - Close file.
    // - Copy string to edit control.
    m_edit.SetWindowText(str.GetString());
    return TRUE;
}


这篇关于通过对话框读写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆