Example #1
0
    public static string GetOpenFileName()
    {
        string result = null;
        IntPtr buf    = Marshal.AllocHGlobal(512);

        try {
            OPENFILENAME ofn = new OPENFILENAME();
            ofn.lStructSize = Marshal.SizeOf(typeof(OPENFILENAME));
            ofn.hInstance   = GetModuleHandleW(null);
            ofn.lpstrFile   = buf;
            ofn.nMaxFile    = 512;
            //
            IntPtr ofPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OPENFILENAME)));
            try {
                Marshal.StructureToPtr(ofn, ofPtr, false);
                if (0 != GetOpenFileNameW(ofPtr))
                {
                    result = Marshal.PtrToStringUni(buf);
                }
            } finally {
                Marshal.FreeHGlobal(ofPtr);
            }
        } finally {
            Marshal.FreeHGlobal(buf);
        }
        return(result);
    }
Example #2
0
        /// <summary>
        /// 保存文件的对话框
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="filter"></param>
        /// <param name="title"></param>
        /// <param name="defaultFilename"></param>
        /// <returns>保存的文件名,如果取消则为null</returns>
        public static string SaveFileDialog(string dirPath,
                                            string filter          = "All(*.*)|*.*",
                                            string title           = "SaveFileDialog",
                                            string defaultFilename = "")
        {
            OPENFILENAME ofn    = GetOFN(dirPath, filter, title, defaultFilename);
            bool         result = GetSaveFileName(ref ofn);

            return(result ? ofn.lpstrFile : null);
        }
Example #3
0
    public DialogResult ShowDialog()
    {
        //set up the struct and populate it

        OPENFILENAME ofn = new OPENFILENAME();

        ofn.lStructSize = Marshal.SizeOf(ofn);
        ofn.lpstrFilter = m_Filter.Replace('|', '\0') + '\0';

        ofn.lpstrFile      = m_FileName + new string(' ', 512);
        ofn.nMaxFile       = ofn.lpstrFile.Length;
        ofn.lpstrFileTitle = System.IO.Path.GetFileName(m_FileName) + new string(' ', 512);
        ofn.nMaxFileTitle  = ofn.lpstrFileTitle.Length;
        ofn.lpstrTitle     = "Save file as";
        ofn.lpstrDefExt    = m_DefaultExt;

        //position the dialog above the active window
        ofn.hwndOwner = Form.ActiveForm.Handle;

        //we need to find out the active screen so the dialog box is
        //centred on the correct display

        m_ActiveScreen = Screen.FromControl(Form.ActiveForm);

        //set up some sensible flags
        ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

        //this is where the hook is set. Note that we can use a C# delegate in place of a C function pointer
        ofn.lpfnHook = new OFNHookProcDelegate(HookProc);

        //if we're running on Windows 98/ME then the struct is smaller
        if (System.Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
            ofn.lStructSize -= 12;
        }

        //show the dialog

        if (!GetSaveFileName(ref ofn))
        {
            int ret = CommDlgExtendedError();

            if (ret != 0)
            {
                throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
            }

            return(DialogResult.Cancel);
        }

        m_FileName    = ofn.lpstrFile;
        m_FilterIndex = ofn.nFilterIndex;

        return(DialogResult.OK);
    }
Example #4
0
        /// <summary>
        /// 打开文件对话框
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="filter"></param>
        /// <param name="title"></param>
        /// <param name="defaultFilename"></param>
        /// <returns>打开的文件名,如果取消则为null</returns>
        public static string OpenFileDialog(
            string dirPath,
            string filter,
            string title           = "OpenFileDialog",
            string defaultFilename = "")
        {
            OPENFILENAME ofn    = GetOFN(dirPath, filter, title, defaultFilename);
            int          result = GetOpenFileName(ref ofn);

            return(result > 0 ? ofn.lpstrFile : null);
        }
Example #5
0
        public void GetOpenFileNameTest()
        {
            using var fn = new SafeCoTaskMemString(261);
            var ofn = new OPENFILENAME
            {
                lStructSize  = (uint)Marshal.SizeOf(typeof(OPENFILENAME)),
                lpstrFile    = (IntPtr)fn,
                nMaxFile     = (uint)fn.Capacity,
                lpstrFilter  = "All\0*.*\0Text\0*.txt\0",
                nFilterIndex = 1,
                Flags        = OFN.OFN_PATHMUSTEXIST | OFN.OFN_FILEMUSTEXIST
            };

            Assert.That(GetOpenFileName(ref ofn), Is.True);
            Assert.That(ofn.lpstrFilter.Length, Is.GreaterThan(0));
        }
