NavisworksTransport/src/Utils/DialogHelper.cs

216 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using Autodesk.Navisworks.Api;
namespace NavisworksTransport.Utils
{
/// <summary>
/// 对话框辅助类 - 统一处理对话框 Owner 设置和置顶显示
/// </summary>
public static class DialogHelper
{
/// <summary>
/// 从 UserControl 获取父窗口并设置为对话框 Owner
/// </summary>
/// <param name="dialog">要显示的对话框</param>
/// <param name="userControl">当前 UserControl</param>
/// <returns>是否成功设置 Owner</returns>
public static bool SetOwnerFromUserControl(Window dialog, UserControl userControl)
{
if (dialog == null || userControl == null)
return false;
try
{
var parentWindow = Window.GetWindow(userControl);
if (parentWindow != null)
{
dialog.Owner = parentWindow;
return true;
}
}
catch (Exception ex)
{
LogManager.Warning($"[DialogHelper] 设置 Owner 失败: {ex.Message}");
}
return false;
}
/// <summary>
/// 在 ViewModel 中查找合适的 Owner 窗口
/// 策略MainWindow -> 活动窗口 -> null
/// </summary>
/// <returns>找到的窗口,或 null</returns>
public static Window FindOwnerWindow()
{
try
{
// 方案1尝试 MainWindow
var mainWindow = System.Windows.Application.Current?.MainWindow;
if (mainWindow != null && (mainWindow.IsVisible || mainWindow.IsLoaded))
{
return mainWindow;
}
}
catch (Exception ex)
{
LogManager.Debug($"[DialogHelper] 获取 MainWindow 失败: {ex.Message}");
}
// 方案2遍历查找活动窗口
try
{
if (System.Windows.Application.Current != null)
{
foreach (Window window in System.Windows.Application.Current.Windows)
{
if (window.IsActive && window.IsLoaded)
{
return window;
}
}
}
}
catch (Exception ex)
{
LogManager.Debug($"[DialogHelper] 遍历窗口失败: {ex.Message}");
}
return null;
}
/// <summary>
/// 安全地设置对话框 OwnerViewModel 场景)
/// </summary>
/// <param name="dialog">要设置的对话框</param>
/// <returns>是否成功设置</returns>
public static bool SetOwnerSafely(Window dialog)
{
if (dialog == null)
return false;
if (dialog.Owner != null)
return true; // 已设置
var owner = FindOwnerWindow();
if (owner != null)
{
try
{
dialog.Owner = owner;
LogManager.Debug($"[DialogHelper] 设置 Owner: {owner.Title}");
return true;
}
catch (InvalidOperationException ex)
{
LogManager.Warning($"[DialogHelper] 设置 Owner 失败: {ex.Message}");
}
}
return false;
}
/// <summary>
/// 显示对话框(自动处理 Owner 设置)
/// </summary>
/// <param name="dialog">要显示的对话框</param>
/// <param name="userControl">可选的 UserControl用于获取 Owner</param>
/// <returns>对话框结果</returns>
public static bool? ShowDialog(Window dialog, UserControl userControl = null)
{
if (dialog == null)
throw new ArgumentNullException(nameof(dialog));
// 如果提供了 UserControl优先使用它获取 Owner
if (userControl != null)
{
SetOwnerFromUserControl(dialog, userControl);
}
else
{
// 否则尝试自动查找
SetOwnerSafely(dialog);
}
return dialog.ShowDialog();
}
/// <summary>
/// 显示 MessageBox 并确保置顶
/// </summary>
/// <param name="messageBoxText">消息内容</param>
/// <param name="caption">标题</param>
/// <param name="button">按钮类型</param>
/// <param name="icon">图标</param>
/// <returns>MessageBox 结果</returns>
public static MessageBoxResult ShowMessageBox(
string messageBoxText,
string caption,
MessageBoxButton button = MessageBoxButton.OK,
MessageBoxImage icon = MessageBoxImage.Information)
{
var owner = FindOwnerWindow();
if (owner != null)
{
return MessageBox.Show(owner, messageBoxText, caption, button, icon);
}
return MessageBox.Show(messageBoxText, caption, button, icon);
}
/// <summary>
/// 获取 Navisworks 主窗口句柄Win32
/// </summary>
/// <returns>窗口句柄,或 IntPtr.Zero</returns>
public static IntPtr GetNavisworksMainWindowHandle()
{
try
{
var mainWindow = Autodesk.Navisworks.Api.Application.Gui?.MainWindow;
if (mainWindow != null)
{
return mainWindow.Handle;
}
}
catch (Exception ex)
{
LogManager.Debug($"[DialogHelper] 获取 Navisworks 主窗口句柄失败: {ex.Message}");
}
return IntPtr.Zero;
}
/// <summary>
/// 设置 Win32 父窗口(用于需要强制置顶到 Navisworks 主窗口的场景)
/// </summary>
/// <param name="dialog">对话框</param>
/// <returns>是否成功</returns>
public static bool SetWin32Owner(Window dialog)
{
if (dialog == null)
return false;
var handle = GetNavisworksMainWindowHandle();
if (handle == IntPtr.Zero)
return false;
try
{
var helper = new WindowInteropHelper(dialog);
helper.Owner = handle;
return true;
}
catch (Exception ex)
{
LogManager.Warning($"[DialogHelper] 设置 Win32 Owner 失败: {ex.Message}");
}
return false;
}
}
}