Files
ShrlAlgoToolkit/ShrlAlgoToolkit.RevitAddins/Common/Assists/WinDialogAssist.cs

232 lines
7.8 KiB
C#
Raw Normal View History

2025-07-11 09:20:23 +08:00
using System.Diagnostics;
using System.IO;
2024-09-22 11:05:41 +08:00
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
2025-07-11 09:20:23 +08:00
2025-12-23 21:35:54 +08:00
using Autodesk.Revit.DB;
2024-09-22 11:05:41 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
2025-07-11 09:20:23 +08:00
2024-09-22 11:05:41 +08:00
using Microsoft.Win32;
2025-07-11 09:20:23 +08:00
2026-02-21 16:31:24 +08:00
namespace ShrlAlgoToolkit.RevitAddins.Common.Assists;
2024-09-22 11:05:41 +08:00
2026-01-01 10:02:59 +08:00
public static class WinDialogAssist
2024-09-22 11:05:41 +08:00
{
/// <summary>
/// 对话框过滤器
/// </summary>
/// <param name="filterName">过滤器的名称</param>
/// <param name="extensions">仅扩展名,*代表所有文件</param>
/// <returns></returns>
2026-02-12 21:29:00 +08:00
public static Microsoft.Win32.FileDialog SetFilter(this Microsoft.Win32.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>
/// 打开或在现有目录并选中相应文件
/// </summary>
/// <param name="fileFullName"></param>
2026-02-22 20:03:42 +08:00
public static void OpenFolderAndSelectFile(string fileFullName)
{
if (string.IsNullOrEmpty(fileFullName))
throw new ArgumentNullException(nameof(fileFullName));
2026-02-21 16:31:24 +08:00
if (!File.Exists(fileFullName))
{
2026-02-21 16:31:24 +08:00
throw new FileNotFoundException("指定的文件不存在", fileFullName);
}
2026-02-24 11:34:18 +08:00
var proc = new Process();
2026-02-21 16:31:24 +08:00
proc.StartInfo.FileName = "explorer";
//打开资源管理器
proc.StartInfo.Arguments = "/select," + fileFullName;
proc.Start();
}
2026-02-21 16:31:24 +08:00
/// <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();
2026-02-24 11:34:18 +08:00
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;
}
2026-02-24 11:34:18 +08:00
public static void UpdateViewDisplay(PreviewControl currentPreviewControl, System.Windows.Controls.Grid previewContainer, Document backgroundDoc, View view, ViewDetailLevel detailLevel, DisplayStyle displayStyle)
2025-12-23 21:35:54 +08:00
{
2026-02-24 11:34:18 +08:00
if (backgroundDoc == null || view == null) return;
2025-12-23 21:35:54 +08:00
2026-02-24 11:34:18 +08:00
backgroundDoc.Invoke(
_ =>
2025-12-23 21:35:54 +08:00
{
try
{
view.DetailLevel = detailLevel;
view.DisplayStyle = displayStyle;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
2024-09-22 11:05:41 +08:00
2025-12-23 21:35:54 +08:00
// 2. 销毁旧控件
2026-02-24 11:34:18 +08:00
if (currentPreviewControl != null)
2025-12-23 21:35:54 +08:00
{
2026-02-24 11:34:18 +08:00
previewContainer.Children.Clear();
currentPreviewControl.Dispose();
currentPreviewControl = null;
2025-12-23 21:35:54 +08:00
}
// 3. 创建新控件并加入 UI
2026-02-24 11:34:18 +08:00
currentPreviewControl = new PreviewControl(backgroundDoc, view.Id);
previewContainer.Children.Add(currentPreviewControl);
2025-12-23 21:35:54 +08:00
}
2026-02-24 11:34:18 +08:00
/// <summary>
/// 打开窗口,非模态窗口置顶显示,默认非模态
/// </summary>
/// <returns></returns>
private static void ShowAhead(this Window window)
{
2026-02-24 11:34:18 +08:00
_ = new WindowInteropHelper(window) { Owner = Process.GetCurrentProcess().MainWindowHandle };
window.Show();
}
2024-09-22 11:05:41 +08:00
public static void ShowModeless<T>(ObservableObject viewModel)
where T : Window, new()
{
2025-07-11 09:20:23 +08:00
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
2026-02-24 11:34:18 +08:00
var view = SingletonViewAssist<T>.GetInstance(out var isNewCreate);
if (isNewCreate)
{
view.DataContext = viewModel;
view.ShowAhead();
}
view.Activate();
2025-07-11 09:20:23 +08:00
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
2026-02-21 16:31:24 +08:00
2026-02-24 11:34:18 +08:00
private static readonly Dictionary<Type, Window> Windows = [];
2026-02-22 20:03:42 +08:00
public static void ShowOrActivate<TView, TViewModel>(params object[] viewModelParams)
where TView : Window,new()
2025-07-11 09:20:23 +08:00
where TViewModel : class
{
2026-02-22 20:03:42 +08:00
var windowType = typeof(TView);
2026-02-24 11:34:18 +08:00
if (!Windows.ContainsKey(windowType) || Windows[windowType] == null)
{
//CloseAllWindowsExcept(windowType);
2025-07-11 09:20:23 +08:00
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
2026-02-24 11:34:18 +08:00
Windows[windowType] = new TView();
Windows[windowType].Closed += WindowClosed;
Windows[windowType].Closed += (_, _) => Windows[windowType] = null;
if (Windows[windowType].DataContext == null || !(Windows[windowType].DataContext is TViewModel))
{
2026-02-24 11:34:18 +08:00
Windows[windowType].DataContext = viewModelParams.Length == 0 ? Activator.CreateInstance(typeof(TViewModel)) : Activator.CreateInstance(typeof(TViewModel), viewModelParams);
}
2026-02-24 11:34:18 +08:00
_ = new WindowInteropHelper(Windows[windowType]) { Owner = Process.GetCurrentProcess().MainWindowHandle };
Windows[windowType].Show();
}
else
{
2026-02-24 11:34:18 +08:00
Windows[windowType].Activate();
}
}
2025-07-11 09:20:23 +08:00
private static void WindowClosed(object sender, EventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
2026-01-01 10:02:59 +08:00
public static void ShowDialog<TWindow, TViewModel>(params object[] viewModelParams)
where TWindow : Window, new()
where TViewModel : class
{
2026-02-24 11:34:18 +08:00
var window = new TWindow
2026-01-01 10:02:59 +08:00
{
2026-02-24 11:34:18 +08:00
DataContext = viewModelParams.Length == 0 ? Activator.CreateInstance(typeof(TViewModel)) : Activator.CreateInstance(typeof(TViewModel), viewModelParams)
};
_ = new WindowInteropHelper(window) { Owner = Process.GetCurrentProcess().MainWindowHandle };
2026-01-01 10:02:59 +08:00
window.ShowDialog();
}
2025-07-11 09:20:23 +08:00
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
StackFrame[] frames = new StackTrace().GetFrames();
if (frames == null)
{
return null;
}
string name = new AssemblyName(args.Name).Name;
List<string> list = new List<string>();
2026-02-24 11:34:18 +08:00
foreach (var t in frames)
2025-07-11 09:20:23 +08:00
{
2026-02-24 11:34:18 +08:00
MethodBase method = t.GetMethod();
2025-07-11 09:20:23 +08:00
if ((object)method.DeclaringType == null)
{
continue;
}
string directoryName = Path.GetDirectoryName(method.DeclaringType.Assembly.Location);
if (list.Contains(directoryName))
{
continue;
}
list.Add(directoryName);
foreach (string item in Directory.EnumerateFiles(directoryName, "*.dll"))
{
if (name == Path.GetFileNameWithoutExtension(item))
{
return Assembly.LoadFile(item);
}
}
}
return null;
}
2024-09-22 11:05:41 +08:00
}