Example #6
0
        protected DialogResult ShowDialog(IntPtr hwndOwner)
        {
            OPENFILENAME ofn = new OPENFILENAME();

            ofn.lStructSize    = Marshal.SizeOf(ofn);
            ofn.lpstrFilter    = _Filter.Replace('|', '\0') + '\0' + '\0';
            ofn.lpstrFile      = _FileName + new string(' ', 520);
            ofn.nMaxFile       = ofn.lpstrFile.Length;
            ofn.lpstrFileTitle = System.IO.Path.GetFileName(_FileName) + new string(' ', 512);
            ofn.nMaxFileTitle  = ofn.lpstrFileTitle.Length;
            ofn.lpstrTitle     = _Title;
            ofn.lpstrDefExt    = _DefaultExt;
            ofn.hwndOwner      = hwndOwner;
            ofn.Flags          =
                OpenFileNameFlags.OFN_ENABLEHOOK |
                OpenFileNameFlags.OFN_ENABLESIZING |
                OpenFileNameFlags.OFN_HIDEREADONLY |
                OpenFileNameFlags.OFN_EXPLORER;
            ofn.lpfnHook = new OfnHookProc(HookProc);

            //if we're running on Windows 98/ME then the struct is smaller
            if (System.Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                ofn.lStructSize -= 12;
            }

            //show the dialog

            if (!ComDlg32.GetOpenFileName(ref ofn))
            {
                int ret = ComDlg32.CommDlgExtendedError();

                if (ret != 0)
                {
                    throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
                }

                return(DialogResult.Cancel);
            }

            _FileName = ofn.lpstrFile;
            return(DialogResult.OK);
        }
Example #7
0
        private static OPENFILENAME GetOFN(string dirPath, string filter, string title, string defaultFilename)
        {
            OPENFILENAME ofn = new OPENFILENAME();

            ofn.lStructSize     = Marshal.SizeOf(ofn);
            ofn.lpstrFilter     = filter.Replace('|', '\0') + '\0';
            ofn.nFilterIndex    = 1;
            ofn.lpstrInitialDir = dirPath;

            char[] _filename        = new char[256];
            char[] _defaultFilename = defaultFilename.ToCharArray();
            Array.Copy(_defaultFilename, _filename, _defaultFilename.Length);
            string filename = new string(_filename);

            ofn.lpstrTitle = title;
            ofn.lpstrFile  = filename;
            ofn.nMaxFile   = ofn.lpstrFile.Length;

            ofn.Flags = OFNEnum.OFN_PATHMUSTEXIST | OFNEnum.OFN_FILEMUSTEXIST;

            return(ofn);
        }
 private static extern bool GetOpenFileName(ref OPENFILENAME lpofn);
    public DialogResult ShowDialog()
    {
        //set up the struct and populate it

        OPENFILENAME ofn = new OPENFILENAME();

        ofn.lStructSize = Marshal.SizeOf(ofn);
        ofn.lpstrFilter = m_Filter.Replace('|', '\0') + '\0';

        ofn.lpstrFile = m_FileName + new string(' ', 512);
        ofn.nMaxFile = ofn.lpstrFile.Length;
        ofn.lpstrFileTitle = System.IO.Path.GetFileName(m_FileName) + new string(' ', 512);
        ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
        ofn.lpstrTitle = "Open file";
        ofn.lpstrDefExt = m_DefaultExt;

        //position the dialog above the active window
        ofn.hwndOwner = Form.ActiveForm.Handle;

        //we need to find out the active screen so the dialog box is
        //centred on the correct display

        m_ActiveScreen = Screen.FromControl(Form.ActiveForm);

        //set up some sensible flags
        ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

        //this is where the hook is set. Note that we can use a C# delegate in place of a C function pointer
        ofn.lpfnHook = new OFNHookProcDelegate(HookProc);

        //if we're running on Windows 98/ME then the struct is smaller
        if (System.Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
            ofn.lStructSize -= 12;
        }

        //show the dialog

        if (!GetOpenFileName(ref ofn))
        {
            int ret = CommDlgExtendedError();

            if (ret != 0)
            {
                throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
            }

            return DialogResult.Cancel;
        }

        m_FileName = ofn.lpstrFile;

        return DialogResult.OK;
    }
