public bool RunDialog(IntPtr hWndOwner)
        {
            OPENFILENAME_I ofn = OPENFILENAME_I.Create();

            try
            {
                charBuffer = CharBuffer.CreateBuffer(FILEBUFSIZE);
                if (fileNames != null)
                {
                    charBuffer.PutString(fileNames[0]);
                }
                ofn.lStructSize = Marshal.SizeOf(typeof(OPENFILENAME_I));
                if (Environment.OSVersion.Platform != System.PlatformID.Win32NT ||
                    Environment.OSVersion.Version.Major < 5)
                {
                    ofn.lStructSize = 0x4C;
                }
                ofn.hwndOwner    = hWndOwner;
                ofn.hInstance    = Instance;
                ofn.nFilterIndex = filterIndex;
                ofn.lpstrFile    = charBuffer.AllocCoTaskMem();
                ofn.nMaxFile     = FILEBUFSIZE;
                ofn.Flags        = (OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING);
                ofn.FlagsEx      = OFN_USESHELLITEM;
                return(RunFileDialog(ofn));
            }
            finally
            {
                charBuffer = null;
                if (ofn.lpstrFile != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ofn.lpstrFile);
                }
            }
        }
            public static OPENFILENAME_I Create()
            {
                OPENFILENAME_I item = new OPENFILENAME_I();

                item.lStructSize   = 152;
                item.nMaxFile      = MAX_PATH;
                item.nMaxFileTitle = MAX_PATH;
                return(item);
            }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool flag;

            OPENFILENAME_I.WndProc proc = new OPENFILENAME_I.WndProc(this.HookProc);
            OPENFILENAME_I         ofn  = new OPENFILENAME_I();

            try
            {
                this._charBuffer = CharBuffer.CreateBuffer(0x2000);
                if (this._fileNames != null)
                {
                    this._charBuffer.PutString(this._fileNames[0]);
                }
                ofn.lStructSize     = Marshal.SizeOf(typeof(OPENFILENAME_I));
                ofn.hwndOwner       = hwndOwner;
                ofn.hInstance       = IntPtr.Zero;
                ofn.lpstrFilter     = MakeFilterString(this._filter, this.DereferenceLinks);
                ofn.nFilterIndex    = this._filterIndex;
                ofn.lpstrFile       = this._charBuffer.AllocCoTaskMem();
                ofn.nMaxFile        = this._charBuffer.Length;
                ofn.lpstrInitialDir = this._initialDirectory;
                ofn.lpstrTitle      = this._title;
                ofn.Flags           = this.Options | 0x880020;
                ofn.lpfnHook        = proc;
                ofn.FlagsEx         = 0x1000000;
                if ((this._defaultExtension != null) && this.AddExtension)
                {
                    ofn.lpstrDefExt = this._defaultExtension;
                }
//                if (_fakeKey != null)
//                    ResetPlaces();
//                if(m_places != null)
//                    SetupFakeRegistryTree();
                flag = this.RunFileDialog(ofn);
            }
            finally
            {
                this._charBuffer = null;
                if (ofn.lpstrFile != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ofn.lpstrFile);
                }
                try
                {
//                    if (m_places != null)
//                        ResetPlaces();
                }
                catch
                {
                }
            }
            return(flag);
        }
