Files
ShrlAlgoToolkit/ShrlAlgo.Toolkit.Core/Heplers/WinDialogHelper.cs

230 lines
8.0 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
2024-09-22 11:05:41 +08:00
using System.Windows;
using System.Windows.Interop;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Win32;
namespace ShrlAlgo.Toolkit.Core.Heplers;
2024-09-22 11:05:41 +08:00
public static class WinDialogHelper
{
/// <summary>
/// 对话框过滤器
/// </summary>
/// <param name="filterName">过滤器的名称</param>
/// <param name="extensions">仅扩展名,*代表所有文件</param>
/// <returns></returns>
2024-12-22 10:26:12 +08:00
public static FileDialog SetFilter(this FileDialog dialog, string filterName, params string[] extensions)
{
if (extensions[0] == "*")
{
2024-12-22 10:26:12 +08:00
dialog.Filter = "所有文件(*)|*";
//return "所有文件(*)|*";
}
2024-09-22 11:05:41 +08:00
var str = string.Empty;
for (var i = 0; i < extensions.Length; i++)
{
var extension = extensions[i];
str += $"*.{extension}";
if (i < extensions.Length - 1)
{
str += ";";
}
}
2024-12-22 10:26:12 +08:00
dialog.Filter = $"{filterName}({str})|{str}";
return dialog;
//return $"{filterName}({str})|{str}";
}
/// <summary>
/// 释放命令行管理程序分配的ITEMIDLIST结构
/// Frees an ITEMIDLIST structure allocated by the Shell.
/// </summary>
/// <param name="pidlList"></param>
[DllImport("shell32.dll", ExactSpelling = true)]
private static extern void ILFree(IntPtr pidlList);
/// <summary>
/// 返回与指定文件路径关联的ITEMIDLIST结构。
/// Returns the ITEMIDLIST structure associated with a specified file path.
/// </summary>
/// <param name="pszPath"></param>
/// <returns></returns>
[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern IntPtr ILCreateFromPathW(string pszPath);
/// <summary>
/// 打开一个Windows资源管理器窗口其中选择了特定文件夹中的指定项目。
/// Opens a Windows Explorer window with specified items in a particular folder selected.
/// </summary>
/// <param name="pidlList"></param>
/// <param name="cild"></param>
/// <param name="children"></param>
/// <param name="dwFlags"></param>
/// <returns></returns>
[DllImport("shell32.dll", ExactSpelling = true)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);
/// <summary>
/// 打开或在现有目录并选中相应文件
/// </summary>
/// <param name="fileFullName"></param>
static public void OpenFolderAndSelectFile(string fileFullName)
{
if (string.IsNullOrEmpty(fileFullName))
throw new ArgumentNullException(nameof(fileFullName));
fileFullName = Path.GetFullPath(fileFullName);
var pidlList = ILCreateFromPathW(fileFullName);
if (pidlList == IntPtr.Zero) return;
try
{
Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
}
finally
{
ILFree(pidlList);
}
}
/// <summary>
/// 新建一个资源管理器窗口并选中文件或文件夹
/// </summary>
/// <param name="fileOrFolderPath"></param>
public static void ExploreFile(string fileOrFolderPath)
{
//Process proc = new();
//proc.StartInfo.FileName = "explorer";
////打开资源管理器
//proc.StartInfo.Arguments = @"/select," + fileOrFolderPath;
////选中"notepad.exe"这个程序,即记事本
//proc.Start();
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{fileOrFolderPath}\"");
}
2024-09-22 11:05:41 +08:00
/// <summary>
/// 选择文件
/// </summary>
/// <param name="title"></param>
/// <param name="extensions"></param>
/// <returns></returns>
public static string GetSelectedFilePath(string title, params string[] extensions)
{
OpenFileDialog dialog =
new()
{
CheckFileExists = true,
2024-12-22 10:26:12 +08:00
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
};
2024-12-22 10:26:12 +08:00
dialog.SetFilter(title, extensions);
return dialog.ShowDialog() == true ? dialog.FileName : null;
}
2024-09-22 11:05:41 +08:00
///// <summary>
///// 单选文件夹路径
///// </summary>
///// <returns></returns>
//public static string GetSelectedFolderPath()
//{
// var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog { Multiselect = false };
2024-09-22 11:05:41 +08:00
// var folderPath = dialog.ShowDialog() == true ? dialog.SelectedPath : null;
2024-09-22 11:05:41 +08:00
// return folderPath;
//}
2024-09-22 11:05:41 +08:00
///// <summary>
///// 多选文件夹路径
///// </summary>
///// <returns></returns>
//public static string[] GetSelectedFolderPaths()
//{
// var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog { Multiselect = true };
2024-09-22 11:05:41 +08:00
// return dialog.ShowDialog() == true ? dialog.SelectedPaths : null;
//}
2024-09-22 11:05:41 +08:00
/// <summary>
/// 打开窗口,非模态窗口置顶显示,默认非模态
/// </summary>
/// <returns></returns>
private static void ShowAhead(this Window window)
{
_ = new WindowInteropHelper(window) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle };
window.Show();
}
2024-09-22 11:05:41 +08:00
public static void ShowModeless<T>(ObservableObject viewModel)
where T : Window, new()
{
2024-12-22 10:26:12 +08:00
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
var view = SingletonViewHelper<T>.GetInstance(out var isNewCreate);
if (isNewCreate)
{
view.DataContext = viewModel;
view.ShowAhead();
}
view.Activate();
2024-12-22 10:26:12 +08:00
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomainOnAssemblyResolve;
//AssemblyLoaderHelpers loaderUtil = new(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
//loaderUtil.HookAssemblyResolve();
//try
//{
// var view = SingletonViewHelper<Command>.GetInstance(out var isNewCreate);
// if (isNewCreate)
// {
// view.DataContext = viewModel;
// view.ShowAhead();
// }
// view.Activate();
//}
//catch (Exception ex)
//{
// LogHelper.ToLog($"{ex.Source}:{ex.StackTrace}");
//}
//finally
//{
// loaderUtil.UnhookAssemblyResolve();
//}
}
2024-12-22 10:26:12 +08:00
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
if (!args.Name.Contains("Xaml.Behaviors"))
{
return null;
}
var assemblyFile = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"Microsoft.Xaml.Behaviors.dll"
);
return File.Exists(assemblyFile) ? Assembly.LoadFrom(assemblyFile) : null;
}
private static Dictionary<Type, Window> _windows = [];
public static void ShowOrActivate<TWindow, TViewModel>(params object[] viewModelParams)
where TWindow : Window, new()
where TViewModel : class
{
var windowType = typeof(TWindow);
if (!_windows.ContainsKey(windowType) || _windows[windowType] == null)
{
//CloseAllWindowsExcept(windowType);
_windows[windowType] = new TWindow();
_windows[windowType].Closed += (sender, args) => _windows[windowType] = null;
if (_windows[windowType].DataContext == null || !(_windows[windowType].DataContext is TViewModel))
{
_windows[windowType].DataContext = viewModelParams.Length == 0
? Activator.CreateInstance(typeof(TViewModel))
: Activator.CreateInstance(typeof(TViewModel), viewModelParams);
}
_ = new WindowInteropHelper(_windows[windowType]) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle };
_windows[windowType].Show();
}
else
{
_windows[windowType].Activate();
}
}
2024-09-22 11:05:41 +08:00
}