public static void RestoreWindow(ProcessDetails pd) { if ((pd == null) || (!pd.MadeBorderless) || (pd.OriginalStyleFlags_Standard == 0)) { return; } Native.SetWindowLong(pd.WindowHandle, WindowLongIndex.Style, pd.OriginalStyleFlags_Standard); Native.SetWindowLong(pd.WindowHandle, WindowLongIndex.ExtendedStyle, pd.OriginalStyleFlags_Extended); Native.SetWindowPos(pd.WindowHandle, IntPtr.Zero, pd.OriginalLocation.X, pd.OriginalLocation.Y, pd.OriginalLocation.Width, pd.OriginalLocation.Height, SetWindowPosFlags.ShowWindow | SetWindowPosFlags.NoZOrder); Native.SetWindowPos(pd.WindowHandle, Native.HWND_NOTTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.NoActivate | SetWindowPosFlags.NoMove | SetWindowPosFlags.NoSize); pd.MadeBorderless = false; pd.MadeBorderlessAttempts = 0; }
/// <summary> /// remove the menu, resize the window, remove border, and maximize /// </summary> public static void MakeWindowBorderless(ProcessDetails processDetails, MainWindow frmMain, IntPtr targetWindow, Rectangle targetFrame, Favorites.Favorite favDetails) { var isUnrealEngine = IsUnrealEngine(targetWindow); // Automatically match a window to favorite details, if that information is available. // Note: if one is not available, the default settings will be used as a new Favorite() object. // Automatically match this window to a process // Failsafe to prevent rapid switching, but also allow a few changes to the window handle (to be persistent) if (processDetails != null) { if (processDetails.MadeBorderless) { if ((processDetails.MadeBorderlessAttempts > 3) || (!processDetails.WindowHasTargetableStyles)) { return; } } } // If no target frame was specified, assume the entire space on the primary screen if ((targetFrame.Width == 0) || (targetFrame.Height == 0)) { targetFrame = Screen.FromHandle(targetWindow).Bounds; } // Get window styles var styleCurrentWindowStandard = Native.GetWindowLong(targetWindow, WindowLongIndex.Style); var styleCurrentWindowExtended = Native.GetWindowLong(targetWindow, WindowLongIndex.ExtendedStyle); // Compute new styles (XOR of the inverse of all the bits to filter) var styleNewWindowStandard = ( styleCurrentWindowStandard & ~( WindowStyleFlags.Caption // composite of Border and DialogFrame // | WindowStyleFlags.Border //| WindowStyleFlags.DialogFrame | WindowStyleFlags.ThickFrame | WindowStyleFlags.OverlappedWindow | WindowStyleFlags.SystemMenu | WindowStyleFlags.MaximizeBox // same as TabStop | WindowStyleFlags.MinimizeBox // same as Group ) ); var styleNewWindowExtended = ( styleCurrentWindowExtended & ~( WindowStyleFlags.ExtendedDlgModalFrame | WindowStyleFlags.ExtendedComposited | WindowStyleFlags.ExtendedWindowEdge | WindowStyleFlags.ExtendedClientEdge | WindowStyleFlags.ExtendedLayered | WindowStyleFlags.ExtendedStaticEdge | WindowStyleFlags.ExtendedToolWindow | WindowStyleFlags.ExtendedAppWindow ) ); // Should have process details by now if (processDetails != null) { // Save original details on this window so that we have a chance at undoing the process processDetails.OriginalStyleFlags_Standard = styleCurrentWindowStandard; processDetails.OriginalStyleFlags_Extended = styleCurrentWindowExtended; Native.GetWindowRect(processDetails.WindowHandle, out Native.Rect rect_temp); processDetails.OriginalLocation = new Rectangle(rect_temp.Left, rect_temp.Top, rect_temp.Right - rect_temp.Left, rect_temp.Bottom - rect_temp.Top); } // remove the menu and menuitems and force a redraw if (favDetails.RemoveMenus || isUnrealEngine) { // unfortunately, menus can't be re-added easily so they aren't removed by default anymore IntPtr menuHandle = Native.GetMenu(targetWindow); if (menuHandle != IntPtr.Zero) { int menuItemCount = Native.GetMenuItemCount(menuHandle); for (int i = 0; i < menuItemCount; i++) { Native.RemoveMenu(menuHandle, 0, MenuFlags.ByPosition | MenuFlags.Remove); } Native.DrawMenuBar(targetWindow); } } // auto-hide the Windows taskbar (do this before resizing the window) if (favDetails.HideWindowsTaskbar) { Native.ShowWindow(frmMain.Handle, WindowShowStyle.ShowNoActivate); if (frmMain.WindowState == FormWindowState.Minimized) { frmMain.WindowState = FormWindowState.Normal; } ToggleWindowsTaskbarVisibility(Tools.Boolstate.False); } // auto-hide the mouse cursor if (favDetails.HideMouseCursor) { ToggleMouseCursorVisibility(frmMain, Tools.Boolstate.False); } // update window position if (favDetails.SizeMode != Favorites.Favorite.SizeModes.NoChange) { if ((favDetails.SizeMode == Favorites.Favorite.SizeModes.FullScreen) || (favDetails.PositionW == 0) || (favDetails.PositionH == 0)) { // Set the window size to the biggest possible, using bounding adjustments Native.SetWindowPos ( targetWindow, 0, targetFrame.X + favDetails.OffsetL, targetFrame.Y + favDetails.OffsetT, targetFrame.Width - favDetails.OffsetL + favDetails.OffsetR, targetFrame.Height - favDetails.OffsetT + favDetails.OffsetB, SetWindowPosFlags.FrameChanged | SetWindowPosFlags.ShowWindow | SetWindowPosFlags.NoOwnerZOrder | SetWindowPosFlags.NoSendChanging ); // And auto-maximize if (favDetails.ShouldMaximize && !isUnrealEngine) { Native.ShowWindow(targetWindow, WindowShowStyle.Maximize); } } else { // Set the window size to the exact position specified by the user Native.SetWindowPos ( targetWindow, 0, favDetails.PositionX, favDetails.PositionY, favDetails.PositionW, favDetails.PositionH, SetWindowPosFlags.FrameChanged | SetWindowPosFlags.ShowWindow | SetWindowPosFlags.NoOwnerZOrder | SetWindowPosFlags.NoSendChanging ); } } // Set topmost if (favDetails.TopMost) { Native.SetWindowPos ( targetWindow, Native.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.FrameChanged | SetWindowPosFlags.ShowWindow | SetWindowPosFlags.NoMove | SetWindowPosFlags.NoSize | SetWindowPosFlags.NoSendChanging ); } //wait before applying styles if (isUnrealEngine) { Thread.Sleep(4200); } // update window styles Native.SetWindowLong(targetWindow, WindowLongIndex.Style, styleNewWindowStandard); Native.SetWindowLong(targetWindow, WindowLongIndex.ExtendedStyle, styleNewWindowExtended); // Make a note that we attempted to make the window borderless if (processDetails != null) { processDetails.MadeBorderless = true; processDetails.MadeBorderlessAttempts++; } if (Program.SteamLoaded) { BorderlessGamingSteam.Achievement_Unlock(0); } }