public DialogResult ShowDialog(IWin32Window owner) { DialogResult returnDialogResult = DialogResult.Cancel; if (this.IsDisposed) { return(returnDialogResult); } if (owner == null || owner.Handle == IntPtr.Zero) { WindowWrapper wr = new WindowWrapper(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); owner = wr; } OriginalCtrlSize = this.Size; _MSdialog = (FileDlgType == FileDialogType.OpenFileDlg) ? new OpenFileDialog() as FileDialog : new SaveFileDialog() as FileDialog; _dlgWrapper = new WholeDialogWrapper(this); OnPrepareMSDialog(); if (!_hasRunInitMSDialog) { InitMSDialog(); } try { System.Reflection.PropertyInfo AutoUpgradeInfo = MSDialog.GetType().GetProperty("AutoUpgradeEnabled"); if (AutoUpgradeInfo != null) { AutoUpgradeInfo.SetValue(MSDialog, false, null); } returnDialogResult = _MSdialog.ShowDialog(owner); } // Sometimes if you open a animated .gif on the preview and the Form is closed, .Net class throw an exception // Lets ignore this exception and keep closing the form. catch (ObjectDisposedException) { } catch (Exception ex) { _eh.ThrowException(MessageBoxIcon.Error, $"Unable to get the modal dialog handle: { ex.Message }", "Error", MessageBoxButtons.OK); } return(returnDialogResult); }
private void InitMSDialog() { System.Reflection.PropertyInfo AutoUpgradeInfo = MSDialog.GetType().GetProperty("AutoUpgradeEnabled"); if (AutoUpgradeInfo != null) { AutoUpgradeInfo.SetValue(MSDialog, false, null); } MSDialog.InitialDirectory = _InitialDirectory.Length == 0 ? Path.GetDirectoryName(Application.ExecutablePath) : _InitialDirectory; MSDialog.AddExtension = _AddExtension; MSDialog.Filter = _Filter; MSDialog.FilterIndex = _FilterIndex; MSDialog.CheckFileExists = _CheckFileExists; MSDialog.DefaultExt = _DefaultExt; MSDialog.FileName = _FileName; MSDialog.DereferenceLinks = _DereferenceLinks; MSDialog.ShowHelp = _ShowHelp; _hasRunInitMSDialog = true; }
public DialogResult ShowDialog(IWin32Window owner) { DialogResult returnDialogResult = DialogResult.Cancel; if (this.IsDisposed) { return(returnDialogResult); } if (owner == null || owner.Handle == IntPtr.Zero) { WindowWrapper wr = new WindowWrapper(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); owner = wr; } OriginalCtrlSize = this.Size; // Tried to port vista filedialog... //CustomCon //var r = new Reflector("System.Windows.Forms"); //uint num = 0; //Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog"); //object dialog = r.Call(ofd, "CreateVistaDialog"); //r.Call(ofd, "OnBeforeVistaDialog", dialog); //uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions"); //options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS"); //r.CallAs(typeIFileDialog, dialog, "SetOptions", options); //object pfde = r.New("FileDialog.VistaDialogEvents", ofd); //object[] parameters = new object[] { pfde, num }; //r.CallAs2(typeIFileDialog, dialog, "Advise", parameters); //num = (uint)parameters[1]; //try //{ // int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner); // flag = 0 == num2; //} //finally //{ // r.CallAs(typeIFileDialog, dialog, "Unadvise", num); // GC.KeepAlive(pfde); //} _MSdialog = new OpenFileDialog(); _MSdialog.AutoUpgradeEnabled = true; _dlgWrapper = new WholeDialogWrapper(this); OnPrepareMSDialog(); if (!_hasRunInitMSDialog) { InitMSDialog(); } try { System.Reflection.PropertyInfo AutoUpgradeInfo = MSDialog.GetType().GetProperty("AutoUpgradeEnabled"); if (AutoUpgradeInfo != null) { AutoUpgradeInfo.SetValue(MSDialog, false, null); } returnDialogResult = _MSdialog.ShowDialog(owner); } // Sometimes if you open a animated .gif on the preview and the Form is closed, .Net class throw an exception // Lets ignore this exception and keep closing the form. catch (ObjectDisposedException) { } catch (Exception ex) { MessageBox.Show("unable to get the modal dialog handle", ex.Message); } return(returnDialogResult); }