菜单命令调用对话框按钮功能实现(迷惑中)
kkmqj 2009-06-18 08:55:14 我想在菜单命令中实现对对话框内容的保存
在对话框上已经有保存按钮,功能可以实现。
但菜单命令中的就有点迷惑了
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
void CTest1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString strExt = ".txt"; // 扩展名
CString strFilePath;
CString strFilter;
strFilter.Format("Text Files (*txt)|*txt|All Files (*.*)|*.*||");
CFileDialog dlg(FALSE, NULL, "", NULL, strFilter);
if (dlg.DoModal() == IDOK)
{
strFilePath = dlg.GetPathName();
if (strFilePath.Find(strExt) == -1)//查找扩展名,如果没有输入则自动加
{
strFilePath += strExt;
}
if ( access(strFilePath, 0) == 0 )//需要#include <io.h>
{
CString strQuery;
strQuery.Format("%s 已经存在,要替换掉吗?", strFilePath);
if ( IDNO == ::MessageBox(m_hWnd, strQuery, "文件覆盖询问", MB_ICONQUESTION | MB_YESNO) )
{
return;
}
}
FILE *fp = fopen(strFilePath, "wt+");
if ( fp != NULL )
{
long lRow1 ;
long lRowCount = m_FlexGrid.GetRows();
long lCol1 ;
long lColCount = m_FlexGrid.GetCols();
for (lRow1 = 1; lRow1 < lRowCount; lRow1++)
{
m_FlexGrid.SetRow(lRow1);//设置焦点到当前列
for(lCol1 = 1; lCol1 < lColCount; lCol1++)
{
m_FlexGrid.SetCol(lCol1);//设置焦点到当前行
CString strMail = m_FlexGrid.GetText();
fputs(strMail, fp);
fputs("\r ", fp);
}
fputs("\n",fp);//逐个保存数据
}
// fputs("测试代码。。。", fp);
fclose(fp);
}
else
{
MessageBox("文件错误或者未能找到文件");
}
}
以上为对话框中程序 调试无问题
ON_COMMAND(ID_DATA_SAVE, OnDataSave)
void CMainFrame::OnDataSave()
{
// TODO: Add your command handler code here
CTest1Dlg *dlg;
dlg->OnButton1()
}
以上为菜单相应程序
编译说不能访问protect方法
将onbutton1方法改为public方法编译可通过 运行出错
请问这个应该如何解决
本人也是刚刚开始学的