将另一个应用程序带到前面 [英] Bring another application to front

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

问题描述

我已经构建了一个 C# Windows 窗体应用程序.当表单加载时,它是全屏的.表单上有启动其他应用程序(不是表单)的图标.我正在尝试确定应用程序是否已经在运行,如果没有,请启动它,否则将它放在前面.我已经确定了应用程序是否正在运行,如果没有,启动它,我只是不知道如何把它放在前面.我已经阅读了 Google 和 Stack Overflow 上的其他结果,但无法让它们发挥作用.

I have built a C# Windows Form application. When the form loads, it's full screen. The form has icons on it that launch other applications (not forms). I'm trying to accomplish determining whether the application is already running or not and if it's not, start it, otherwise bring it to the front. I have accomplished determining whether the application is running or not and if it's not, to start it, I just can't figure out how to bring it to the front if it is. I have read other results on Google and Stack Overflow, but haven't been able to get them to work.

非常感谢任何帮助!

到目前为止,我的代码是:

My code, so far, is:

private void button4_Click(object sender, EventArgs e)
{
    Process[] processName = Process.GetProcessesByName("ProgramName");
    if (processName.Length == 0)
    {
        //Start application here
        Process.Start("C:\\bin\\ProgramName.exe");
    }
    else
    {
        //Set foreground window
        ?
    }
}

推荐答案

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

private IntPtr handle;

private void button4_Click(object sender, EventArgs e)
{
    Process[] processName = Process.GetProcessesByName("ProgramName");
    if (processName.Length == 0)
    {
        //Start application here
        Process.Start("C:\\bin\\ProgramName.exe");
    }
    else
    {
        //Set foreground window
        handle = processName[0].MainWindowHandle;
        SetForegroundWindow(handle);
    }
}

如果您还希望在窗口最小化的情况下也显示该窗口,请使用:

If you also wish to show the window even if it is minimized, use:

if (IsIconic(handle))
    ShowWindow(handle, SW_RESTORE);

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

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