Helpers/ParentWindowHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System;
using System.Runtime.InteropServices; enum GetAncestorFlags { GetParent = 1, GetRoot = 2, /// <summary> /// Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent. /// </summary> GetRootOwner = 3 } public static class ParentWindowHelper { [DllImport("user32.dll", ExactSpelling = true)] static extern IntPtr GetAncestor(IntPtr hwnd, GetAncestorFlags flags); [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); // This is your window handle! public static IntPtr GetConsoleOrTerminalWindow() { IntPtr consoleHandle = GetConsoleWindow(); if(null == consoleHandle) throw new System.PlatformNotSupportedException("Could not get console window handle on this OS platform"); IntPtr handle = GetAncestor(consoleHandle, GetAncestorFlags.GetRootOwner ); return handle; } } |