MFC中文件打开与保存
生活随笔
收集整理的這篇文章主要介紹了
MFC中文件打开与保存
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、文件打開與保存
// NotePad01Dlg.cpp : implementation file //#include "stdafx.h" #include "NotePad01.h" #include "NotePad01Dlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog { public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation protected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP() };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT }void CAboutDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CNotePad01Dlg dialogCNotePad01Dlg::CNotePad01Dlg(CWnd* pParent /*=NULL*/): CDialog(CNotePad01Dlg::IDD, pParent) {//{{AFX_DATA_INIT(CNotePad01Dlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }void CNotePad01Dlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CNotePad01Dlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CNotePad01Dlg, CDialog)//{{AFX_MSG_MAP(CNotePad01Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_CLOSE()ON_COMMAND(ID_FILE_EXIT, OnFileExit)ON_COMMAND(ID_FILE_OPEN, OnFileOpen)ON_COMMAND(ID_FILE_SAVE, OnFileSave)//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CNotePad01Dlg message handlersBOOL CNotePad01Dlg::OnInitDialog() {CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control }void CNotePad01Dlg::OnSysCommand(UINT nID, LPARAM lParam) {if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);} }// If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework.void CNotePad01Dlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();} }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CNotePad01Dlg::OnQueryDragIcon() {return (HCURSOR) m_hIcon; }void CNotePad01Dlg::OnClose() {// TODO: Add your message handler code here and/or call defaultCDialog::OnClose() ; }void CNotePad01Dlg::OnFileExit() {// TODO: Add your command handler code hereCDialog::OnCancel();}void CNotePad01Dlg::OnFileOpen() {// TODO: Add your command handler code hereCFileDialog fd( TRUE, "*.txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "TEXT(*.txt)|*.txt|All Files (*.*)|*.*||", NULL );if(IDCANCEL==fd.DoModal())return;CString szFile;szFile=fd.GetPathName();CFile file;if(!file.Open(szFile,CFile::modeReadWrite)){AfxMessageBox("文件打開失敗");return;}DWORD dwLen= file.GetLength();char *pBuff=new char[dwLen+1];file.Read(pBuff,dwLen);pBuff[dwLen]=0;SetDlgItemText(IDC_EDIT1,pBuff);file.Close();delete []pBuff;}void CNotePad01Dlg::OnFileSave() {// TODO: Add your command handler code hereCFileDialog fd( FALSE, "*.txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "TEXT(*.txt)|*.txt|All Files (*.*)|*.*||", NULL );if(IDCANCEL==fd.DoModal())return;CFile file;if(!file.Open(fd.GetPathName(),CFile::modeWrite|CFile::modeCreate)){AfxMessageBox("保存文件失敗");return;}CWnd *pEdit=GetDlgItem(IDC_EDIT1);int nLen=pEdit->GetWindowTextLength();char *pBuff=new char[nLen+1];pEdit->GetWindowText(pBuff,nLen+1);pBuff[nLen]=0;file.Write(pBuff,nLen);file.Close();delete []pBuff;} 2、運行效果如下:
總結(jié)
以上是生活随笔為你收集整理的MFC中文件打开与保存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu16.04编译安装c++ o
- 下一篇: 单反相机内部光线反射原理