Example #10
0
 private static extern int GetSaveFileName(ref OPENFILENAME f);
Example #11
0
            protected override void WndProc(ref Message m)
            {
                switch ((Msg)m.Msg)
                {
                case Msg.WM_NOTIFY:
                    OFNOTIFY ofNotify = (OFNOTIFY)Marshal.PtrToStructure(m.LParam, typeof(OFNOTIFY));
                    switch (ofNotify.hdr.code)
                    {
                    case (uint)DialogChangeStatus.CDN_SELCHANGE:
                    {
                        StringBuilder filePath = new StringBuilder(256);
                        NativeMethods.SendMessage(new HandleRef(this, NativeMethods.GetParent(Handle)), (uint)DialogChangeProperties.CDM_GETFILEPATH, (IntPtr)256, filePath);
                        if (_CustomCtrl != null)
                        {
                            _CustomCtrl.OnFileNameChanged(this, filePath.ToString());
                        }
                    }
                    break;

                    case (uint)DialogChangeStatus.CDN_FOLDERCHANGE:
                    {
                        StringBuilder folderPath = new StringBuilder(256);
                        NativeMethods.SendMessage(new HandleRef(this, NativeMethods.GetParent(Handle)), (int)DialogChangeProperties.CDM_GETFOLDERPATH, (IntPtr)256, folderPath);
                        if (_CustomCtrl != null)
                        {
                            _CustomCtrl.OnFolderNameChanged(this, folderPath.ToString());
                        }
                    }
                    break;

                    case (uint)DialogChangeStatus.CDN_TYPECHANGE:
                    {
                        OPENFILENAME ofn = (OPENFILENAME)Marshal.PtrToStructure(ofNotify.OpenFileName, typeof(OPENFILENAME));
                        int          i   = ofn.nFilterIndex;
                        if (_CustomCtrl != null && _filterIndex != i)
                        {
                            _filterIndex = i;
                            _CustomCtrl.OnFilterChanged(this as IWin32Window, i);
                        }
                    }
                    break;

                    case (uint)DialogChangeStatus.CDN_INITDONE:
                        break;

                    case (uint)DialogChangeStatus.CDN_SHAREVIOLATION:
                        break;

                    case (uint)DialogChangeStatus.CDN_HELP:
                        break;

                    case (uint)DialogChangeStatus.CDN_INCLUDEITEM:
                        break;

                    case (uint)DialogChangeStatus.CDN_FILEOK:        //0xfffffda2:
#pragma warning disable 1690, 0414
                        //NativeMethods.SetWindowPos(_CustomCtrl._hFileDialogHandle, IntPtr.Zero,
                        //(int)_CustomCtrl._OpenDialogWindowRect.left,
                        //(int)_CustomCtrl._OpenDialogWindowRect.top,
                        //(int)_CustomCtrl._OpenDialogWindowRect.Width,
                        //(int)_CustomCtrl._OpenDialogWindowRect.Height,
                        //FileDialogControlBase.MSFileDialogWrapper.UFLAGSSIZE);
                        break;

#pragma warning restore 1690, 0414
                    default:

                        break;
                    }
                    break;

                case Msg.WM_COMMAND:
                    switch (NativeMethods.GetDlgCtrlID(m.LParam)) //switch (m.WParam & 0x0000ffff)
                    {
                    case (int)ControlsId.ButtonOk:                //OK
                        break;

                    case (int)ControlsId.ButtonCancel:        //Cancel
                        break;

                    case (int)ControlsId.ButtonHelp:          //0x0000040e://help
                        break;
                    }
                    break;

                default:
                    break;
                }
                base.WndProc(ref m);
            }
Example #12
0
 public static extern bool GetSaveFileName(ref OPENFILENAME lpofn);
Example #13
0
 private extern static bool GetSaveFileName(ref OPENFILENAME pOpenfilename);
