我正在为 Autodesk Inventor 创建插件,我有类库项目,我将在按钮单击时显示一个 wpf 窗口,效果很好。但是,我无法为我的窗口设置所有者属性。在我的研究中,我知道我们需要获取父窗口对象。
问问题
2536 次
3 回答
2
如果您无法获取父窗口,您可以尝试使用窗口句柄设置父窗口。
我不熟悉 Autodesk Inventor 以及您如何为应用程序创建插件,所以我不知道您是否可以获得窗口句柄,但我想您可以知道进程 ID 或窗口标题/标题或其他一些可以帮助您获得的信息父窗口句柄(你应该谷歌如何获取窗口句柄)。获得父窗口句柄后,您可以使用 WindowInteropHelper 将其设置为窗口的所有者。
这里只是一个如何使用 WindowInteropHelper 的示例:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
IntPtr parentWindowHandler = IntPtr.Zero;
// I'll just look for notepad window so I can demonstrate (remember to run notepad before running this sample code :))
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains("Notepad"))
{
parentWindowHandler = pList.MainWindowHandle;
break;
}
}
var interop = new WindowInteropHelper(this);
interop.EnsureHandle();
// this is it
interop.Owner = parentWindowHandler;
// i'll use this to check if owner is set
// if it's set MainWindow will be shown at the center of notepad window
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
}
}
于 2014-08-20T08:05:32.017 回答
1
我只是重申 user1018735 给出的例子。我开发了 Inventor 插件,因此我修改了上面的代码,以确保我始终使用正确的 Inventor 会话,因为可以一次打开多个 Inventor 实例。我通过 _App 参数将我已知的应用程序对象传递给我的表单来做到这一点;然后由于进程 MainWindowTitle 始终与应用程序标题相同,因此我将两者匹配。
我在 WPF 类库@VB.Net 4.5.1 中运行它。这是我在 Inventor 2014 中工作的代码的一瞥...
Public Sub New(ByVal _App As Inventor.Application)
'This call is required by the designer.
InitializeComponent()
'Find the Inventor Window Handle.
Dim InvWndHnd As IntPtr = IntPtr.Zero
'Search the process list for the matching Inventor application.
For Each pList As Process In Process.GetProcesses()
If pList.MainWindowTitle.Contains(_App.Caption) Then
InvWndHnd = pList.MainWindowHandle
Exit For
End If
Next
Dim InvWndIrp = New WindowInteropHelper(Me)
InvWndIrp.EnsureHandle()
InvWndIrp.Owner = InvWndHnd
...
于 2014-09-05T21:53:00.623 回答
0
// Create a window and make this window its owner
Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.Show();
于 2014-08-19T10:24:57.253 回答