博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity调用Windows窗口句柄,选择文件和目录
阅读量:5266 次
发布时间:2019-06-14

本文共 2841 字,大约阅读时间需要 9 分钟。

 

T:2019-6-25 10:06:59

C:Scatt Kang

using System;

using System.Collections;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using UnityEngine;

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

public class OpenDialogFile

{
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;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenDialogDir
{
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 class WindowsExplorer

{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenDialogFile ofn);

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

public static extern bool GetSaveFileName([In, Out] OpenDialogFile ofn);

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

public static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);

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

public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);

/// <summary>

/// 调用WindowsExploer 并返回所选文件夹路径
/// </summary>
/// <param name="dialogtitle">打开对话框的标题</param>
/// <returns>所选文件夹路径</returns>
public static string GetPathFromWindowsExplorer(string dialogtitle = "请选择下载路径" )
{
string res;
OpenDialogDir ofn2 = new OpenDialogDir();
ofn2.pszDisplayName = new string(new char[2000]); ;     // 存放目录路径缓冲区  
        ofn2.lpszTitle = dialogtitle;// 标题  
        ofn2.ulFlags = 0x00000040; // 新的样式,带编辑框  
        IntPtr pidlPtr = WindowsExplorer.SHBrowseForFolder(ofn2);

char[] charArray = new char[2000];

for (int i = 0; i < 2000; i++)
charArray[i] = '\0';

WindowsExplorer.SHGetPathFromIDList(pidlPtr, charArray);

res = new String(charArray);
res = res.Substring(0, res.IndexOf('\0'));
return res;
}

}

转载于:https://www.cnblogs.com/51-Scott/p/11081137.html

你可能感兴趣的文章
IOC容器
查看>>
Windows 2003全面优化
查看>>
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
我的Hook学习笔记
查看>>
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>