文件打开(保存)对话框:GetOpenFileName和 GetSaveFileName

void Test() 
{
	TCHAR strFilter[]		= _T("Bitmap Files(*.bmp)\0*.bmp\0")
						_T("JPEG Files(*.jpeg;*jpg)\0*.jpeg;*.jpg\0")
						_T("PNG Files(*.png)\0*.png\0")
						_T("All Files(*.*)\0*.*\0");


	TCHAR strFile[MAX_PATH]	= _T("未命名");
	TCHAR strFileTitle[256]	= {0};
	TCHAR strInitiaDir[]		= _T("D:\\DeskTop");
	TCHAR strTitle[]			= _T("请选择");


	OPENFILENAME ofn;         
	ZeroMemory(&ofn, sizeof(ofn));


	// 指定结构的大小,以字节为单位
	ofn.lStructSize			= sizeof(ofn);


	/*Handle to the window that owns the dialog box. 
	This member can be any valid window handle, or it can be NULL if the dialog box has no owner.*/
	ofn.hwndOwner           = m_hWnd;	


	// Not supported.
	//ofn.hInstance;		


	// 过滤器字符串的长指针,需以两个空字符结尾
	ofn.lpstrFilter 		= strFilter;			


	// Not supported.
	//ofn.lpstrCustomFilter;	


	// Not supported.
	//ofn.nMaxCustFilter;	


	// 过滤器序号(由1开始,如果该值为0表示第一个)			
	ofn.nFilterIndex		= 3;			


	/*指向包含初始化文件名编辑控件使用的文件名的缓冲。如果不需要初始值,这个缓冲的第一个字符必须是NULL。
	当GetOpenFileName或GetSaveFileName函数返回成功时,这个缓冲包含驱动器,路径,文件名,及所选择的文件的扩展名。 
	If the buffer is too small, the function returns FALSE. In this case, 
	the first two bytes of the lpstrFile buffer contain the required size, in bytes or characters.*/
	ofn.lpstrFile			= strFile;		


	/*Specifies the size, in bytes (ANSI version) or 16-bit characters (Unicode version), of the buffer pointed to by lpstrFile. 
	The GetOpenFileName and GetSaveFileName functions return FALSE if the buffer is too small to contain the file information. 
	The buffer should be at least 256 characters long.*/
	ofn.nMaxFile			= sizeof(strFile);	


	// 指向接收选择的文件的文件名和扩展名的缓冲(不带路径信息)。这个成员可以是NULL。
	ofn.lpstrFileTitle		= strFileTitle;			


	/*指定lpstrFileTitle缓冲的大小,以TCHARs为单位。对于ANSI版本,是字节的个数;
	对于Unicode版本,是字节的个数。如果lpstrFileTitle是NULL,这个成员被忽略。*/
	ofn.nMaxFileTitle		= sizeof(strFileTitle);	


	// 打开对话框的初始目录,可以为NULL
	ofn.lpstrInitialDir		= strInitiaDir;	


	// 打开对话框标题栏的提示文字
	ofn.lpstrTitle			= strTitle;		


	/*A bitmask of flags used to initialize the dialog box. When the dialog box returns, 
	it sets these flags to indicate the user's input. This member can be a combination of the following flags.*/
	ofn.Flags				= 0;	


	// 指定从路径开始到通过lpstrFile指定的文件名字符串基于0的偏移
	ofn.nFileOffset			= 0;	


	// 指定从路径开始到通过lpstrFile指定的文件名字符串中扩展名基于0的偏移
	ofn.nFileExtension		= 0;					


	/*指向包含默认扩展名的缓冲。如果用户忘记输入扩展名,GetOpenFileName和GetSaveFileName附加这个扩展名到文件名中。
	这个字符串可以是任一长度,但但只有头三个字符被附加。字符串不应该包含一个句点(.)。
	如果这个成员是NULL并且用户忘记了输入一个扩展名,那么将没有扩展名被附加。*/
	ofn.lpstrDefExt			= _T("bmp");				
	
	//ofn.lCustData;								// Not supported.
	//ofn.lpfnHook;									// Not supported.
	//ofn.lpTemplateName;							// Not supported.




	CString str;
	if (GetOpenFileName(&ofn))
	{
		str = ofn.lpstrFile;
		AfxMessageBox(str);
	}


	if (GetSaveFileName(&ofn))
	{
		str = ofn.lpstrFile;
		AfxMessageBox(str);
	}
}





使用信号和槽机制可以很方便地实现文件保存打开功能。下面是一个简单的示例: 首先,创建一个自定义的类,例如 "FileManager",该类负责文件保存打开操作。在该类中定义两个槽函数,分别用于保存打开文件。 ```cpp class FileManager : public QObject { Q_OBJECT public slots: void saveFile(const QString& filePath) { // 实现保存文件的逻辑 // ... } void openFile(const QString& filePath) { // 实现打开文件的逻辑 // ... } }; ``` 接下来,在需要使用文件保存打开功能的类中,创建一个 FileManager 对象,并连接相应的信号和槽。 ```cpp // 在主窗口类中的构造函数或初始化函数中 FileManager* fileManager = new FileManager(this); // 连接信号和槽 connect(saveButton, &QPushButton::clicked, [=]() { QString filePath = QFileDialog::getSaveFileName(this, "Save File"); if (!filePath.isEmpty()) { fileManager->saveFile(filePath); } }); connect(openButton, &QPushButton::clicked, [=]() { QString filePath = QFileDialog::getOpenFileName(this, "Open File"); if (!filePath.isEmpty()) { fileManager->openFile(filePath); } }); ``` 在上述代码中,我们假设 saveButton 和 openButton 分别是用于触发保存打开文件操作的按钮。当点击这些按钮时,会弹出一个文件对话框用于选择保存打开文件路径。选择好文件路径后,会调用 FileManager 对象的相应槽函数进行文件保存打开操作。 当然,上述代码只是一个简单的示例,你可以根据实际需要进行修改和扩展。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值