{"record":{"id":"687c3bcc05cbf0e1","repo":"HandyOrg/HandyControl","slug":"unable-to-combine-two-hrgns","errorCode":null,"errorMessage":"Unable to combine two HRGNs.","messagePattern":"Unable to combine two HRGNs\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/Microsoft.Windows.Shell/WindowChromeWorker.cs","lineNumber":630,"sourceCode":"    private static IntPtr _CreateRoundRectRgn(Rect region, double radius)\n    {\n        if (DoubleUtilities.AreClose(0.0, radius))\n        {\n            return NativeMethods.CreateRectRgn((int) Math.Floor(region.Left), (int) Math.Floor(region.Top), (int) Math.Ceiling(region.Right), (int) Math.Ceiling(region.Bottom));\n        }\n        return NativeMethods.CreateRoundRectRgn((int) Math.Floor(region.Left), (int) Math.Floor(region.Top), (int) Math.Ceiling(region.Right) + 1, (int) Math.Ceiling(region.Bottom) + 1, (int) Math.Ceiling(radius), (int) Math.Ceiling(radius));\n    }\n\n    [SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"HRGNs\")]\n    private static void _CreateAndCombineRoundRectRgn(IntPtr hrgnSource, Rect region, double radius)\n    {\n        IntPtr hrgnSrc = IntPtr.Zero;\n        try\n        {\n            hrgnSrc = WindowChromeWorker._CreateRoundRectRgn(region, radius);\n            if (NativeMethods.CombineRgn(hrgnSource, hrgnSource, hrgnSrc, RGN.OR) == CombineRgnResult.ERROR)\n            {\n                throw new InvalidOperationException(\"Unable to combine two HRGNs.\");\n            }\n        }\n        catch\n        {\n            Utility.SafeDeleteObject(ref hrgnSrc);\n            throw;\n        }\n    }\n\n    private static bool _IsUniform(CornerRadius cornerRadius)\n    {\n        return DoubleUtilities.AreClose(cornerRadius.BottomLeft, cornerRadius.BottomRight) && DoubleUtilities.AreClose(cornerRadius.TopLeft, cornerRadius.TopRight) && DoubleUtilities.AreClose(cornerRadius.BottomLeft, cornerRadius.TopRight);\n    }\n\n    private void _ExtendGlassFrame()\n    {\n        if (!Utility.IsOSVistaOrNewer)\n        {","sourceCodeStart":612,"sourceCodeEnd":648,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/WindowChromeWorker.cs#L612-L648","documentation":"WindowChromeWorker._CreateAndCombineRoundRectRgn builds a rounded-rectangle GDI region and OR-combines it into an existing HRGN using NativeMethods.CombineRgn. If CombineRgn returns CombineRgnResult.ERROR (a GDI failure), the worker throws InvalidOperationException because the window chrome region can no longer be constructed correctly. The original source HRGN is cleaned up safely before rethrowing.","triggerScenarios":"CombineRgn returning ERROR during chrome region creation — typically when one of the HRGN handles is invalid/zero (previous _CreateRoundRectRgn failed under GDI resource pressure), or the target HRGN was already deleted.","commonSituations":"GDI handle exhaustion (many windows/regions leaked), window operations racing with handle destruction, running in Remote Desktop or low-memory sessions where GDI allocations fail.","solutions":["Check for GDI handle leaks (GDI Objects count in Task Manager) and ensure all HRGNs are deleted via DeleteObject","Verify the source hrgnSource handle is valid before combining","Catch the InvalidOperationException around window chrome setup and fall back to a non-chrome (standard) window presentation"],"exampleFix":"// before\nworker.EnableChrome(); // throws InvalidOperationException under GDI pressure\n// after\ntry { worker.EnableChrome(); }\ncatch (InvalidOperationException) { /* fall back: don't customize chrome */ }","handlingStrategy":"try-catch","validationCode":"if (hrgnSource == IntPtr.Zero || hrgnSource == hrgnInvalid) throw new InvalidOperationException(\"Source HRGN is invalid before CombineRgn\");","typeGuard":"bool IsValidRegion(IntPtr hrgn) => hrgn != IntPtr.Zero && NativeMethods.GetRegionData(hrgn, 0, IntPtr.Zero) != 0;","tryCatchPattern":"try { worker.EnableChrome(); } catch (InvalidOperationException ex) when (ex.Message == \"Unable to combine two HRGNs.\") { /* disable chrome customization, keep standard window frame */ }","preventionTips":["Always pair region creation with DeleteObject in a finally block","Monitor GDI object counts for leaks under heavy window churn","Recreate regions rather than reusing stale HRGN handles across window operations"],"tags":["gdi","win32","internal-error","windowchrome"],"backgroundTag":"internal-invariant-violation","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}