Exemple #4
0
        internal override bool RunFileDialog(OPENFILENAME_I ofn)
        {
            bool saveFileName = false;

            saveFileName = NativeMethods.GetSaveFileName(ofn);
            if (!saveFileName)
            {
                switch (NativeMethods.CommDlgExtendedError())
                {
                case 0x3001:
                    throw new InvalidOperationException("FileDialogSubClassFailure");

                case 0x3002:
                    throw new InvalidOperationException("FileDialogInvalidFileName" + base.SafeFileName);

                case 0x3003:
                    throw new InvalidOperationException("FileDialogBufferTooSmall");
                }
            }
            return(saveFileName);
        }
        private bool DoFileOk(IntPtr lpOFN)
        {
            OPENFILENAME_I openfilename_i = (OPENFILENAME_I)Marshal.PtrToStructure(lpOFN, typeof(OPENFILENAME_I));
            int            num            = this._dialogOptions;
            int            num2           = this._filterIndex;

            string[] strArray = this._fileNames;
            bool     flag     = false;

            try
            {
                this._dialogOptions = (this._dialogOptions & -2) | (openfilename_i.Flags & 1);
                this._filterIndex   = openfilename_i.nFilterIndex;
                this._charBuffer.PutCoTaskMem(openfilename_i.lpstrFile);
                if (!this.GetOption(0x200))
                {
                    this._fileNames = new string[] { this._charBuffer.GetString() };
                }
                else
                {
                    this._fileNames = GetMultiselectFiles(this._charBuffer);
                }
                if (this.ProcessFileNames())
                {
                    CancelEventArgs e = new CancelEventArgs();
                    this.OnFileOk(e);
                    flag = !e.Cancel;
                }
            }
            finally
            {
                if (!flag)
                {
                    this._dialogOptions = num;
                    this._filterIndex   = num2;
                    this._fileNames     = strArray;
                }
            }
            return(flag);
        }
 internal abstract bool RunFileDialog(OPENFILENAME_I ofn);
        private IntPtr ProcOnNotify(IntPtr hwnd, IntPtr lParam)
        {
            IntPtr   hres      = IntPtr.Zero;
            OFNOTIFY structure = (OFNOTIFY)Marshal.PtrToStructure(lParam, typeof(OFNOTIFY));

            switch ((NativeMethods.DialogChangeStatus)structure.hdr_code)
            {
            case NativeMethods.DialogChangeStatus.CDN_FILEOK:    //- 606:
                if (this._ignoreSecondFileOkNotification)
                {
                    if (this._fileOkNotificationCount != 0)
                    {
                        this._ignoreSecondFileOkNotification = false;
                        NativeMethods.CriticalSetWindowLong(new HandleRef(this, hwnd), 0, InvalidIntPtr);
                        hres = InvalidIntPtr;
                        break;
                    }
                    this._fileOkNotificationCount = 1;
                }

                if (!this.DoFileOk(structure.lpOFN))
                {
                    NativeMethods.CriticalSetWindowLong(new HandleRef(this, hwnd), 0, InvalidIntPtr);
                    hres = InvalidIntPtr;
                }
                break;

            case NativeMethods.DialogChangeStatus.CDN_TYPECHANGE:
            {
                OPENFILENAME_I ofn = (OPENFILENAME_I)Marshal.PtrToStructure(structure.lpOFN, typeof(OPENFILENAME_I));
                int            i   = ofn.nFilterIndex;

                OnFilterChanged(this, i);
            }
            break;

            case NativeMethods.DialogChangeStatus.CDN_HELP:    // - 605:
                break;

            case NativeMethods.DialogChangeStatus.CDN_FOLDERCHANGE:    //- 603:
            {
                StringBuilder folderPath = new StringBuilder(256);
                NativeMethods.SendMessage(new HandleRef(this, structure.hdr_hwndFrom), (int)DialogChangeProperties.CDM_GETFOLDERPATH, (IntPtr)256, folderPath);
                OnPathChanged(this, folderPath.ToString());
                folderPath.Length = 0;
            }
            break;

            case NativeMethods.DialogChangeStatus.CDN_SHAREVIOLATION:    //- 604:
                this._ignoreSecondFileOkNotification = true;
                this._fileOkNotificationCount        = 0;
                break;

            case NativeMethods.DialogChangeStatus.CDN_SELCHANGE:    //- 602:
            {
                OPENFILENAME_I openfilename_i = (OPENFILENAME_I)Marshal.PtrToStructure(structure.lpOFN, typeof(OPENFILENAME_I));
                int            num            = (int)NativeMethods.UnsafeSendMessage(this._hwndFileDialog, 0x464, IntPtr.Zero, IntPtr.Zero);
                if (num > openfilename_i.nMaxFile)
                {
                    int        size   = num + 0x800;
                    CharBuffer buffer = CharBuffer.CreateBuffer(size);
                    IntPtr     ptr2   = buffer.AllocCoTaskMem();
                    Marshal.FreeCoTaskMem(openfilename_i.lpstrFile);
                    openfilename_i.lpstrFile = ptr2;
                    openfilename_i.nMaxFile  = size;
                    this._charBuffer         = buffer;
                    Marshal.StructureToPtr(openfilename_i, structure.lpOFN, true);
                    Marshal.StructureToPtr(structure, lParam, true);
                }
                StringBuilder filePath = new StringBuilder(256);
                NativeMethods.SendMessage(new HandleRef(this, structure.hdr_hwndFrom), (uint)DialogChangeProperties.CDM_GETFILEPATH, (IntPtr)256, filePath);

                //艾宏增加 多选文件时,获取第一个文件名
                string temppath = filePath.ToString();
                int    index    = temppath.IndexOf(" \"");      //多个文件名中间由 ' "'分隔
                if (index > 0)
                {
                    temppath = temppath.Substring(0, index);
                }

                temppath = temppath.Replace("\"", "");          //多选文件名时,需要去掉中间的'"'字符

                OnPathChanged(this, temppath);
                filePath.Length = 0;
                break;
            }

            case NativeMethods.DialogChangeStatus.CDN_INITDONE:    //- 601:
            {
                NativeMethods.PostMessage(new HandleRef(this, this._hwndFileDialogEmbedded), MSG_POST_CREATION, IntPtr.Zero, IntPtr.Zero);
            }
            break;
            }
            return(hres);
        }
 internal bool RunFileDialog(OPENFILENAME_I ofn)
 {
     return(GetOpenFileName(ofn));
 }
 public static extern bool GetOpenFileName([In, Out] OPENFILENAME_I ofn);