internal static extern bool GetOpenFileName(ref OpenFileName ofn);
internal static extern bool GetSaveFileName(ref OpenFileName lpofn);
/// <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; } }