C++MFC开发程序,使运行时首先弹出对话框,代码应写哪

2024-11-23 11:51:59
推荐回答(4个)
回答1:

InitInstance() 在这个函数上添吧。mfc的winmain被隐藏了,所以找不着。。如果你不是用vc添加的mfc的话就是winmain了。

回答2:

C++是门语言,VC6是一门基于C++语言的工具,你需要专门去学一下怎么使用VC6这门工具,这和学C++语言本身是不一样的.

回答3:

建议先看 孙鑫C++视频吧 很有用

回答4:

用 boost的filesystem吧
//FileIO.h
#ifndef __FILE_IO_H__
#define __FILE_IO_H__

#include
#include
#include
#include "clysedef.hpp"
#include
#include

class FileIO
{
public:
FileIO(void);
virtual ~FileIO(void);
static void writeFile(const std::string& fname, const char* buffer, u32 size);
static u8* readFile(const std::string& fname, u32& size);
static bool createDirectory(const std::string& dpathstr, const std::string& dname);
static bool renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname);
static bool removeDirectory(const std::string& dpathstr, const std::string& dname);
static bool copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr);
static bool removeFile(const std::string& dpathstr, const std::string& fpathstr);
static bool cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr);
static bool renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname);
static void serchfile(const std::string& dpathstr, std::vector& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileBackFileName(const std::string& dpathstr, std::vector& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileCountCtrl(const std::string& dpathstr, std::vector& fnames, boost::function< bool(const std::string&)> foo, u32 count);
static bool isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo);
static bool isExistThisFile(const std::string dpathstr, const std::string& fname);

};

#endif // __FILE_IO_H__

//FileIO.cpp
#include "FileIO.h"

#include
#include
#include
#include

FileIO::FileIO(void)
{
}

FileIO::~FileIO(void)
{

}

void FileIO::writeFile(const std::string& fname, const char* buffer, u32 size)
{
if(fname.empty())
{
return;
}

try
{
std::ofstream opf;
opf.open(fname.c_str(), std::ios::out | std::ios::binary);
//opf.write((char*)(&size), sizeof(u32));

opf.write(buffer, size);
opf.close();
}
catch(const std::exception& ex)
{
std::cout << ex.what()< }
};

u8* FileIO::readFile(const std::string& fname, u32& size)
{
if(fname.empty())
{
return 0;
}
u8* buffer = 0;

try
{
boost::filesystem::path fpath(fname.c_str(), boost::filesystem::native);
u32 rsize = boost::filesystem::file_size(fpath);

std::ifstream ipf;
ipf.open(fname.c_str(), std::ios::in | std::ios::binary);
//ipf.read((char*)(&rsize), sizeof(u32));
buffer = new u8[rsize];
ipf.read((char*)buffer, rsize);
ipf.close();
size = rsize;
}
catch(const std::exception& ex)
{
std::cout << ex.what()< size = 0;
if(buffer)
delete[] buffer;

buffer = 0;
}
return buffer;
};

bool FileIO::createDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;

std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}

full_path += dname;

if(full_path.empty())
{
return true;
}

boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
if(boost::filesystem::exists(dpath))
{
return true;
}

try
{
bret = boost::filesystem::create_directory(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
bret = false;
}

full_path.clear();
return bret;
};

bool FileIO::renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname)
{
bool bret = true;
std::string full_path_old = dpath;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}

std::string full_path_new = full_path_old;

full_path_old += oldname;
full_path_new += newname;

if(full_path_old.empty() || full_path_new.empty())
{
return false;
}

boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);

if(!(boost::filesystem::is_directory(oldpath)))
{
return false;
}
try
{
boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << oldpath.string() << " To " << newpath.string() << " " << ex.what() << std::endl;
bret = false;
}

return bret;
};

bool FileIO::removeDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;

std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}
full_path += dname;
boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
try
{
bret = boost::filesystem::remove(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}

full_path.clear();
return bret;
};

bool FileIO::copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr)
{
bool bret = true;
std::string srcfullpath = srcpathstr;
std::string tagfullpath = tagpathstr;

if((!(srcfullpath.empty())) && (srcfullpath[srcfullpath.size() - 1] != '/'))
{
srcfullpath += "/";
}

if((!(tagfullpath.empty())) && (tagfullpath[tagfullpath.size() - 1] != '/'))
{
tagfullpath += "/";
}

srcfullpath += srcfilestr;
tagfullpath += tagfilestr;

boost::filesystem::path srcpath(srcfullpath.c_str(), boost::filesystem::native);
boost::filesystem::path tagpath(tagfullpath.c_str(), boost::filesystem::native);

try
{
boost::filesystem::copy_file(srcpath, tagpath);
}
catch(const std::exception& ex)
{
std::cout << srcpath.string() << " TO " << tagpath.string() << " " << ex.what() << std::endl;
bret = false;
}

return bret;
}

bool FileIO::removeFile(const std::string& dpathstr, const std::string& fpathstr)
{
bool bret = false;
std::string fullpathstr = dpathstr;
if((!(fullpathstr.empty())) && (fullpathstr[fullpathstr.size() - 1] != '/'))
{
fullpathstr += "/";
}
fullpathstr += fpathstr;

boost::filesystem::path fullpath(fullpathstr.c_str(), boost::filesystem::native);

if(boost::filesystem::is_directory(fullpath))
{
return false;
}

try
{
bret = boost::filesystem::remove(fullpath);
}
catch(const std::exception& ex)
{
std::cout << fullpath.string() << " " << ex.what() << std::endl;
}

return bret;
}

bool FileIO::cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr)
{
bool bret = false;

try
{
bret |= copyFile(srcpathstr, srcfilestr, tagpathsrc, tagfilestr);
bret |= removeFile(srcpathstr, srcfilestr);
}
catch(const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}

return bret;

}

bool FileIO::renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname)
{
bool bret = true;
std::string full_path_old = dpathstr;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}

std::string full_path_new = full_path_old;

full_path_old += srcname;
full_path_new += tagname;

if(full_path_old.empty() || full_path_new.empty())
{
return false;
}

try
{
boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);

if(boost::filesystem::is_directory(oldpath))
{
return false;
}

boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << ex.what() << std::endl;
bret = false;
}

return bret;

};

void FileIO::serchfile(const std::string& dpathstr, std::vector& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);

try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}

if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->string());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};

void FileIO::serchfileBackFileName(const std::string& dpathstr, std::vector& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);

try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}

if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->leaf());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};

void FileIO::serchfileCountCtrl(const std::string& dpathstr, std::vector& fnames,
boost::function< bool(const std::string&)> foo, u32 count)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
u32 ncount = 0;
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}

if(foo(dir_iter->leaf()) && (ncount < count))
{
fnames.push_back(dir_iter->string());
ncount++;
}
else
{
if(ncount < count)
{
return;
}
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};

bool FileIO::isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);

try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}

if(foo(dir_iter->leaf()))
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}

return false;
};

bool FileIO::isExistThisFile(const std::string dpathstr, const std::string& fname)
{
std::string srcstr = dpathstr + fname;
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);

try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}

std::string tstr = (*dir_iter).string();

if(srcstr == tstr)
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}

return false;
};

以上是用BOOST和文件操作
至于如何运行 你就写个对话框之类的程序调用就行了