コード例 #1
0
        /// <summary>
        ///   Retrieves the current state of the window.
        /// </summary>
        public WindowState GetWindowState()
        {
            User32.WindowPlacement placement = GetWindowPlacement();
            var state = (User32.WindowState)placement.ShowCommand;

            return(EnumHelper <User32.WindowState> .Convert(state, _windowStateMapping));
        }
コード例 #2
0
        /// <summary>
        ///   Retrieves placement information of a window in a User32.dll structure.
        /// </summary>
        User32.WindowPlacement GetWindowPlacement()
        {
            User32.WindowPlacement placement = User32.WindowPlacement.Default;
            if (!User32.GetWindowPlacement(Handle, ref placement))
            {
                MarshalHelper.ThrowLastWin32ErrorException();
            }

            return(placement);
        }
コード例 #3
0
        public static IEnumerable <Window> GetOpenWindows()
        {
            var shell_window           = User32.GetShellWindow();
            var application_process_id = Process.GetCurrentProcess().Id;
            var windows = new List <Window>();

            User32.EnumWindows((wnd, param) =>
            {
                if (wnd == shell_window || !User32.IsWindowVisible(wnd))
                {
                    return(true);
                }

                var length = User32.GetWindowTextLength(wnd);
                if (length == 0)
                {
                    return(true);
                }

                var sb = new StringBuilder(length);
                User32.GetWindowText(wnd, sb, length + 1);

                uint pid;
                User32.GetWindowThreadProcessId(wnd, out pid);
                var process = Process.GetProcessById((int)pid);
                if (process.Id == application_process_id)
                {
                    return(true);
                }

                var placement    = new User32.WindowPlacement();
                placement.length = Marshal.SizeOf(placement);
                User32.GetWindowPlacement(wnd, ref placement);

                windows.Add(new Window
                {
                    Handle      = wnd,
                    Title       = sb.ToString(),
                    ProcessName = process.ProcessName,
                    ProcessPath = process.MainModule.FileName,
                    Admin       = process.IsAdmin(),
                    Placement   = placement
                });

                return(true);
            }, 0);

            return(windows);
        }
        public RepositionWindowInfo( WindowInfo toPosition )
        {
            ToPosition = toPosition;

            _originalVisible = toPosition.IsVisible();
            Visible = _originalVisible;

            var placement = new User32.WindowPlacement();
            User32.GetWindowPlacement( toPosition.Handle, ref placement );

            User32.Rectangle position = placement.NormalPosition;
            _originalX = position.Left;
            X = _originalX;
            _originalY = position.Top;
            Y = _originalY;
            // TODO: According to the documentation, the pixel at (right, bottom) lies immediately outside the rectangle. Do we need to subtract with 1?
            _originalWidth = position.Right - position.Left;
            Width = _originalWidth;
            _originalHeight = position.Bottom - position.Top;
            Height = _originalHeight;
        }
        public RepositionWindowInfo(WindowInfo toPosition)
        {
            ToPosition = toPosition;

            _originalVisible = toPosition.IsVisible();
            Visible          = _originalVisible;

            var placement = new User32.WindowPlacement();

            User32.GetWindowPlacement(toPosition.Handle, ref placement);

            User32.Rectangle position = placement.NormalPosition;
            _originalX = position.Left;
            X          = _originalX;
            _originalY = position.Top;
            Y          = _originalY;
            // TODO: According to the documentation, the pixel at (right, bottom) lies immediately outside the rectangle. Do we need to subtract with 1?
            _originalWidth  = position.Right - position.Left;
            Width           = _originalWidth;
            _originalHeight = position.Bottom - position.Top;
            Height          = _originalHeight;
        }