您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页MFC实现记事本

MFC实现记事本

来源:化拓教育网


void Cmf13View::OnMyopen()

{

// TODO: 在此添加命令处理程序代码

CClientDC dc(this);

CFileDialog filedlg(TRUE);

//filedlg.m_ofn.Flags=OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

filedlg.m_ofn.lpstrFilter=TEXT(\"Text Files(*.txt)\\0*.txt\\0All Files(*.*)\\0*.*\\0\\0\");

filedlg.m_ofn.lStructSize=sizeof(OPENFILENAME);

if(filedlg.DoModal() == IDOK)

{

fullpath1=filedlg.GetPathName();

fullname1=filedlg.GetFileName();

//CString szLine=_T(\"\");

//CString strText=_T(\"\");

//CStdioFile file;

////SetWindowText(hWnd,ofn.lpstrFileTitle);

//file.Open(fullpath1,CFile::modeRead);

////逐行读取字符串

//while(file.ReadString(szLine))

//{

//

// strText+=szLine;

// strText+=\"\\r\\n\";

//}

//MessageBox(strText);

/* CFile cfile;

cfile.Open(fullpath1,CFile::modeRead);

int len=cfile.GetLength();

char *buf;

buf=new char[len];

buf[len]=0;

cfile.Read(buf,len);

MessageBox(buf);

cfile.Close();*/

/*CFile cfile;

cfile.Open(fullpath1,CFile::modeRead);

int len=cfile.GetLength();

TCHAR *buf;

buf=new TCHAR[len];

buf[len]=0;

cfile.Read(buf,len);

MessageBox(buf);

cfile.Close();*/

/* CFile cfile;

cfile.Open(fullpath1,CFile::modeRead);

int len=cfile.GetLength();

WCHAR *buf;

buf=new WCHAR[len];

buf[len]=0;

cfile.Read(buf,len);

MessageBox(WcharToChar(buf));

cfile.Close();*/

CFile cfile;

cfile.Open(fullpath1,CFile::modeRead);

int len=cfile.GetLength();

char *buf;

buf=new char[len];

buf[len]=0;

cfile.Read(buf,len);

//MessageBox(CharToWchar(buf));

m_edit.SetWindowTextW(CharToWchar(buf));

cfile.Close();

}

else

{

return;

}

}

void Cmf13View::OnMysave()

{

// TODO: 在此添加命令处理程序代码

CClientDC dc(this);

CFileDialog filedlg(FALSE);

//filedlg.m_ofn.Flags=OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

filedlg.m_ofn.lpstrFilter=TEXT(\"Text Files(*.txt)\\0*.txt\\0C++ Files(*.cpp)\\0*.cpp\\0All Files(*.*)\\0*.*\\0\\0\");

filedlg.m_ofn.lStructSize=sizeof(OPENFILENAME);

if(filedlg.DoModal() == IDOK)

{

fullpath1=filedlg.GetPathName();

//fullname1=filedlg.GetFileName();

fileext=filedlg.GetFileExt();

/* if(fileext==_T(\"\"))

{

MessageBox(_T(\"文件后缀名为空!\"));

}*/

/* if(fileext==_T(\"\"))

{

switch(filedlg.m_ofn.nFilterIndex)

{

case 1:

fullpath1+=_T(\".txt\");

break;

case 2:

fullpath1+=_T(\".cpp\");

break;

case 3:

break;

}

}*/

switch(filedlg.m_ofn.nFilterIndex)

{

case 1:

if(fullpath1.Right(4)!=_T(\".txt\"))

fullpath1+=_T(\".txt\");

break;

case 2:

if(fullpath1.Right(4)!=_T(\".cpp\"))

fullpath1+=_T(\".cpp\");

break;

}

CFile cfile;

cfile.Open(fullpath1,CFile::modeWrite|CFile::modeCreate);

CString strText;

m_edit.GetWindowTextW(strText);

int len=strText.GetLength();

char *buf;

buf=new char[len*2];

//buf[len*2]=0;

memset(buf,0,len*2);

strcpy(buf,WcharToChar(strText.GetBuffer(strText.GetLength())));

cfile.Write(buf,len*2);

cfile.Close();

}

else

{

return;

}

}

char* Cmf13View::WcharToChar(wchar_t* wc)

{

char* m_char(NULL);

wchar_t* m_wchar(NULL);

if(m_char)

{

delete m_char;

m_char=NULL;

}

if(m_wchar)

{

delete m_wchar;

m_wchar=NULL;

}

int len= WideCharToMultiByte(CP_ACP,0,wc,wcslen(wc),NULL,0,NULL,NULL);

m_char=new char[len+1];

WideCharToMultiByte(CP_ACP,0,wc,wcslen(wc),m_char,len,NULL,NULL);

m_char[len]='\\0';

return m_char;

}

wchar_t* Cmf13View::CharToWchar(char* c)

{

char* m_char(NULL);

wchar_t* m_wchar(NULL);

if(m_char)

{

delete m_char;

m_char=NULL;

}

if(m_wchar)

{

delete m_wchar;

m_wchar=NULL;

}

int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);

m_wchar=new wchar_t[len+1];

MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);

m_wchar[len]='\\0';

return m_wchar;

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务