示例#1
0
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch (id) {
    case ID_FILE_OPEN:
    {
        IFileDialog *pFileDialog = NULL;
        HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
        if (SUCCEEDED(hr)) {
            COMDLG_FILTERSPEC filter[3];
            filter[0].pszName = L"位图文件(*.bmp)";
            filter[0].pszSpec = L"*.bmp";
            filter[1].pszName = L"JPEG(*.jpg;*.jpeg)";
            filter[1].pszSpec = L"*.jpg;*.jpeg";
            filter[2].pszName = L"所有支持的文件(*.jpg;*.jpeg;*.bmp;*.png)";
            filter[2].pszSpec = L"*.jpg;*.jpeg;*.bmp;*.png";

            hr = pFileDialog->SetFileTypes(3, filter);
            hr = pFileDialog->SetFileTypeIndex(3);
            hr = pFileDialog->Show(hwnd);
            if (SUCCEEDED(hr)) {
                IShellItem *psi = NULL;
                hr = pFileDialog->GetResult(&psi);
                if (SUCCEEDED(hr)) {
                    PWSTR pwszFilePath = NULL;
                    hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pwszFilePath);
                    if (SUCCEEDED(hr)) {
                        wchar_t * pext = wcsrchr(pwszFilePath, L'.');
                        if(pext != NULL && _wcsicmp(pext, L".bmp") == 0)
                            SendMessage(hwnd, WM_USER_IMG, 0, (LPARAM)pwszFilePath);
                        else
                            SendMessage(hwnd, WM_USER_IMG, 1, (LPARAM)pwszFilePath);
                        CoTaskMemFree(pwszFilePath);
                    }
                    psi->Release();
                }
            }
        }
        pFileDialog->Release();
        
        break;
    }
    case ID_FILE_EXIT:
        PostQuitMessage(0);
        break;
    default:
        FORWARD_WM_COMMAND(hwnd, id, hwndCtl, codeNotify, DefWindowProc);
    }
}
示例#2
0
文件: canvas.c 项目: snapbug/atire
/*
	ANT_CANVAS::LOAD_FILE()
	-----------------------
*/
long long ANT_canvas::load_file(void)
{
long long lines;
IFileDialog *pfd = NULL;
IShellItem *psiResult;
DWORD dwFlags;
PWSTR pszFilePath = NULL;
char chosen_filter[1024];
char chosen_filename[MAX_PATH];
OPENFILENAME parameters;

/*
	Create the Open Dialg object IFileDialog
*/
if ((CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)) == S_OK))
	{
	/*
		we're on VISTA / Windows 7 or later
	*/
	const COMDLG_FILTERSPEC c_rgSaveTypes[] =
	{
	{L"C/C++ files",		L"*.c;*.cxx;*.cpp;*.h;*.hpp.*.hxx;*.mak;makefile"},
	{L"All Documents (*.*)",		L"*.*"}
	};

	pfd->GetOptions(&dwFlags);
	pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);					// we want to see a file
	pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
	pfd->SetFileTypeIndex(1);										// first item in the list is the default
	pfd->SetDefaultExtension(L"doc;docx");
	if (pfd->Show(NULL) == S_OK)
		{
		if (pfd->GetResult(&psiResult) == S_OK)						// get the result object if the user clicks "Open"
			{
			psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

			lines = file->read_file((char *)pszFilePath);

			CoTaskMemFree(pszFilePath);
			psiResult->Release();
			}
		}
	pfd->Release();

	return lines;
	}
