码迷,mamicode.com
首页 > Windows程序 > 详细

WPFの操作文件浏览框几种方式

时间:2018-11-11 13:52:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:不能   unmap   ring   ntp   microsoft   引用   调用   .sh   rsh   

原文:WPFの操作文件浏览框几种方式

方式1: 使用win32控件OpenFileDialog

 

Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); 

ofd.DefaultExt = ".xml"; 

ofd.Filter = "xml file|*.xml"; 

if (ofd.ShowDialog() == true) 

{ 

     //此处做你想做的事 ...=ofd.FileName; 

} 

方式2: 使用Forms中的OpenFileDialog控件

WPF中是不能直接使用Forms中的控件的,否则会提示找不到Forms名字控件. 如果你仍然要用, 答案便是添加.net 引用reference

System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 

openFileDialog1.InitialDirectory = "c:\\"; 

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 

openFileDialog1.FilterIndex = 2; 

openFileDialog1.RestoreDirectory = true; 

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 

{ 

    //此处做你想做的事 ...=openFileDialog1.FileName; 

}
 

 

类似的有文件夹浏览对话框:



System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); 

System.Windows.Forms.DialogResult result = folderBrowserDialog.ShowDialog(); 

if (result == System.Windows.Forms.DialogResult.OK) 

{ 

    tb_FolderPath.Text = folderBrowserDialog.SelectedPath; 

} 

方式三: 使用win32 api

BOOL WINAPI GetOpenFileName(  __inout  LPOPENFILENAME lpofn)

 

使用这种方式, 你需要自己实现LPOPENFILENAME结构和对GetOpenFileName方法就行封装:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 

public class OpenFileName 

{ 

    public int structSize = 0; 

    public IntPtr hwnd = IntPtr.Zero; 

    public IntPtr hinst = IntPtr.Zero; 

    public string filter = null; 

    public string custFilter = null; 

    public int custFilterMax = 0; 

    public int filterIndex = 0; 

    public string file = null; 

    public int maxFile = 0; 

    public string fileTitle = null; 

    public int maxFileTitle = 0; 

    public string initialDir = null; 

    public string title = null; 

    public int flags = 0; 

    public short fileOffset = 0; 

    public short fileExtMax = 0; 

    public string defExt = null; 

    public int custData = 0; 

    public IntPtr pHook = IntPtr.Zero; 

    public string template = null; 

} 

 

public class LibWrap 

{ 

    // Declare a managed prototype for the unmanaged function. 

    [DllImport("Comdlg32.dll",SetLastError=true,ThrowOnUnmappableChar=true, CharSet = CharSet.Auto)] 

    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn); 

}
 

之后的工作就是实例化、初始化和方法调用了:







1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
 

OpenFileName ofn = new OpenFileName(); 

ofn.structSize = Marshal.SizeOf(ofn); 

ofn.filter = "Project files\0*.xml"; 

ofn.file = new string(new char[256]); 

ofn.maxFile = ofn.file.Length; 

ofn.fileTitle = new string(new char[64]); 

ofn.maxFileTitle = ofn.fileTitle.Length; 

ofn.initialDir = "C:\\"; 

ofn.title = "Open Project"; 

ofn.defExt = "xml"; 

ofn.structSize = Marshal.SizeOf(ofn); 

if (LibWrap.GetOpenFileName(ofn)) 

{ 

    //此处做你想做的事 ...=ofn.file; 

} 

 

WPFの操作文件浏览框几种方式

标签:不能   unmap   ring   ntp   microsoft   引用   调用   .sh   rsh   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9941817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!