繁体   English   中英

检查进程是否正在运行。 如果有,请打开它

[英]Check if process is runing. if it does - open it

好吧,可以说我有一个正在运行的process

可以说“ Skype”。 我想在C#Windows Form App中单击一个button

当我点击该button (让呼叫button “Skype的运行”)。

因此,当我单击“ Skype Run”时。 它将检查该进程当前是否正在运行。

如果该进程未在运行,可以说它将执行以下操作:

MessageBox.Show("Sorry! Skype is not running.")

但。 如果它正在运行,它将打开它成为您当前正在查看的窗口。

因此,如果您使用的是Word,则单击“ Skype Run”。 Skype正在运行,它打开了窗口。 (就像在Windows的任务管理器中执行“切换到”一样。)

如果我不能很好地解释我的问题,请告诉我。 我只是真的需要答案:)

这里有一个代码示例显示了如何执行此操作:

http://www.codeguru.com/forum/showthread.php?p=1757526

下面的帖子中的相关代码示例。 这显示了如何在button1_Click事件处理程序中查找进程。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //required for APIs
namespace Find

{

public partial class Form1 : Form
{
    //Import the FindWindow API to find our window
    [DllImportAttribute("User32.dll")]
    private static extern int FindWindow(String ClassName, String WindowName);

    //Import the SetForeground API to activate it
    [DllImportAttribute("User32.dll")]
    private static extern IntPtr SetForegroundWindow(int hWnd);


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Find the window, using the CORRECT Window Title, for example, Notepad
        int hWnd = FindWindow(null, "Untitled - Notepad");
        if (hWnd > 0) //If found
        {
            SetForegroundWindow(hWnd); //Activate it
        }
        else
        {
            MessageBox.Show("Window Not Found!");
        }


    }
}
}

您可以使用Process.GetProcessesByName静态方法检查进程是否已在运行。

通过使用SetWindowPos Win API,可以将特定的窗口置于最前面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM