{"record":{"id":"178680855cd5a6e7","repo":"stride3d/stride","slug":"unable-to-retrieve-the-hwndsource-of-this-window","errorCode":null,"errorMessage":"Unable to retrieve the HwndSource of this window","messagePattern":"Unable to retrieve the HwndSource of this window","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/View/Behaviors/DragDrop/DragWindow.cs","lineNumber":31,"sourceCode":"        {\n            AllowDrop = false;\n            AllowsTransparency = true;\n            Background = null;\n            IsHitTestVisible = false;\n            ShowActivated = false;\n            ShowInTaskbar = false;\n            SizeToContent = SizeToContent.WidthAndHeight;\n            Style = null;\n            Topmost = true;\n            WindowStyle = WindowStyle.None;\n            SourceInitialized += InitializeExtendedStyle;\n        }\n\n        private void InitializeExtendedStyle(object sender, EventArgs eventArgs)\n        {\n            var source = (HwndSource)PresentationSource.FromVisual(this);\n            if (source == null)\n                throw new InvalidOperationException(\"Unable to retrieve the HwndSource of this window\");\n\n            int extendedStyle = NativeHelper.GetWindowLong(source.Handle, NativeHelper.GWL_EXSTYLE);\n            extendedStyle = (int)(extendedStyle | NativeHelper.WS_EX_TRANSPARENT | NativeHelper.WS_EX_LAYERED | NativeHelper.WS_EX_TOPMOST);\n            NativeHelper.SetWindowLong(source.Handle, NativeHelper.GWL_EXSTYLE, extendedStyle);\n        }\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":39,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/View/Behaviors/DragDrop/DragWindow.cs#L13-L39","documentation":"DragWindow.InitializeExtendedStyle needs the Win32 HwndSource of the WPF Window to apply WS_EX_TRANSPARENT/WS_EX_LAYERED/WS_EX_TOPMOST styles. PresentationSource.FromVisual(this) returns null when the window has no presentation source yet (not fully shown/loaded into a HWND), so an InvalidOperationException is thrown.","triggerScenarios":"Calling InitializeExtendedStyle (e.g. from a SourceInitialized/Loaded handler wired too early, or constructor) before the window's HWND exists, or after the window was closed.","commonSituations":"Subscribing the handler in the Window constructor instead of waiting for SourceInitialized; showing the window in a non-WPF-hosted context where no HwndSource is created; calling on a Window that never got shown.","solutions":["Hook InitializeExtendedStyle to the SourceInitialized event instead of Loaded/constructor","Guard with a null check on PresentationSource.FromVisual and defer/retry once the window is shown","Ensure Show()/ShowDialog() is actually called and the window is not being manipulated after Close()"],"exampleFix":"// before\npublic DragWindow() { InitializeExtendedStyle(null, null); }\n// after\npublic DragWindow() { SourceInitialized += InitializeExtendedStyle; }","handlingStrategy":"validation","validationCode":"var source = PresentationSource.FromVisual(dragWindow);\nif (source is HwndSource hwndSource) InitializeStyle(hwndSource.Handle); else dragWindow.SourceInitialized += (s,e) => InitializeExtendedStyle(s, e);","typeGuard":"static bool TryGetHwndSource(Visual v, out HwndSource src) => (src = PresentationSource.FromVisual(v) as HwndSource) != null;","tryCatchPattern":"try { InitializeExtendedStyle(sender, e); }\ncatch (InvalidOperationException) { window.SourceInitialized += InitializeExtendedStyle; } // defer until HWND exists","preventionTips":["Always apply Win32 window styles from the SourceInitialized event, not the constructor","Never manipulate HWND-based state after Window.Close()","Guard PresentationSource.FromVisual results for null"],"tags":["wpf","hwnd","window-lifetime"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}