Example #14
0
        /// <summary>
        /// Runs a common dialog box with the specified owner.
        /// </summary>
        /// <param name="owner">Any object that implements <see cref="IWin32Window"/> that represents the top-level window that will own the modal dialog box.</param>
        /// <returns><see cref="DialogResult">DialogResult.OK</see> if the user clicks OK in the dialog box; otherwise, <see cref="DialogResult">DialogResult.Cancel</see>.</returns>
        public DialogResult ShowDialog(IWin32Window owner)
        {
            if (owner != null)
            {
                info.hwndOwner = owner.Handle;
            }

            if (InTheHand.WindowsCE.Forms.SystemSettingsInTheHand.Platform == Microsoft.WindowsCE.Forms.WinCEPlatform.WinCEGeneric)
            {
                IntPtr pitemidlist;

                try
                {
                    pitemidlist = SHBrowseForFolder(ref info);
                }
                catch (MissingMethodException mme)
                {
                    throw new PlatformNotSupportedException("Your platform doesn't support the SHBrowseForFolder API", mme);
                }

                if (pitemidlist == IntPtr.Zero)
                {
                    return(DialogResult.Cancel);
                }

                // maxpath unicode chars
                byte[] buffer  = new byte[520];
                bool   success = SHGetPathFromIDList(pitemidlist, buffer);

                // get string from buffer
                if (success)
                {
                    folder = System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);

                    int nullindex = folder.IndexOf('\0');
                    if (nullindex != -1)
                    {
                        folder = folder.Substring(0, nullindex);
                    }
                }

                Marshal.FreeHGlobal(pitemidlist);
            }
            else
            {
                OPENFILENAME ofn = new OPENFILENAME();
                ofn.lStructSize = Marshal.SizeOf(ofn);
                ofn.hwndOwner   = info.hwndOwner;
                ofn.lpstrTitle  = info.lpszTitle;
                ofn.nMaxFile    = (InTheHand.EnvironmentInTheHand.MaxPath + 1);
                ofn.lpstrFile   = new string('\0', ofn.nMaxFile);
                ofn.Flags       = OFN.PROJECT;
                bool success = GetOpenFileName(ref ofn);
                if (!success)
                {
                    return(DialogResult.Cancel);
                }

                folder = ofn.lpstrFile;
                int nullIndex = folder.IndexOf('\0');
                if (nullIndex > -1)
                {
                    folder = folder.Substring(0, nullIndex);
                }
            }

            return(DialogResult.OK);
        }
Example #15
0
 private extern static int GetOpenFileName(ref OPENFILENAME pOpenfilename);
 internal static extern bool GetSaveFileName(ref OPENFILENAME lpofn);
Example #17
0
 internal static extern bool GetSaveFileName([In, Out] OPENFILENAME ofn);
Example #18
0
        public DialogResult ShowDialog()
        {
            OPENFILENAME openfilename = new OPENFILENAME {
                lpstrFilter = this.m_Filter.Replace('|', '\0') + '\0',
                lpstrFile = this.m_FileName + new string(' ', 0x200),
                lpstrFileTitle = Path.GetFileName(this.m_FileName) + new string(' ', 0x200),
                lpstrTitle = this.title,
                lpstrDefExt = this.m_DefaultExt,
                hwndOwner = Form.ActiveForm.Handle
            };
            openfilename.lStructSize = Marshal.SizeOf(openfilename);
            openfilename.nMaxFileTitle = openfilename.lpstrFileTitle.Length;
            openfilename.nMaxFile = openfilename.lpstrFile.Length;

            this.m_ActiveScreen = Screen.FromControl(Form.ActiveForm);
            openfilename.Flags = 0x90826;
            openfilename.lpfnHook = new OFNHookProcDelegate(this.HookProc);
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                openfilename.lStructSize -= 12;
            }
            if (!GetOpenFileName(ref openfilename))
            {
                int num = CommDlgExtendedError();
                if (num != 0)
                {
                    throw new ApplicationException("Couldn't show file open dialog - " + num.ToString());
                }
                return DialogResult.Cancel;
            }
            this.m_FileName = openfilename.lpstrFile;
            return DialogResult.OK;
        }
Example #19
0
 internal static extern bool GetOpenFileName(ref OPENFILENAME lpofn);