Unity C# GetSaveFileName()的執行個體詳解

來源:互聯網
上載者:User
唉喲,這次厲害咯,網上搜羅了好久,終於被我找到漢化的儲存對話方塊了,根據網上的一些前輩總結的內容,做了一些修改,

先放個:

首先需要定義一個OpenFileName的類:

[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;}

  當然,也不是都用到了,只用到了一小部分,有性趣的童鞋可以個人化一下儲存對話方塊,

然後是最重要的委託GetSaveName()方法:

public class DllUse{[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);}

  擋擋擋,最後調用一下即可:

public static void OpenDialog(Action<Stream> onSave)    {        OpenFileName ofn = new OpenFileName();        ofn.structSize = Marshal.SizeOf(ofn);            ofn.filter = "Excel (*.xls)\0*.xls\0\0";        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 = UnityEngine.Application.dataPath;//預設路徑ofn.title = "儲存檔案";        ofn.defExt = ".xls";//顯示檔案的類型ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (DllTest.GetSaveFileName(ofn))        {            dosomething();string Savepath = Path.GetDirectoryName (ofn.file);            Process.Start (Savepath);        }    }

終於搞定儲存對話方塊漢化了,5555,也是不容易啊!

那位小夥伴有更好的方法希望可以分享給我啊!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.