如何在wpf中自定义文件对话框

时间:2012-11-13 04:36:19

标签: c# .net wpf winforms wpf-controls

在Windows7中,我使用的是自定义的“打开文件”对话框(WPF应用程序)。 “我的打开文件”对话框派生自Microsoft.Win32.CommonDialog。 对话框有旧貌,如何将其更改为新外观(windows7文件对话框外观(资源管理器样式))。

代码部分:

private const int OFN_ENABLESIZING = 0x00800000;
private const int OFN_EXPLORER = 0x00080000;
private const int OFN_ENABLEHOOK = 0x00000020;       
protected override bool RunDialog(IntPtr hwndOwner)
{
    OPENFILENAME_I.WndProc proc = new OPENFILENAME_I.WndProc(this.HookProc);
                    OPENFILENAME_I ofn = new OPENFILENAME_I();
    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 =  OFN_EXPLORER | OFN_ENABLESIZING | OFN_ENABLEHOOK;
    ofn.lpfnHook = proc;
    ofn.FlagsEx = 0x1000000 ;    
    NativeMethods.GetOpenFileName(ofn); // 
 }

[SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("comdlg32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool GetOpenFileName([In, Out] OPENFILENAME_I ofn);    

1 个答案:

答案 0 :(得分:2)

看一下这个链接。它有自定义文件对话框。 http://www.ookii.org/software/dialogs/ 可能您可能需要根据您的需要进行调整。

此致

VIMAL