Question

OK, j'ai trouvé de nombreux articles sur la recherche d'une fenêtre par nom, etc. Ce que je n'ai pas trouvé, c'est comment trouver et changer la focalisation de l'application de fenêtre pour la dernière fenêtre active. Le code que je montre ci-dessous me donnera la liste des applications actives dans le gestionnaire de tâches qui sont actives.

Ce que je ne peux pas comprendre comment faire, c'est déterminer quelle application était la dernière application active, puis passer à elle. par exemple...

J'ai mon application WinForm personnalisée ouverte.

Je clique sur un bouton

Mon application passe à la dernière fenêtre / application active.

Voici le code de travail que j'ai jusqu'à présent. (Il s'agit de l'action sur un bouton, et il s'attend à ce que l'application ait une zone de texte nommée TextBox1. Vous devrez également ajouter en utilisant System.Diagnostics;

    private void button1_Click(object sender, EventArgs e)
    {

        Process[] procs = Process.GetProcesses();
        IntPtr hWnd;
        foreach (Process proc in procs)
        {
            if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
            {
                textBox1.Text += (proc.ProcessName.ToString());
                textBox1.Text += "\t";
                textBox1.Text += (hWnd.ToString());
                textBox1.Text += "\r\n";
            }
        }         

    }
Était-ce utile?

La solution

Puisque mes commentaires ne vous ont pas aidé, voici un petit CV (ne l'a pas testé cependant):

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

[DllImport("user32.dll")]
static extern IntPtr GetLastActivePopup(IntPtr hWnd);

[DllImport("user32.dll", ExactSpelling = true)]
static extern IntPtr GetForegroundWindow();

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

const uint GA_PARENT = 1;
const uint GA_ROOT = 2;
const uint GA_ROOTOWNER = 3;

public IntPtr GetPreviousWindow()
{
        IntPtr activeAppWindow = GetForegroundWindow();
        if ( activeAppWindow == IntPtr.Zero )
            return IntPtr.Zero;

        IntPtr prevAppWindow = GetLastActivePopup(activeAppWindow);
        return IsWindowVisible(prevAppWindow) ? prevAppWindow : IntPtr.Zero;
 }

 public void FocusToPreviousWindow()
 {
     IntPtr prevWindow = GetPreviousWindow();
     if (  prevWindow != IntPtr.Zero )
         SetForegroundWindow(prevWindow);
 }

Autres conseils

Consultez cet article: http://www.whitebyte.info/programming/how-to-get-main-window-handle-of-the-last-active-window

Plus précisément, ce code:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
enum GetWindow_Cmd : uint
{
    GW_HWNDFIRST = 0,
    GW_HWNDLAST = 1,
    GW_HWNDNEXT = 2,
    GW_HWNDPREV = 3,
    GW_OWNER = 4,
    GW_CHILD = 5,
    GW_ENABLEDPOPUP = 6
}
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[...]

IntPtr targetHwnd = GetWindow(Process.GetCurrentProcess().MainWindowHandle, (uint)GetWindow_Cmd.GW_HWNDNEXT);
while (true)
{
    IntPtr temp = GetParent(targetHwnd);
    if (temp.Equals(IntPtr.Zero)) break;
    targetHwnd = temp;
}
SetForegroundWindow(targetHwnd);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top