簡體   English   中英

不使用 Microsoft.Win32 的 Openfiledailog 框

[英]Openfiledailog box without using Microsoft.Win32

我正在使用 ike 這個使用 openfiledailog 框,但我想在不使用 Microsoft.Win32 引用的情況下執行相同的功能

  Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  dlg.DefaultExt = ".png";
  dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
  //Nullable<bool> result =
  dlg.ShowDialog();

選項1

您可以創建自己的對話框來顯示文件列表並讓用戶選擇文件。

選項 2

您可以改用GetOpenFileName

[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

這是pinvoke.net 頁面

這是您需要的工作示例:

[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    public int structSize = 0;
    public IntPtr dlgOwner = IntPtr.Zero;
    public IntPtr instance = IntPtr.Zero;
    public String filter = null;
    public String customFilter = null;
    public int maxCustFilter = 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 fileExtension = 0;
    public String defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public String templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}

private void OpenButton_Click(object sender, EventArgs e)
{
    OpenFileName openFileName = new OpenFileName();
    openFileName.structSize = Marshal.SizeOf(openFileName);
    openFileName.filter = "JPEG Files (*.jpeg)\0*.jpeg\0PNG Files (*.png)\0*.png\0JPG Files (*.jpg)\0*.jpg\0GIF Files (*.gif)\0*.gif\0";
    openFileName.file = new String(new char[256]);
    openFileName.maxFile = openFileName.file.Length;
    openFileName.fileTitle = new String(new char[64]);
    openFileName.maxFileTitle = openFileName.fileTitle.Length;
    openFileName.title = "Open";
    openFileName.defExt = "png";

    if (GetOpenFileName(openFileName))
    {
        MessageBox.Show(openFileName.file);
    }
}

基於MSDNPInvokeArie代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM