如何在 Microsoft PowerPoint / Office 应用程序中触发模式对话框关闭?
How to trigger a modal dialog box to close in Microsoft PowerPoint / Office applications?
问题
我正在尝试使用 VSTO 加载项检测并关闭 PowerPoint 中打开的 WPF 对话框。当我使用 this question 的解决方案时,它似乎不起作用,因为 System.Windows.Application.Current
总是 return null 即使打开了一个对话框。
代码
我的对话框不是使用默认的 Winform 作为对话框,而是 WPF Window,例如,
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="Test"
WindowStyle="None"
SizeToContent="WidthAndHeight">
...
</Window>
这是隐藏代码:
namespace AddInProject.Classes
{
public partial class DlgCustomWindow:Window, IDisposable
{
public CustomWindow()
{
InitializeComponent();
}
public Dispose()
{
this.Close();
}
}
}
我用这个方法打开上面的WPFwindow
using (DlgCustomWindow dlgCustom = new DlgCustomWindow())
{
dlgCustom.ShowDialog();
}
但是 运行 System.Windows.Application.Current
始终 return 为空。
我使用 win32 API 的 FindWindow
使用对话框的标题或标题找到要关闭的对话框的指针引用。然后我使用 win32 的 SendMessage
通过使用之前找到的指针引用触发正确的对话框关闭。
将这些代码放入您的任何 class:
[DllImport("user32.dll",SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll",CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,IntPtr wParam,IntPtr lParam);
public static bool CloseWindowIfOpen(string name = "")
{
IntPtr hWnd = (IntPtr)0;
hWnd = FindWindow(null,name);
if ((int)hWnd!=0)
{
//Close Window
SendMessage(hWnd,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
return true;
}
return false;
}
所以它可以像这样使用:
YourClass.CloseWindowIfOpen("CaptionOfModalDialog");
备注
到目前为止,我只能通过输入要关闭的对话框的标题才能成功。您还应该能够使用对话框的 class 名称,但我没有成功。例如,我的对话框 class 名称 DlgCustomWindow
位于命名空间:AddInProject.Classes
。当我使用 FindWindow("DlgCustomWindow",name)
或 FindWindow("AddInProject.Classes.DlgCustomWindow",name)
时,FindWindow
找不到模态对话框
问题
我正在尝试使用 VSTO 加载项检测并关闭 PowerPoint 中打开的 WPF 对话框。当我使用 this question 的解决方案时,它似乎不起作用,因为 System.Windows.Application.Current
总是 return null 即使打开了一个对话框。
代码
我的对话框不是使用默认的 Winform 作为对话框,而是 WPF Window,例如,
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="Test"
WindowStyle="None"
SizeToContent="WidthAndHeight">
...
</Window>
这是隐藏代码:
namespace AddInProject.Classes
{
public partial class DlgCustomWindow:Window, IDisposable
{
public CustomWindow()
{
InitializeComponent();
}
public Dispose()
{
this.Close();
}
}
}
我用这个方法打开上面的WPFwindow
using (DlgCustomWindow dlgCustom = new DlgCustomWindow())
{
dlgCustom.ShowDialog();
}
但是 运行 System.Windows.Application.Current
始终 return 为空。
我使用 win32 API 的 FindWindow
使用对话框的标题或标题找到要关闭的对话框的指针引用。然后我使用 win32 的 SendMessage
通过使用之前找到的指针引用触发正确的对话框关闭。
将这些代码放入您的任何 class:
[DllImport("user32.dll",SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll",CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,IntPtr wParam,IntPtr lParam);
public static bool CloseWindowIfOpen(string name = "")
{
IntPtr hWnd = (IntPtr)0;
hWnd = FindWindow(null,name);
if ((int)hWnd!=0)
{
//Close Window
SendMessage(hWnd,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
return true;
}
return false;
}
所以它可以像这样使用:
YourClass.CloseWindowIfOpen("CaptionOfModalDialog");
备注
到目前为止,我只能通过输入要关闭的对话框的标题才能成功。您还应该能够使用对话框的 class 名称,但我没有成功。例如,我的对话框 class 名称 DlgCustomWindow
位于命名空间:AddInProject.Classes
。当我使用 FindWindow("DlgCustomWindow",name)
或 FindWindow("AddInProject.Classes.DlgCustomWindow",name)
FindWindow
找不到模态对话框