将流程带到前台 [英] Bring Process to the front

查看:62
本文介绍了将流程带到前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


在C#应用程序中,如何将使用Process.Start()创建的应用程序实例带到前台?



谢谢

K

Hi
In a C# application, how do I bring the instance of an application that was created using Process.Start() to the foreground?

Thanks
K

推荐答案

尝试:

Try:
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private IntPtr handle;
private Process process;
private void button1_Click(object sender, EventArgs e)
    {
    process = Process.Start("cmd");
    }

private void button2_Click(object sender, EventArgs e)
    {
    handle = process.MainWindowHandle;
    SetForegroundWindow(handle);
    }


解决方案1可以满足您的需求。但我只是想补充一点,如果窗口恰好是最小化的,你需要使用类似的东西:



Solution 1 will work just fine for your need. But I just wanted to add that if the windows happens to be minimized you will need to use something like this:

[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);

const int SW_RESTORE = 9;

Process proc = Process.Start("cmd.exe");

private void button1_Click(object sender, EventArgs e)
{
    IntPtr handle = proc.MainWindowHandle;
    if (IsIconic(handle))
    {
        ShowWindow(handle, SW_RESTORE);
    }

    SetForegroundWindow(handle);
}


遇到同样的问题,希望这会有所帮助:



诀窍是让windows'认为'我们的进程和目标窗口(hwnd)是相关的,通过附加线程(使用AttachThreadInput API)和使用替代API:BringWindowToTop。





强制Window / Internet Explorer到达前台:

Hi, had the same problem, hope this will help:

The trick is to make windows ‘think’ that our process and the target window (hwnd) are related by attaching the threads (using AttachThreadInput API) and using an alternative API: BringWindowToTop.


Forcing Window/Internet Explorer to the foreground:
private static void ForceForegroundWindow(IntPtr hWnd)
{
    uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
    uint appThread = GetCurrentThreadId();
    const uint SW_SHOW = 5;
 
    if (foreThread != appThread)
    {
        AttachThreadInput(foreThread, appThread, true);
        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);
        AttachThreadInput(foreThread, appThread, false);
    }
    else
    {
        BringWindowToTop(hWnd);
        ShowWindow(hWnd, SW_SHOW);
    }
}





AttachedThreadInputAction模式的更高级实现



我的想法是附加到目标流程线程并执行操作。



代码段



A more advanced implementation of AttachedThreadInputAction pattern

The idea is to attach to the target process thread and preform an action.

Code Snippet

public static void AttachedThreadInputAction(Action action)
{
    var foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
    var appThread = GetCurrentThreadId();
    bool threadsAttached = false;
    try
    {
        threadsAttached =
            foreThread == appThread ||
            AttachThreadInput(foreThread, appThread, true);
        if (threadsAttached) action();
        else throw new ThreadStateException("AttachThreadInput failed.");
    }
    finally
    {
        if (threadsAttached)
            AttachThreadInput(foreThread, appThread, false);
    }
}



使用



代码片段


Usage

Code Snippet

public const uint SW_SHOW = 5;

///<summary>
/// Forces the window to foreground.
///</summary>
///hwnd">The HWND.</param>
public static void ForceWindowToForeground(IntPtr hwnd)
{
    AttachedThreadInputAction(
        () =>
        {
            BringWindowToTop(hwnd);
            ShowWindow(hwnd, SW_SHOW);
        });
}

public static IntPtr SetFocusAttached(IntPtr hWnd)
{
    var result = new IntPtr();
    AttachedThreadInputAction(
        () =>
        {
            result = SetFocus(hWnd);
        });
    return result;
}


这篇关于将流程带到前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