else
	{
	/*
		we're on something prior to VISTA (NT through to XP) so use the old-style common control
	*/
	memset(chosen_filename, 0, sizeof(chosen_filename));

	parameters.lStructSize = sizeof(parameters);
	parameters.hwndOwner = window;
	parameters.hInstance = hInstance;
	parameters.lpstrFilter = L"C/C++ files\0*.c;*.cxx;*.cpp;*.h;*.hpp.*.hxx\0\0\0";
	parameters.lpstrCustomFilter = (LPWSTR)chosen_filter;
	parameters.nMaxCustFilter = sizeof(chosen_filter) - 1;
	parameters.nFilterIndex = 1;
	parameters.lpstrFile = (LPWSTR)chosen_filename;
	parameters.nMaxFile = sizeof(chosen_filename) - 1;
	parameters.lpstrFileTitle = NULL;
	parameters.nMaxFileTitle = 0;
	parameters.lpstrInitialDir = NULL;
	parameters.lpstrTitle = L"Open...";
	parameters.Flags = OFN_LONGNAMES;
	parameters.nFileOffset = 0;
	parameters.nFileExtension = 0;
	parameters.lpstrDefExt = NULL;
	parameters.lCustData = 0;
	parameters.lpfnHook = NULL;
	parameters.lpTemplateName = 0;
	#if (_WIN32_WINNT >= 0x0500)
		parameters.pvReserved = NULL;
		parameters.dwReserved = 0;
		parameters.FlagsEx = 0;
	#endif

	if ((GetOpenFileNameW(&parameters)) != 0)
		return file->read_file((char *)parameters.lpstrFile);
	}

return 0;
}
示例#3
0
//-----------------------------------------------------------------------------
bool VistaFileSelector::runModalInternal ()
{
	fileDialog = 0;
	HRESULT hr = -1;
	if (style == kSelectSaveFile)
	{
		hr = CoCreateInstance (CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog));
		if (defaultSaveName)
		{
			fileDialog->SetFileName (UTF8StringHelper (defaultSaveName));
		}
	}
	else
	{
		hr = CoCreateInstance (CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog));
		if (SUCCEEDED (hr))
		{
			if (style == kSelectDirectory)
			{
				DWORD dwOptions = 0;
				hr = fileDialog->GetOptions (&dwOptions);
				if (SUCCEEDED (hr))
					hr = fileDialog->SetOptions (dwOptions | FOS_PICKFOLDERS);
				if (FAILED (hr))
				{
					fileDialog->Release ();
					fileDialog = 0;
					return false;
				}
			}
			if (allowMultiFileSelection)
			{
				DWORD dwOptions = 0;
				hr = fileDialog->GetOptions (&dwOptions);
				if (SUCCEEDED (hr))
					hr = fileDialog->SetOptions (dwOptions | FOS_ALLOWMULTISELECT);
				if (FAILED (hr))
				{
					fileDialog->Release ();
					fileDialog = 0;
					return false;
				}
			}
		}
	}
	if (FAILED (hr))
	{
		fileDialog = 0;
		return false;
	}

	if (title)
		hr = fileDialog->SetTitle (UTF8StringHelper (title));

	DWORD numExtensions = 0;
	DWORD defaultFileTypeIndex = 0;
	COMDLG_FILTERSPEC* filters = buildExtensionFilter (extensions, defaultExtension, numExtensions, defaultFileTypeIndex);
	if (filters)
	{
		fileDialog->SetFileTypes (numExtensions, filters);
		if (defaultFileTypeIndex)
			fileDialog->SetFileTypeIndex (defaultFileTypeIndex);
	}
	if (initialPath && _SHCreateItemFromParsingName)
	{
		IShellItem* shellItem;
		hr = _SHCreateItemFromParsingName (UTF8StringHelper (initialPath), 0, IID_PPV_ARG (IShellItem, &shellItem));
		if (SUCCEEDED (hr))
		{
			fileDialog->SetFolder (shellItem);
			shellItem->Release ();
		}
	}
	Win32Frame* win32Frame = frame->getPlatformFrame () ? dynamic_cast<Win32Frame*> (frame->getPlatformFrame ()) : 0;
	hr = fileDialog->Show (win32Frame ? win32Frame->getPlatformWindow () : 0);
	if (SUCCEEDED (hr))
	{
		if (allowMultiFileSelection)
		{
			IFileOpenDialog* openFileDialog = 0;
			hr = fileDialog->QueryInterface (IID_PPV_ARG(IFileOpenDialog, &openFileDialog));
			if (SUCCEEDED (hr))
			{
				IShellItemArray* items;
				hr = openFileDialog->GetResults (&items);
				if (SUCCEEDED (hr))
				{
					DWORD count;
					hr = items->GetCount (&count);
					for (DWORD i = 0; i < count; i++)
					{
						IShellItem* item;
						hr = items->GetItemAt (i, &item);
						if (SUCCEEDED (hr))
						{
							LPWSTR filesysPath = 0;
							hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath);
							if (SUCCEEDED (hr))
							{
								UTF8StringHelper str (filesysPath);
								UTF8StringBuffer resultPath = String::newWithString (str);
								result.push_back (resultPath);
							}
							item->Release ();
						}
					}
					items->Release ();
				}
				openFileDialog->Release ();
			}
		}
		else
		{
			IShellItem *item;
			hr = fileDialog->GetResult (&item);
			if (SUCCEEDED (hr))
			{
				LPWSTR filesysPath = 0;
				hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath);
				if (SUCCEEDED (hr))
				{
					UTF8StringHelper str (filesysPath);
					UTF8StringBuffer resultPath = String::newWithString (str);
					result.push_back (resultPath);
				}
				item->Release ();
			}
		}
	}
	fileDialog->Release ();
	fileDialog = 0;
	freeExtensionFilter (filters);
	return SUCCEEDED (hr);
}
示例#4
0
HRESULT BasicFileOpen(PWSTR* filePath)
{
	// CoCreate the File Open Dialog object.
	IFileDialog *pfd = NULL;
	HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, 
		NULL, 
		CLSCTX_INPROC_SERVER, 
		IID_PPV_ARGS(&pfd));
	if (SUCCEEDED(hr))
	{
		// Create an event handling object, and hook it up to the dialog.
		IFileDialogEvents *pfde = NULL;
		//DWORD dwCookie;
		// Set the options on the dialog.
		DWORD dwFlags;

		// Before setting, always get the options first in order 
		// not to override existing options.
		hr = pfd->GetOptions(&dwFlags);
		if (SUCCEEDED(hr))
		{
			// In this case, get shell items only for file system items.
			hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
			if (SUCCEEDED(hr))
			{
				static const COMDLG_FILTERSPEC c_rgSaveTypes[] =
				{
					{L"Executable Files(*.exe)",       L"*.exe"},
				};
				// Set the file types to display only. 
				// Notice that this is a 1-based array.
				hr = pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
				if (SUCCEEDED(hr))
				{
					// Set the selected file type index to Word Docs for this example.
					hr = pfd->SetFileTypeIndex(1);
					if (SUCCEEDED(hr))
					{
						// Set the default extension to be ".doc" file.
						hr = pfd->SetDefaultExtension(L"exe");
						if (SUCCEEDED(hr))
						{
							// Show the dialog
							hr = pfd->Show(g_hwnd);
							if (SUCCEEDED(hr))
							{
								// Obtain the result once the user clicks 
								// the 'Open' button.
								// The result is an IShellItem object.
								IShellItem *psiResult;
								hr = pfd->GetResult(&psiResult);
								if (SUCCEEDED(hr))
								{
									// We are just going to print out the 
									// name of the file for sample sake.
									PWSTR pszFilePath = NULL;
									hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, 
										&pszFilePath);
									if (SUCCEEDED(hr))
									{
										*filePath=pszFilePath;
										
									}
									psiResult->Release();
								}
							}
						}
					}
				}
			}
		}
		pfd->Release();
	}
	else
	{
		hr=E_OUTOFMEMORY;
		OPENFILENAME ofn;
		LPWSTR pszFile=(LPWSTR)CoTaskMemAlloc(260*2);
		if(pszFile!=NULL)
		{
			memset1(&ofn,0,sizeof(ofn));
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = g_hwnd;
			ofn.lpstrFile = pszFile;
			// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
			// use the contents of szFile to initialize itself.
			ofn.lpstrFile[0] = L'\0';
			ofn.nMaxFile = 260;
			ofn.lpstrFilter = L"Executable Files(*.exe)\0*.exe\0";
			ofn.nFilterIndex = 1;
			ofn.lpstrFileTitle = NULL;
			ofn.nMaxFileTitle = 0;
			ofn.lpstrInitialDir = NULL;
			ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

			if (GetOpenFileName(&ofn)==TRUE)
			{
				*filePath=pszFile;
				hr=0;
			}
		}

	}
	return hr;
}