private protected override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetSaveFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition switch (Comdlg32.CommDlgExtendedError()) { case Comdlg32.FNERR.INVALIDFILENAME: throw new InvalidOperationException(string.Format(SR.FileDialogInvalidFileName, FileName)); } } return(result); }
protected unsafe override bool RunDialog(IntPtr hwndOwner) { var hookProcPtr = new User32.WNDPROCINT(HookProc); var cc = new Comdlg32.CHOOSECOLORW { lStructSize = (uint)Marshal.SizeOf <Comdlg32.CHOOSECOLORW>() }; IntPtr custColorPtr = Marshal.AllocCoTaskMem(64); try { Marshal.Copy(_customColors, 0, custColorPtr, 16); cc.hwndOwner = hwndOwner; cc.hInstance = Instance; cc.rgbResult = ColorTranslator.ToWin32(_color); cc.lpCustColors = custColorPtr; Comdlg32.CC flags = (Comdlg32.CC)Options | Comdlg32.CC.RGBINIT | Comdlg32.CC.ENABLEHOOK; // Our docs say AllowFullOpen takes precedence over FullOpen; ChooseColor implements the opposite if (!AllowFullOpen) { flags &= ~Comdlg32.CC.FULLOPEN; } cc.Flags = flags; cc.lpfnHook = hookProcPtr; if (!Comdlg32.ChooseColorW(ref cc).IsTrue()) { return(false); } if (cc.rgbResult != ColorTranslator.ToWin32(_color)) { _color = ColorTranslator.FromOle(cc.rgbResult); } Marshal.Copy(custColorPtr, _customColors, 0, 16); return(true); } finally { Marshal.FreeCoTaskMem(custColorPtr); } }
/// <summary> /// Displays the "open file" dialog and returns the selected path name. /// </summary> /// <param name="title">Title of dialog.</param> /// <param name="directory">Default directory of dialog.</param> /// <param name="filter">Filter of dialog; example "Image(*.jpg;*.png)\0*.jpg;*.png".</param> /// <returns>The path of selected file.</returns> public static string OpenFileDialog(string title, string directory, string filter) { var ofn = new OpenFileName { lpstrTitle = title, lpstrInitialDir = directory, lpstrFilter = filter, nMaxFile = 256, flags = OFNFlags.OFN_EXPLORER | OFNFlags.OFN_FILEMUSTEXIST | OFNFlags.OFN_PATHMUSTEXIST | OFNFlags.OFN_NOCHANGEDIR }; ofn.lStructSize = Marshal.SizeOf(ofn); if (Comdlg32.GetOpenFileName(ofn)) { return(ofn.lpstrFile); } return(string.Empty); }
/// <summary> /// Displays a file open dialog. /// </summary> private protected override bool RunFileDialog(NativeMethods.OPENFILENAME_I ofn) { bool result = UnsafeNativeMethods.GetOpenFileName(ofn); if (!result) { // Something may have gone wrong - check for error condition switch (Comdlg32.CommDlgExtendedError()) { case Comdlg32.FNERR.INVALIDFILENAME: throw new InvalidOperationException(string.Format(SR.FileDialogInvalidFileName, FileName)); case Comdlg32.FNERR.SUBCLASSFAILURE: throw new InvalidOperationException(SR.FileDialogSubLassFailure); case Comdlg32.FNERR.BUFFERTOOSMALL: throw new InvalidOperationException(SR.FileDialogBufferTooSmall); } } return(result); }
private void AttachImage() { if (this.CurrentInputDescription.AttachedImage != null) { this.CurrentInputDescription.AttachedImage = null; } else { var ofn = new Comdlg32.OPENFILENAME(); var hwndsrc = (HwndSource)HwndSource.FromVisual(Application.Current.MainWindow); ofn.lStructSize = (UInt32)Marshal.SizeOf(ofn); ofn.hwndOwner = hwndsrc.Handle; ofn.lpstrFilter = "画像ファイル(*.jpg;*.png;*.gif;*.bmp)\0*.jpg;*.png;*.gif;*.bmp\0すべてのファイル(*.*)\0*.*\0"; ofn.lpstrFile = new string('\0', 1024); ofn.nMaxFile = 1024; ofn.lpstrTitle = "添付する画像を選択"; ofn.Flags = Comdlg32.OFN_HIDEREADONLY | Comdlg32.OFN_DONTADDTORECENT | Comdlg32.OFN_FILEMUSTEXIST | Comdlg32.OFN_NODEREFERENCELINKS | Comdlg32.OFN_NOTESTFILECREATE | Comdlg32.OFN_PATHMUSTEXIST; var ret = Comdlg32.GetOpenFileName(ofn); if (ret) { AttachImage(ofn.lpstrFile); } } }
public static bool OpenSaveFileDialog(SaveFileDialogOptions options, out string fileName) { if (options == null) { throw new ArgumentNullException(nameof(options)); } string lpstrFilter; if (options.Filters.Count > 0) { var builder = new StringBuilder(); foreach ((string key, string value) in options.Filters) { builder.Append(key); builder.Append('\0'); builder.Append(value); builder.Append('\0'); } lpstrFilter = builder.ToString(); } else { lpstrFilter = null; } var buffer = new char[1024]; options.FileName?.CopyTo(0, buffer, 0, Math.Min(options.FileName.Length, 1024)); GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { var arg = new Comdlg32.LPOPENFILENAMEW { lStructSize = (uint)Marshal.SizeOf <Comdlg32.LPOPENFILENAMEW>(), hwndOwner = IntPtr.Zero, hInstance = IntPtr.Zero, lpstrFilter = lpstrFilter, lpstrCustomFilter = null, nMaxCustFilter = 0, nFilterIndex = 0, lpstrFile = handle.AddrOfPinnedObject(), nMaxFile = 1024, lpstrFileTitle = null, nMaxFileTitle = 0, lpstrInitialDir = null, lpstrTitle = null, Flags = Comdlg32.OFN_PATHMUSTEXIST | Comdlg32.OFN_OVERWRITEPROMPT, nFileOffset = 0, nFileExtension = 0, lpstrDefExt = options.DefaultExtension, lCustData = IntPtr.Zero, lpfnHook = IntPtr.Zero, lpTemplateName = null, //lpEditInfo = IntPtr.Zero, //lpstrPrompt = null, pvReserved = IntPtr.Zero, dwReserved = 0, FlagsEx = 0 }; if (Comdlg32.GetSaveFileNameW(ref arg)) { fileName = new string(buffer, 0, Array.IndexOf(buffer, '\0')); return(true); } } finally { if (handle.IsAllocated) { handle.Free(); } } //uint error = Comdlg32.CommDlgExtendedError(); fileName = null; return(false); }