[C#]Unity调用window窗口

15 篇文章 0 订阅
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
namespace XRMaker.Editor
{
    /// <summary>
    /// 参考
    /// Url:http://www.cnblogs.com/U-tansuo/archive/2012/07/10/GetOpenFileName.html
    /// Url:https://msdn.microsoft.com/en-us/library/windows/desktop/ff468808(v=vs.85).aspx
    /// Url:https://msdn.microsoft.com/en-us/library/windows/desktop/bb776426(v=vs.85).aspx
    /// Url:http://blog.csdn.net/cwj649956781/article/details/76218218
    /// </summary>
    public class WindowsForms
    {
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern bool GetOpenFileName([In, Out] FileName ofn);
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern bool GetSaveFileName([In, Out] FileName ofd);
        [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern IntPtr SHBrowseForFolder([In, Out] DirName ofn);

        [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
        public static string Load(params string[] ext)
        {
            FileName i = new FileName(ext);
            i.title = "打开";
            GetOpenFileName(i);
            return i.file;
        }
        public static string Save(string ext)
        {
            FileName i = new FileName(ext);
            i.title = "保存";
            GetSaveFileName(i);
            return i.file;
        }
        public static string GetDir()
        {
            DirName d = new DirName();
            IntPtr i = SHBrowseForFolder(d);
            char[] c = new char[256];
            SHGetPathFromIDList(i, c);
            return new string(c);
        }
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private class DirName
        {
            public IntPtr hwndOwner = IntPtr.Zero;
            public IntPtr pidlRoot = IntPtr.Zero;
            public String pszDisplayName = null;
            public String lpszTitle = null;
            public UInt32 ulFlags = 0;
            public IntPtr lpfn = IntPtr.Zero;
            public IntPtr lParam = IntPtr.Zero;
            public int iImage = 0;
            public DirName()
            {
                pszDisplayName = new string(new char[256]);
                ulFlags = 0x00000040 | 0x00000010; //BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
                lpszTitle = "打开目录";
            }
        }
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private class FileName
        {
            public int structSize = 0;
            private IntPtr dlgOwner = IntPtr.Zero;
            private IntPtr instance = IntPtr.Zero;
            private string filter = null;
            private string customFilter = null;
            private int maxCustFilter = 0;
            private int filterIndex = 0;
            public string file { get; set; }
            private int maxFile = 0;
            public string fileTitle { get; set; }
            private int maxFileTitle = 0;
            public string initialDir { get; set; }
            public string title { get; set; }
            private int flags = 0;
            private short fileOffset = 0;
            private short fileExtension = 0;
            private string defExt = null;
            private IntPtr custData = IntPtr.Zero;
            private IntPtr hook = IntPtr.Zero;
            private string templateName = null;
            private IntPtr reservedPtr = IntPtr.Zero;
            private int reservedInt = 0;
            private int flagsEx = 0;
            public FileName(params string[] ext)
            {
                structSize = Marshal.SizeOf(this);
                defExt = ext[0];
                string n = null;
                string e = null;
                foreach (string _e in ext)
                {
                    if (_e == "*")
                    {
                        n += "All Files";
                        e += "*.*;";
                    }
                    else
                    {
                        string _n = "." + _e + ";";
                        n += _n;
                        e += "*" + _n;
                    }
                }
                n = n.Substring(0, n.Length - 1);
                filter = n + "\0" + e + "\0";
                file = new string(new char[256]);
                maxFile = file.Length;
                fileTitle = new string(new char[64]);
                maxFileTitle = fileTitle.Length;
                //flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
                flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
                initialDir = Application.dataPath;
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值