private void SetDialogProperties(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { // Description if (!string.IsNullOrEmpty(_description)) { if (_useDescriptionForTitle) { dialog.SetTitle(_description); } else { Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize customize = (Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize)dialog; customize.AddText(0, _description); } } dialog.SetOptions(NativeMethods.FOS.FOS_PICKFOLDERS | NativeMethods.FOS.FOS_FORCEFILESYSTEM | NativeMethods.FOS.FOS_FILEMUSTEXIST); if (!string.IsNullOrEmpty(_selectedPath)) { string parent = Path.GetDirectoryName(_selectedPath); if (parent == null || !Directory.Exists(parent)) { dialog.SetFileName(_selectedPath); } else { string folder = Path.GetFileName(_selectedPath); dialog.SetFolder(NativeMethods.CreateItemFromParsingName(parent)); dialog.SetFileName(folder); } } }
internal override void GetResult(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { if (Multiselect) { Ookii.Dialogs.WinForms.Interop.IShellItemArray results; ((Ookii.Dialogs.WinForms.Interop.IFileOpenDialog)dialog).GetResults(out results); uint count; results.GetCount(out count); string[] fileNames = new string[count]; for (uint x = 0; x < count; ++x) { Ookii.Dialogs.WinForms.Interop.IShellItem item; results.GetItemAt(x, out item); string name; item.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out name); fileNames[x] = name; } FileNamesInternal = fileNames; } else { FileNamesInternal = null; } if (ShowReadOnly) { Interop.IFileDialogCustomize customize = (Interop.IFileDialogCustomize)dialog; int selected; customize.GetSelectedControlItem(_openDropDownId, out selected); _readOnlyChecked = (selected == _readOnlyItemId); } base.GetResult(dialog); }
/// <summary> /// Specifies a common dialog box. /// </summary> /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param> /// <returns><see langword="true" /> if the file could be opened; otherwise, <see langword="false" />.</returns> protected override bool RunDialog(IntPtr hwndOwner) { if (_downlevelDialog != null) { return(_downlevelDialog.ShowDialog(hwndOwner == IntPtr.Zero ? null : new WindowHandleWrapper(hwndOwner)) == DialogResult.OK); } Ookii.Dialogs.WinForms.Interop.IFileDialog dialog = null; try { dialog = new Ookii.Dialogs.WinForms.Interop.NativeFileOpenDialog(); SetDialogProperties(dialog); int result = dialog.Show(hwndOwner); if (result < 0) { if ((uint)result == (uint)HRESULT.ERROR_CANCELLED) { return(false); } else { throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result); } } GetResult(dialog); return(true); } finally { if (dialog != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog); } } }
private bool RunFileDialog(IntPtr hwndOwner) { _hwndOwner = hwndOwner; Ookii.Dialogs.WinForms.Interop.IFileDialog dialog = null; try { dialog = CreateFileDialog(); SetDialogProperties(dialog); int result = dialog.Show(hwndOwner); if (result < 0) { if ((uint)result == (uint)HRESULT.ERROR_CANCELLED) { return(false); } else { throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result); } } return(true); } finally { _hwndOwner = IntPtr.Zero; if (dialog != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog); } } }
internal bool DoFileOk(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { GetResult(dialog); System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs(); OnFileOk(e); return(!e.Cancel); }
internal virtual void SetDialogProperties(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { uint cookie; dialog.Advise(new VistaFileDialogEvents(this), out cookie); // Set the default file name if (!(_fileNames == null || _fileNames.Length == 0 || string.IsNullOrEmpty(_fileNames[0]))) { dialog.SetFileName(_fileNames[0]); } // Set the default directory if (Directory.Exists(_initialDirectory)) { dialog.SetFolder(NativeMethods.CreateItemFromParsingName(_initialDirectory)); } // Set the filter if (!string.IsNullOrEmpty(_filter)) { string[] filterElements = _filter.Split(new char[] { '|' }); NativeMethods.COMDLG_FILTERSPEC[] filter = new NativeMethods.COMDLG_FILTERSPEC[filterElements.Length / 2]; for (int x = 0; x < filterElements.Length; x += 2) { filter[x / 2].pszName = filterElements[x]; filter[x / 2].pszSpec = filterElements[x + 1]; } dialog.SetFileTypes((uint)filter.Length, filter); if (_filterIndex > 0 && _filterIndex <= filter.Length) { dialog.SetFileTypeIndex((uint)_filterIndex); } } // Default extension if (_addExtension && !string.IsNullOrEmpty(_defaultExt)) { dialog.SetDefaultExtension(_defaultExt); } // ShowHelp if (_showHelp) { Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize customize = (Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize)dialog; customize.AddPushButton(HelpButtonId, "Help"); } if (!string.IsNullOrEmpty(_title)) { dialog.SetTitle(_title); } dialog.SetOptions((_options | NativeMethods.FOS.FOS_FORCEFILESYSTEM)); }
internal virtual void GetResult(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { if (!GetOption(NativeMethods.FOS.FOS_ALLOWMULTISELECT)) { _fileNames = new string[1]; Ookii.Dialogs.WinForms.Interop.IShellItem result; dialog.GetResult(out result); result.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out _fileNames[0]); } }
internal override void SetDialogProperties(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { base.SetDialogProperties(dialog); if (_showReadOnly) { Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize customize = (Ookii.Dialogs.WinForms.Interop.IFileDialogCustomize)dialog; customize.EnableOpenDropDown(_openDropDownId); customize.AddControlItem(_openDropDownId, _openItemId, ComDlgResources.LoadString(ComDlgResources.ComDlgResourceId.OpenButton)); customize.AddControlItem(_openDropDownId, _readOnlyItemId, ComDlgResources.LoadString(ComDlgResources.ComDlgResourceId.ReadOnly)); } }
private void GetResult(Ookii.Dialogs.WinForms.Interop.IFileDialog dialog) { Ookii.Dialogs.WinForms.Interop.IShellItem item; dialog.GetResult(out item); item.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out _selectedPath); }