1

我正在尝试通过直接创建来创建一个窗口HwndSource。目前我有一个替代解决方案 - 从Window类继承,但我只是好奇我的HwndSource实现有什么问题。由于在其核心中Window使用HwndSource,我觉得应该有一种方法。

这是我的代码的简化版本:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    HwndSource wndPopup = new HwndSource(0, 0x12cf0000/*WS_VISIBLE|WS_OVERLAPPEDWINDOW*/, 0, 10, 10, 500, 500, "Test", IntPtr.Zero);
    wndPopup.RootVisual = new Rectangle() { Fill = Brushes.Red, Width = 100, Height = 100 };
}

窗口按预期创建,但在我关闭它(Alt+F4 或关闭图标)并将鼠标悬停在我的主窗口上后,大量消息被吐到调试器中:

抛出异常:WindowsBase.dll 中的“System.ComponentModel.Win32Exception”

异常详情如下:

Exception thrown: 'System.ComponentModel.Win32Exception' in WindowsBase.dll
Additional information: Invalid window handle

堆栈跟踪:

 WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetWindowText(System.Runtime.InteropServices.HandleRef, System.Text.StringBuilder, int)
 PresentationCore.dll!System.Windows.Automation.Peers.GenericRootAutomationPeer.GetNameCore()
 PresentationCore.dll!System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
 PresentationCore.dll!System.Windows.ContextLayoutManager.fireAutomationEvents()
 PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout()
 PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayoutCallback(object)
 PresentationCore.dll!System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
 PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
 PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object)
 PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object)
 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, object, int)
 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object, System.Delegate, object, int, System.Delegate)
 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl()
 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object)
 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke()
 WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue()
 WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr, int, System.IntPtr, System.IntPtr, ref bool)
 WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr, int, System.IntPtr, System.IntPtr, ref bool)
 WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object)
 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, object, int)
 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object, System.Delegate, object, int, System.Delegate)
 WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, object, int)
 WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr, int, System.IntPtr, System.IntPtr)
 [Native to Managed Transition]    
 [Managed to Native Transition]    
 WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
 WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
 PresentationFramework.dll!System.Windows.Application.RunDispatcher(object)
 PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window)
 PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window)
 PresentationFramework.dll!System.Windows.Application.Run()
 WpfCombox.exe!WpfCombox.App.Main()
 [Native to Managed Transition]    
 [Managed to Native Transition]    
 mscorlib.dll!System.AppDomain.ExecuteAssembly(string, System.Security.Policy.Evidence, string[])
 Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object)
 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()

因此,错误发生在框架代码深处。

我尝试填充其他属性或向Disposed处理程序添加清理,但这对结果没有影响。如果我不设置RootVisual,则没有错误(但是,当然,这不是一个选项)。

有什么我想念的想法吗?

4

1 回答 1

1

我发现的一种解决方案是为根视觉创建一个自动化对等点。否则,GenericRootAutomationPeer将由调用GetWindowTextWindows API 的框架创建。

public class PopupRootAutomationPeer : UIElementAutomationPeer
{
    public PopupRootAutomationPeer(FrameworkElement owner)
        : base(owner) { }

    protected override string GetClassNameCore()
    {
        return "Pane";
    }

    protected override AutomationControlType GetAutomationControlTypeCore()
    {
        return AutomationControlType.Pane;
    }

    protected override string GetNameCore()
    {
        return "PopupRootAutomationPeer";
    }
}

// Wrap content of the window with this class
class PopupRoot : Canvas
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new PopupRootAutomationPeer(this);
    }
}

可能有一种方法可以HwndSource在它关闭时取消初始化,这样就不会在不存在的窗口上调用自动化,但我还没有找到它。

于 2017-05-11T18:17:32.743 回答