Example #1
0
        public static string[] Show(Form parent)
        {
            if (Environment.OSVersion.Version.Major >= 6)  // vista and newer
            {
                try{
                    OpenFileDialog dialog = new OpenFileDialog {
                        Filter             = Lang.Get["DialogFilterFolders"] + FilterFolders,
                        CheckFileExists    = false,
                        CheckPathExists    = true,
                        DereferenceLinks   = true,
                        AddExtension       = false,
                        Multiselect        = true,
                        AutoUpgradeEnabled = true
                    };

                    Assembly assembly   = LoadAssembly("System.Windows.Forms");
                    Type     ifdType    = assembly.GetType("System.Windows.Forms.FileDialogNative").GetNestedType("IFileDialog", BindingFlags.NonPublic);
                    Type     fdType     = typeof(FileDialog);
                    Type     dialogType = dialog.GetType();

                    const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

                    var vistaDialog = dialogType.GetMethod("CreateVistaDialog", flags).Invoke(dialog, new object[0]);
                    dialogType.GetMethod("OnBeforeVistaDialog", flags).Invoke(dialog, new object[] { vistaDialog });

                    uint opts = (uint)fdType.GetMethod("GetOptions", flags).Invoke(dialog, new object[0]);
                    ifdType.GetMethod("SetOptions", flags).Invoke(vistaDialog, new object[] { opts | 32u }); // 32u = FOS_PICKFOLDERS

                    object   events     = fdType.GetNestedType("VistaDialogEvents", flags).GetConstructors()[0].Invoke(new object[] { dialog });
                    object[] parameters = { events, (uint)0 };
                    ifdType.GetMethod("Advise").Invoke(vistaDialog, parameters);
                    uint id = (uint)parameters[1];

                    try{
                        ifdType.GetMethod("Show").Invoke(vistaDialog, new object[] { parent.Handle });
                    }finally{
                        ifdType.GetMethod("Unadvise").Invoke(vistaDialog, new object[] { id });
                        GC.KeepAlive(events);
                    }

                    return(dialog.FileNames);
                }catch (Exception e) {
                    Debug.Write(e.ToString());
                }
            }

            var oldDialog = new FolderBrowserDialog {
                ShowNewFolderButton = false
            };

            return(oldDialog.ShowDialog(parent) == DialogResult.OK ? new string[] { oldDialog.SelectedPath } : new string[0]);
        }
Example #2
0
        /// <summary>
        /// Show a dialog which prompts the user to pick a PDF file to import
        /// </summary>
        /// <returns></returns>
        public string OpenFileDialog()
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                DefaultExt      = ".pdf",
                Filter          = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*",
                CheckFileExists = true,
            };

            bool res = (bool)dlg.GetType()
                       .GetMethod("RunDialog", BindingFlags.NonPublic | BindingFlags.Instance)
                       .Invoke(dlg, new object[] { Svc.SM.UI.ElementWdw.Handle });

            return(res //dlg.ShowDialog(this).GetValueOrDefault(false)
        ? dlg.FileName
        : null);
        }
Example #3
0
        internal DialogResult ShowDialogExt(OpenFileDialog fdlg, 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         = fdlg;
            _dlgWrapper      = new WholeDialogWrapper(this);

            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);
        }
Example #4
0
        public bool ShowDialog(IntPtr hWndOwner)
        {
            if (Environment.OSVersion.Version.Major >= 6)               // Vista/Win7/Win8/Win10
            {
                name_string = "System.Windows.Forms";
                assembly    = null;
                AssemblyName[] referencedAssemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
                foreach (AssemblyName assemblyName in referencedAssemblies)
                {
                    if (assemblyName.FullName.StartsWith(name_string))
                    {
                        assembly = Assembly.Load(assemblyName);
                        break;
                    }
                }

                Type   typeIFileDialog = GetType("FileDialogNative.IFileDialog");
                object dialog          = Call(OpenFileDir.GetType(), OpenFileDir, "CreateVistaDialog");
                Call(OpenFileDir.GetType(), OpenFileDir, "OnBeforeVistaDialog", dialog);

                uint options = (uint)Call(typeof(System.Windows.Forms.FileDialog), OpenFileDir, "GetOptions");
                options = options | (uint)GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
                Call(typeIFileDialog, dialog, "SetOptions", options);

                object pFileDialogEvent = New("FileDialog.VistaDialogEvents", OpenFileDir);

                uint num_parms = 0;

                object[] parameters = new object[] { pFileDialogEvent, num_parms };
                Call(typeIFileDialog, dialog, "Advise", parameters);

                num_parms = (uint)parameters[1];

                try
                {
                    // show the dialog
                    int count = (int)Call(typeIFileDialog, dialog, "Show", hWndOwner);
                    return(count == 0);
                }
                finally
                {
                    // remove event handler
                    Call(typeIFileDialog, dialog, "Unadvise", num_parms);
                    GC.KeepAlive(pFileDialogEvent);
                }
            }
            else              // XP and earlier
            {
                FolderBrowserDialog FolderBrowser = new FolderBrowserDialog();

                FolderBrowser.Description         = this.Title;
                FolderBrowser.SelectedPath        = this.InitialDirectory;
                FolderBrowser.ShowNewFolderButton = false;

                DialogResult dialog_result = FolderBrowser.ShowDialog();
                if (dialog_result == DialogResult.OK)
                {
                    OpenFileDir.FileName = FolderBrowser.SelectedPath;
                    return(true);
                }
            }
            return(false);
        }