收藏到CSDN网摘
c++打开选择文件对话框
或者直接这样调用
#include这是原来的函数#include #include #include using namespace std; #pragma comment(lib, "Comdlg32.lib") // Gobal Variables and declarations. // int main() { OPENFILENAME ofn ; // a another memory buffer to contain the file name char szFile[100] ; // open a file name ZeroMemory( &ofn , sizeof( ofn)); ofn.lStructSize = sizeof ( ofn ); ofn.hwndOwner = NULL ; ofn.lpstrFile = szFile ; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof( szFile ); ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"; ofn.nFilterIndex = 1; ofn.lpstrTitle = "test title" ; ofn.nMaxFileTitle = 0 ; ofn.lpstrInitialDir=NULL ; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST ; GetOpenFileName( &ofn ); // Now simpley display the file name string fname(ofn.lpstrFile); cout << fname << endl; }
// get file name dialog string GetFile( string &title ) { // returned file name char filename[255]; // title of the dialog char lpsTitle[255]; strcpy(lpsTitle,(LPCSTR)title.c_str()); // initial path LPCSTR initialPath = "C:\\"; // strucutre OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFile = filename; ofn.lpstrFile[0] = '\0'; // doesn't use filename content to initialise itself ofn.nMaxFile = sizeof(filename); ofn.lpstrFilter = "XML File (*.xml)\0*.xml\0"; ofn.lpstrFileTitle = lpsTitle; ofn.lpstrInitialDir = initialPath; ofn.Flags = OFN_PATHMUSTEXIST | // path must exist OFN_FILEMUSTEXIST | // file must exist OFN_DONTADDTORECENT | // don't add to recent file OFN_ENABLESIZING | // enable size the dialog OFN_NONETWORKBUTTON; // no network path if (GetOpenFileName(&ofn)==TRUE) { string file(ofn.lpstrFile); return file; } else { return ""; } }
No comments :
Post a Comment