示例#1
0
        public DialogResult ShowDialog(IWin32Window window)
        {
            NativeStructs.OPENFILENAME ofn = new NativeStructs.OPENFILENAME();

            try
            {
                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     = m_title;
                if (AddExtension)
                {
                    ofn.lpstrDefExt = m_defaultExt;
                }

                ofn.hwndOwner = window.Handle;

                m_activeScreen = Screen.FromHandle(window.Handle);

                int flags = (int)(NativeEnums.OpenFileNameFlags.OFN_EXPLORER | NativeEnums.OpenFileNameFlags.OFN_PATHMUSTEXIST | NativeEnums.OpenFileNameFlags.OFN_NOTESTFILECREATE | NativeEnums.OpenFileNameFlags.OFN_ENABLEHOOK | NativeEnums.OpenFileNameFlags.OFN_HIDEREADONLY);
                if (m_overwritePrompt)
                {
                    flags |= (int)NativeEnums.OpenFileNameFlags.OFN_OVERWRITEPROMPT;
                }

                ofn.Flags = flags;

                //set up hook
                ofn.lpfnHook = new NativeDelegates.OpenFileNameHookProc(HookProc);

                if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                {
                    ofn.lStructSize -= 12;
                }

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

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

                    return(DialogResult.Cancel);
                }

                m_fileName = ofn.lpstrFile;
            }
            finally
            {
                /* if (ofn.lpstrFile != IntPtr.Zero)
                 * Marshal.FreeCoTaskMem(ofn.lpstrFile);*/
            }

            return(DialogResult.OK);
        }
		public DialogResult ShowDialog(IWin32Window window)
		{
			NativeStructs.OPENFILENAME ofn = new NativeStructs.OPENFILENAME();

      try
      {
        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 = m_title;
        if (AddExtension)
          ofn.lpstrDefExt = m_defaultExt;

        ofn.hwndOwner = window.Handle;

        m_activeScreen = Screen.FromHandle(window.Handle);

        int flags = (int)(NativeEnums.OpenFileNameFlags.OFN_EXPLORER | NativeEnums.OpenFileNameFlags.OFN_PATHMUSTEXIST | NativeEnums.OpenFileNameFlags.OFN_NOTESTFILECREATE | NativeEnums.OpenFileNameFlags.OFN_ENABLEHOOK | NativeEnums.OpenFileNameFlags.OFN_HIDEREADONLY);
        if (m_overwritePrompt)
          flags |= (int)NativeEnums.OpenFileNameFlags.OFN_OVERWRITEPROMPT;

        ofn.Flags = flags;

        //set up hook
        ofn.lpfnHook = new NativeDelegates.OpenFileNameHookProc(HookProc);

        if (Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
          ofn.lStructSize -= 12;
        }

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

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

          return DialogResult.Cancel;
        }

        m_fileName = ofn.lpstrFile;
      }
      finally
      {
       /* if (ofn.lpstrFile != IntPtr.Zero)
          Marshal.FreeCoTaskMem(ofn.lpstrFile);*/
      }

			return DialogResult.OK;
		}