private T GetWindowInfoValue <T>(IntPtr handle, Func <WindowInfo, T> selector) { var info = WindowInfo.Create(); if (User32Api.GetWindowInfo(handle, ref info)) { return(selector(info)); } return(default);
/// <summary> /// Get the WindowInfo /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="forceUpdate">set to true to make sure the value is updated</param> /// <returns>WindowInfo</returns> public static WindowInfo GetInfo(this IInteropWindow interopWindow, bool forceUpdate = false) { if (interopWindow.Info.HasValue && !forceUpdate) { return(interopWindow.Info.Value); } var windowInfo = WindowInfo.Create(); User32Api.GetWindowInfo(interopWindow.Handle, ref windowInfo); // Now correct the bounds, for Windows 10 if (Dwm.IsDwmEnabled) { NativeRect extendedFrameBounds; bool gotFrameBounds = Dwm.GetExtendedFrameBounds(interopWindow.Handle, out extendedFrameBounds); if (gotFrameBounds && (interopWindow.IsApp() || WindowsVersion.IsWindows10OrLater && !interopWindow.IsMaximized())) { windowInfo.Bounds = extendedFrameBounds; } } interopWindow.Info = windowInfo; return(interopWindow.Info.Value); }
/// <summary> /// Get the WindowInfo /// </summary> /// <param name="interopWindow">InteropWindow</param> /// <param name="forceUpdate">set to true to make sure the value is updated</param> /// <param name="autoCorrect">enable auto correction, e,g, have the bounds cropped to the parent(s)</param> /// <returns>WindowInfo</returns> public static WindowInfo GetInfo(this IInteropWindow interopWindow, bool forceUpdate = false, bool autoCorrect = true) { if (interopWindow.Info.HasValue && !forceUpdate) { return(interopWindow.Info.Value); } var windowInfo = WindowInfo.Create(); User32Api.GetWindowInfo(interopWindow.Handle, ref windowInfo); // Test if we need to correct some values if (autoCorrect) { // Correct the bounds, for Windows 8+ if (Dwm.IsDwmEnabled) { // This only works for top level windows, otherwise a access denied is returned bool gotFrameBounds = Dwm.GetExtendedFrameBounds(interopWindow.Handle, out var extendedFrameBounds); if (gotFrameBounds && (interopWindow.IsApp() || WindowsVersion.IsWindows10OrLater && !interopWindow.IsMaximized())) { windowInfo.Bounds = extendedFrameBounds; } } var parentWindow = interopWindow.GetParentWindow(); if (interopWindow.HasParent) { var parentInfo = parentWindow.GetInfo(forceUpdate, true); windowInfo.Bounds = windowInfo.Bounds.Intersect(parentInfo.Bounds); windowInfo.ClientBounds = windowInfo.ClientBounds.Intersect(parentInfo.ClientBounds); } } interopWindow.Info = windowInfo; return(windowInfo); }