Laden...

Anderes Fenster in Vordergrund holen

Erstellt von Dawamaha vor 14 Jahren Letzter Beitrag vor 7 Jahren 6.476 Views
D
Dawamaha Themenstarter:in
53 Beiträge seit 2009
vor 14 Jahren
Anderes Fenster in Vordergrund holen

Ich suche nach einem Mittel, wie ich andere Fenster (z.B. Firefox) nach Klick auf einen Button in den Vordergrund holen kann.
Danke im Vorraus,
Dawamaha

49.485 Beiträge seit 2005
vor 14 Jahren

Hallo Dawamaha,

das geht verlässlich nur über Win32. Siehe SetForegroundWindow.

herbivore

D
Dawamaha Themenstarter:in
53 Beiträge seit 2009
vor 14 Jahren

Danke erstmal!
Aber wie kann man denn da an das jeweilige Fenster-Handle kommen?

49.485 Beiträge seit 2005
vor 14 Jahren

Hallo Dawamaha,

auch per Win32: EnumWindows.

Wenn es es nur um das jeweilige Haupfenster geht, auch mit Process.MainWindowHandle.

herbivore

J
237 Beiträge seit 2008
vor 14 Jahren

Gibt es da nicht eine Funktion FindWindow?
EDIT: http://pinvoke.net/default.aspx/user32.FindWindow
Und herbivore's EnumWindows: http://pinvoke.net/default.aspx/user32.EnumWindows

Grüße, JasonDelife.

Beim Programmieren löst man die Probleme, die man nicht hätte, programmierte man nicht.

D
Dawamaha Themenstarter:in
53 Beiträge seit 2009
vor 14 Jahren

Danke für die Antworten. Zur Vollständigkeit hier der Code:


        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        bool SetForeground(string window)
        {
            return SetForegroundWindow(FindWindow(null, window));
        }

E
27 Beiträge seit 2006
vor 9 Jahren

Bei Windows 7 kann dieser Befehl aber ab und zu nicht funktionieren (SetForegroundWindow Win32-API not always works on Windows-7)
Dabei habe ich festgestellt, dass das Problem meißtens auftritt, wenn ein Fenster in der Taskleiste minimiert ist.
Bezogen auf dem Programmcode von der oben genannten Seite habe ich folgende Lösung:


        [DllImport("user32.dll", SetLastError = true)]
        static extern bool BringWindowToTop(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

        enum ShowWindowCommands
        {
            /// <summary>
            /// Hides the window and activates another window.
            /// </summary>
            Hide = 0,
            Normal = 1,
            /// <summary>
            /// Activates the window and displays it as a minimized window.
            /// </summary>
            ShowMinimized = 2,
            /// <summary>
            /// Maximizes the specified window.
            /// </summary>
            Maximize = 3, // is this the right value?
            /// <summary>
            /// Activates the window and displays it as a maximized window.
            /// </summary>       
            ShowMaximized = 3,
            /// <summary>
            /// Displays a window in its most recent size and position. This value 
            /// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except 
            /// the window is not activated.
            /// </summary>
            ShowNoActivate = 4,
            /// <summary>
            /// Activates the window and displays it in its current size and position. 
            /// </summary>
            Show = 5,
            /// <summary>
            /// Minimizes the specified window and activates the next top-level 
            /// window in the Z order.
            /// </summary>
            Minimize = 6,
            /// <summary>
            /// Displays the window as a minimized window. This value is similar to
            /// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the 
            /// window is not activated.
            /// </summary>
            ShowMinNoActive = 7,
            /// <summary>
            /// Displays the window in its current size and position. This value is 
            /// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the 
            /// window is not activated.
            /// </summary>
            ShowNA = 8,
            /// <summary>
            /// Activates and displays the window. If the window is minimized or 
            /// maximized, the system restores it to its original size and position. 
            /// An application should specify this flag when restoring a minimized window.
            /// </summary>
            Restore = 9,
            /// <summary>
            /// Sets the show state based on the SW_* value specified in the 
            /// STARTUPINFO structure passed to the CreateProcess function by the 
            /// program that started the application.
            /// </summary>
            ShowDefault = 10,
            /// <summary>
            ///  <b>Windows 2000/XP:</b> Minimizes a window, even if the thread 
            /// that owns the window is not responding. This flag should only be 
            /// used when minimizing windows from a different thread.
            /// </summary>
            ForceMinimize = 11
        }

        public bool BringToFront (IntPtr win)
        {
            BringWindowToTop(win);
            Rectangle rect = new Rectangle();
            GetWindowRect(win, ref rect); 
            if (rect.Left > -30000)
            {
                ShowWindow(win, ShowWindowCommands.Show);
            }
            else
            {
                ShowWindow(win, ShowWindowCommands.Restore);
            }
            return true;
        }

Was noch fehlt ist eine Abfrage, ob die Funktion auch wirklich geklappt hat, aktuell gibt diese immer nur "true" zurück.

Gruß Marco

S
789 Beiträge seit 2007
vor 7 Jahren

Altes Thema, selbiges Problem:

Auf meinem Windows 8.1 Rechner funktioniert das mit SetForegroundWindow einwandfrei, auf dem Zielrechner, welches ein Windows 7 Rechner ist, blinkt die Anwendung in der Taskleiste nur gelb und wird nicht in den Vordergrund geholt.
Auch mit dem Script über mir funktioniert es leider nicht.

Kann das auch an Benutzerrechten liegen?

49.485 Beiträge seit 2005
vor 7 Jahren

Hallo schuppsl,

der Link von esparki geht zwar mittlerweile ins Leere, aber per Google-Suche nach SetForegroundWindow Win32-API not always works on Windows-7 findet mal vermutlich so ziemlich alles, was das Netz dazu weiß. Und anscheinend gibt es unter Windows 7 keine wirklich zuverlässige Lösung, die in jeder Situation funktioniert.

herbivore