private static bool GetMainWindowForProcess_EnumWindows(List <IntPtr> ptrList, IntPtr hWndEnumerated, uint lParam)
        {
            WindowStyleFlags styleCurrentWindowStandard = Native.GetWindowLong(hWndEnumerated, WindowLongIndex.Style);

            if (lParam == 0)             // strict: windows that are visible and have a border
            {
                if (Native.IsWindowVisible(hWndEnumerated))
                {
                    if
                    (
                        ((styleCurrentWindowStandard & WindowStyleFlags.Caption) > 0) &&
                        (
                            ((styleCurrentWindowStandard & WindowStyleFlags.Border) > 0) ||
                            ((styleCurrentWindowStandard & WindowStyleFlags.ThickFrame) > 0)
                        )
                    )
                    {
                        ptrList.Add(hWndEnumerated);
                    }
                }
            }
            else if (lParam == 1)             // loose: windows that are visible
            {
                if (Native.IsWindowVisible(hWndEnumerated))
                {
                    if (((uint)styleCurrentWindowStandard) != 0)
                    {
                        ptrList.Add(hWndEnumerated);
                    }
                }
            }
            return(true);
        }
Exemple #2
0
 public static extern IntPtr CreateWindowEx(
     ExtendedWindowStyleFlags exStyle,
     [MarshalAs(UnmanagedType.LPTStr)] string className,
     [MarshalAs(UnmanagedType.LPTStr)] string windowTitle,
     WindowStyleFlags style,
     int x, int y,
     int width, int height,
     IntPtr hParentWnd,
     IntPtr hMenu,
     IntPtr hInstance,
     IntPtr hParam);
        // Do some preferential treatment to windows
        private static bool GetMainWindowForProcess_EnumWindows(IntPtr hWndEnumerated, uint lParam)
        {
            if (GetMainWindowForProcess_Value == IntPtr.Zero)
            {
                WindowStyleFlags styleCurrentWindow_standard = GetWindowLong(hWndEnumerated, WindowLongIndex.Style);

                if (lParam == 0) // strict: windows that are visible and have a border
                {
                    if (IsWindowVisible(hWndEnumerated))
                    {
                        if
                        (
                            ((styleCurrentWindow_standard & WindowStyleFlags.Caption) > 0) &&
                            (
                                ((styleCurrentWindow_standard & WindowStyleFlags.Border) > 0) ||
                                ((styleCurrentWindow_standard & WindowStyleFlags.ThickFrame) > 0)
                            )
                        )
                        {
                            GetMainWindowForProcess_Value = hWndEnumerated;
                            return(false);
                        }
                    }
                }
                else if (lParam == 1) // loose: windows that are visible
                {
                    if (IsWindowVisible(hWndEnumerated))
                    {
                        if (((uint)styleCurrentWindow_standard) != 0)
                        {
                            GetMainWindowForProcess_Value = hWndEnumerated;
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemple #4
0
        public Window(IntPtr HWnd,
                      Point Location,
                      Size Size,
                      WindowStyleFlags Style,
                      WindowExStyleFlags ExStyle,
                      FormWindowState State,
                      string ClassName)
        {
            _sysWin = new SystemWindow(HWnd);
            IsValid = false; // until proven otherwise

            // SystemWindow.IsValid only checks that the HWnd != IntPtr.Zero, so we perform our own checks
            try
            {
                // if the ClassNames don't match, it's probably not the same window
                IsValid = (ClassName == _sysWin.ClassName);
                if (!IsValid)
                {
                    return;
                }
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                // this might not really be an error...
                if (ex.HResult == 0)
                {
                    IsValid = true;
                }
                // ... but usually it is.  attempting to access an invalid window throws a Win32Exception whose
                // NativeErrorCode is 0x0 ("operation completed successfully"), but whose HResult is not S_OK =
                // 0x0 (it's usually E_FAIL, "Unspecified failure", 0x80004005).
            }

            this.Location = Location;
            this.Size     = Size;
            this.Style    = Style;
            this.ExStyle  = ExStyle;
            this.State    = State;
        }
Exemple #5
0
 public static extern Boolean AdjustWindowRectEx(ref Rectangle coords, WindowStyleFlags dwStyle, [MarshalAs(UnmanagedType.Bool)] bool hasMenu, ExtendedWindowStyleFlags dwExStyle);
Exemple #6
0
 public static bool HasExtendedStyles(this WindowStyleFlags flags)
 {
     return(ExtendedStyles.Any(style => flags.HasFlag(style)));
 }
Exemple #7
0
 public static bool HasTargetStyles(this WindowStyleFlags flags)
 {
     return(TargetStyles.Any(style => flags.HasFlag(style)));
 }
Exemple #8
0
 /// <summary>
 // This static method is required because legacy OSes do not support SetWindowLongPtr
 /// </summary>
 public static WindowStyleFlags SetWindowLong(IntPtr hWnd, WindowLongIndex nIndex, WindowStyleFlags dwNewLong)
 {
     return(IntPtr.Size == 8
         ? SetWindowLong64(hWnd, nIndex, dwNewLong)
         : SetWindowLong32(hWnd, nIndex, dwNewLong));
 }
 public static extern WindowStyleFlags SetWindowLongPtr64(IntPtr hWnd, WindowLongIndex nIndex, WindowStyleFlags dwNewLong);
Exemple #10
0
 public static extern WindowStyleFlags SetWindowLong(IntPtr hWnd, int nIndex, WindowStyleFlags dwNewLong);
Exemple #11
0
        /// <summary>
        ///     remove the menu, resize the window, remove border, and maximize
        /// </summary>
        public static void MakeWindowBorderless(ProcessDetails processDetails, Forms.MainWindow frmMain, IntPtr targetWindow, Rectangle targetFrame, Favorites.Favorite favDetails)
        {
            // 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
            WindowStyleFlags styleCurrentWindow_standard = Native.GetWindowLong(targetWindow, WindowLongIndex.Style);
            WindowStyleFlags styleCurrentWindow_extended = Native.GetWindowLong(targetWindow, WindowLongIndex.ExtendedStyle);

            // Compute new styles (XOR of the inverse of all the bits to filter)
            WindowStyleFlags styleNewWindow_standard =
                (
                    styleCurrentWindow_standard
                    & ~(
                        WindowStyleFlags.Caption // composite of Border and DialogFrame
                        //   | WindowStyleFlags.Border
                        //   | WindowStyleFlags.DialogFrame
                        | WindowStyleFlags.ThickFrame
                        | WindowStyleFlags.SystemMenu
                        | WindowStyleFlags.MaximizeBox // same as TabStop
                        | WindowStyleFlags.MinimizeBox // same as Group
                        )
                );

            WindowStyleFlags styleNewWindow_extended =
                (
                    styleCurrentWindow_extended
                    & ~(
                        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 = styleCurrentWindow_standard;
                processDetails.OriginalStyleFlags_Extended = styleCurrentWindow_extended;
                Native.RECT rect_temp = new Native.RECT();
                Native.GetWindowRect(processDetails.WindowHandle, out 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)
            {
                // 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;
                }

                Manipulation.ToggleWindowsTaskbarVisibility(Tools.Boolstate.False);
            }

            // auto-hide the mouse cursor
            if (favDetails.HideMouseCursor)
            {
                Manipulation.ToggleMouseCursorVisibility(frmMain, Tools.Boolstate.False);
            }

            // update window styles
            Native.SetWindowLong(targetWindow, WindowLongIndex.Style, styleNewWindow_standard);
            Native.SetWindowLong(targetWindow, WindowLongIndex.ExtendedStyle, styleNewWindow_extended);

            // 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.ShowWindow | SetWindowPosFlags.NoOwnerZOrder
                    );

                    // And auto-maximize
                    if (favDetails.ShouldMaximize)
                    {
                        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.ShowWindow | SetWindowPosFlags.NoOwnerZOrder
                    );
                }
            }

            // Set topmost
            if (favDetails.TopMost)
            {
                Native.SetWindowPos
                (
                    targetWindow,
                    Native.HWND_TOPMOST,
                    0,
                    0,
                    0,
                    0,
                    SetWindowPosFlags.ShowWindow | SetWindowPosFlags.NoMove | SetWindowPosFlags.NoSize
                );
            }

            // Make a note that we attempted to make the window borderless
            if (processDetails != null)
            {
                processDetails.MadeBorderless = true;
                processDetails.MadeBorderlessAttempts++;
            }
            return;
        }
Exemple #12
0
 public static extern WindowStyleFlags SetWindowLong(IntPtr hWnd, int nIndex, WindowStyleFlags dwNewLong);
        /// <summary>
        // This static method is required because legacy OSes do not support SetWindowLongPtr 
        /// </summary>
        public static WindowStyleFlags SetWindowLong(IntPtr hWnd, WindowLongIndex nIndex, WindowStyleFlags dwNewLong)
        {
            if (IntPtr.Size == 8)
                return Native.SetWindowLong64(hWnd, nIndex, dwNewLong);

            return Native.SetWindowLong32(hWnd, nIndex, dwNewLong);
        }
 private static extern WindowStyleFlags SetWindowLong64(IntPtr hWnd, WindowLongIndex nIndex, WindowStyleFlags dwNewLong);
Exemple #15
0
 public static extern int SetWindowLong(IntPtr hWnd, WindowFlags nIndex, WindowStyleFlags dwNewLong);
Exemple #16
0
 /// <summary>
 ///     Set the WindowStyle
 /// </summary>
 /// <param name="interopWindow">InteropWindow</param>
 /// <param name="windowStyleFlags">WindowStyleFlags</param>
 /// <returns>IInteropWindow for fluent calls</returns>
 public static IInteropWindow SetStyle(this IInteropWindow interopWindow, WindowStyleFlags windowStyleFlags)
 {
     User32Api.SetWindowLongWrapper(interopWindow.Handle, WindowLongIndex.GWL_STYLE, new IntPtr((uint)windowStyleFlags));
     interopWindow.Info = null;
     return(interopWindow);
 }
Exemple #17
0
 public static extern bool AdjustWindowRectEx(ref RECT lpRect, WindowStyleFlags dwStyle, bool bMenu, uint dwExStyle);
Exemple #18
0
 private static extern WindowStyleFlags SetWindowLong64(IntPtr hWnd, WindowLongIndex nIndex,
                                                        WindowStyleFlags dwNewLong);
 private static IEnumerable <string> Convert(WindowStyleFlags styles) =>
 from styleName in Enum.GetNames(typeof(WindowStyleFlags)) let style = (WindowStyleFlags)Enum.Parse(typeof(WindowStyleFlags), styleName) where (styles & style) == style select $"{styleName} (0x{(uint) style:X8})";