天天看点

C++ 选择文件对话框 函数 GetOpenFileName 使用的一个demo

#include <windows.h>
#include <commdlg.h>

int main()  
{  
	//     
	OPENFILENAME ofn;      // 公共对话框结构。   
	TCHAR szFile[MAX_PATH]; // 保存获取文件名称的缓冲区。             
	// 初始化选择文件对话框。   
	ZeroMemory(&ofn, sizeof(OPENFILENAME));  
	ofn.lStructSize = sizeof(OPENFILENAME);  
	ofn.hwndOwner = NULL;  
	ofn.lpstrFile = szFile;  
	ofn.lpstrFile[0] = '\0'; 
	ofn.nMaxFile = sizeof(szFile);  
	ofn.lpstrFilter = "All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";  
	ofn.nFilterIndex = 1;  
	ofn.lpstrFileTitle = NULL;  
	ofn.nMaxFileTitle = 0;  
	ofn.lpstrInitialDir = NULL;  
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;  
	//ofn.lpTemplateName =  MAKEINTRESOURCE(ID_TEMP_DIALOG);  
	// 显示打开选择文件对话框。   

	if ( GetOpenFileName(&ofn) )  
	{  
		//显示选择的文件。   
		OutputDebugString(szFile);  
		OutputDebugString("\r\n");  
	} 
	system("pause");
	return 0;
}