例如:
【程序】
// failcreating.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <Windows.h>
#define MAX_LOADSTRING 1024
using namespace std;
void displayDirectory()
{
TCHAR szDirectory[MAX_LOADSTRING];
GetCurrentDirectory(MAX_LOADSTRING, szDirectory);
cout << "当前工作目录: " << endl;
wcout << szDirectory << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
displayDirectory();
OPENFILENAME ofn;
TCHAR szFile[1024] = {0};
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = TEXT("图像文件\0*.png;*.jpg;*.gif;*.bmp\0所有文件\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = TEXT("选择怪物图像");
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn))
{
cout << "您选择了文件: ";
wcout << szFile;
}
else
cout << "您没有选择文件" << endl;
displayDirectory();
system("pause");
return 0;
}
【运行】
当前工作目录:
E:\Users\Octopus\Documents\Visual Studio 2012\Projects\failcreating\failcreating
您选择了文件: E:\Users\Octopus\Pictures\Downloaded\adaf2edda3cc7cd9441fa8e43e012
13fb80e91d9.jpg
当前工作目录:
E:\Users\Octopus\Pictures\Downloaded
Press any key to continue . . .