/// <summary> /// Runs the dialog. /// </summary> /// <param name="owner">The owner.</param> /// <returns></returns> private DialogResult RunDialog(IWin32Window owner) { _ownerWindow = owner; InitializeDialog(); //show the dialog bool result = this.DialogType == VersionFileDialogType.OpenFileDialog ? NativeMethods.GetOpenFileName(ref openFileName) : NativeMethods.GetSaveFileName(ref openFileName); if (!result) { int ret = NativeMethods.CommDlgExtendedError(); if (ret != 0) { throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString()); } return(DialogResult.Cancel); } if (_selectedVersionIndex < 0 && this.VersionIsRequired) { throw new VersionNotSelectedException(); } this.charBuffer = null; if (openFileName.lpstrFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(openFileName.lpstrFile); } return(DialogResult.OK); }
/// <summary> /// Gets the multiselect files. /// </summary> /// <param name="charBuffer">The char buffer.</param> /// <returns></returns> private string[] GetMultiselectFiles(NativeMethods.CharBuffer charBuffer) { string text1 = charBuffer.GetString(); string text2 = charBuffer.GetString(); if (text2.Length == 0) { return(new string[] { text1 }); } if (text1[text1.Length - 1] != '\\') { text1 = text1 + @"\"; } ArrayList list1 = new ArrayList(); while (true) { if ((text2[0] != '\\') && (((text2.Length <= 3) || (text2[1] != ':')) || (text2[2] != '\\'))) { text2 = text1 + text2; } list1.Add(text2); text2 = charBuffer.GetString(); if (text2.Length <= 0) { string[] textArray1 = new string[list1.Count]; list1.CopyTo(textArray1, 0); return(textArray1); } } }
/// <summary> /// Initializes the dialog. /// </summary> private void InitializeDialog() { openFileName = new OpenFileName(); this.charBuffer = NativeMethods.CharBuffer.CreateBuffer(0x2000); openFileName.lStructSize = Marshal.SizeOf(openFileName); openFileName.lpstrFilter = Filter.Replace('|', '\0') + '\0'; openFileName.lpstrFile = this.charBuffer.AllocCoTaskMem(); openFileName.nMaxFile = 0x2000; if (_fileNames != null && _fileNames.Length > 0 && !string.IsNullOrEmpty(_fileNames[0])) { openFileName.lpstrFileTitle = System.IO.Path.GetFileName(_fileNames[0]) + new string ( '\0', 512 ); } else { openFileName.lpstrFileTitle = new string ( '\0', 512 ); } openFileName.nMaxFileTitle = openFileName.lpstrFileTitle.Length; if (this.DialogType == VersionFileDialogType.OpenFileDialog && string.IsNullOrEmpty(this._title)) { openFileName.lpstrTitle = "Open File"; } else if (this.DialogType == VersionFileDialogType.SaveFileDialog && string.IsNullOrEmpty(this._title)) { openFileName.lpstrTitle = "Save File As..."; } else { openFileName.lpstrTitle = this._title; } if (this.DialogType == VersionFileDialogType.OpenFileDialog && string.IsNullOrEmpty(this.OkButtonText)) { this.OkButtonText = "&Open"; } else if (this.DialogType == VersionFileDialogType.SaveFileDialog && string.IsNullOrEmpty(this.OkButtonText)) { this.OkButtonText = "&Save"; } if (string.IsNullOrEmpty(this.CancelButtonText)) { this.CancelButtonText = "&Cancel"; } openFileName.lpstrDefExt = _defaultExt; //position the dialog above the active window openFileName.hwndOwner = _ownerWindow != null ? _ownerWindow.Handle : IntPtr.Zero; //we need to find out the active screen so the dialog box is //centred on the correct display _activeScreen = Screen.FromControl(Form.ActiveForm); SetDialogFlags(); //this is where the hook is set. Note that we can use a C# delegate in place of a C function pointer openFileName.lpfnHook = new WndHookProc(HookProc); //if we're running on Windows 98/ME then the struct is smaller if (System.Environment.OSVersion.Platform != PlatformID.Win32NT) { openFileName.lStructSize -= 12; } }