[Winform]检测exe是否已经运行,并将其置顶

栏目: ASP.NET · 发布时间: 8年前

内容简介:[Winform]检测exe是否已经运行,并将其置顶

摘要

在很多pc应用中,基本上都需要有这样的判断,保证在一个终端只运行一个winform的client。并且如果最小化了,用户再次双击桌面图标的时候,将client置顶显示。

解决方案

需要使用windows的API,可以很方便的实现这个目的。

代码如下:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class AppMain
{
    [DllImport("user32.dll")] private static extern 
        bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")] private static extern 
        bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] private static extern 
        bool IsIconic(IntPtr hWnd);

    private const int SW_HIDE = 0;
    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;
    private const int SW_SHOWNOACTIVATE = 4;
    private const int SW_RESTORE = 9;
    private const int SW_SHOWDEFAULT = 10;

    static void Main()
    {
        // get the name of our process
        string proc=Process.GetCurrentProcess().ProcessName;
        // get the list of all processes by that name
        Process[] processes=Process.GetProcessesByName(proc);
        // if there is more than one process...
        if (processes.Length > 1)
        {
            // Assume there is our process, which we will terminate, 
            // and the other process, which we want to bring to the 
            // foreground. This assumes there are only two processes 
            // in the processes array, and we need to find out which 
            // one is NOT us.

            // get our process
            Process p=Process.GetCurrentProcess();
            int n=0;        // assume the other process is at index 0
            // if this process id is OUR process ID...
            if (processes[0].Id==p.Id)
            {
                // then the other process is at index 1
                n=1;
            }
            // get the window handle
            IntPtr hWnd=processes[n].MainWindowHandle;
            // if iconic, we need to restore the window
            if (IsIconic(hWnd))
            {
                ShowWindowAsync(hWnd, SW_RESTORE);
            }
            // bring it to the foreground
            SetForegroundWindow(hWnd);
            // exit our process
            return;
        }
        // ... continue with your application initialization here.
    }
}

上面代码的意思是判断exe是否已经在进程列表中,如果存在则标识已经运行了客户端,如果存在获取窗口的句柄,并进行展示。

资料来源

https://www.codeproject.com/Articles/2976/Detect-if-another-process-is-running-and-bring-it


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏化革命:未来商业模式的驱动力

游戏化革命:未来商业模式的驱动力

[美]盖布·兹彻曼、[美]乔斯琳·林德 / 应皓 / 中国人民大学出版社有限公司 / 2014-8-1 / CNY 59.00

第一本植入游戏化理念、实现APP互动的游戏化商业图书 游戏化与商业的大融合、游戏化驱动未来商业革命的权威之作 作者被公认为“游戏界的天才”,具有很高的知名度 亚马逊五星级图书 本书观点新颖,游戏化正成为最热门的商业新策略 游戏化是当今最热门的商业新策略,它能帮助龙头企业创造出前所未有的客户和员工的参与度。商业游戏化策略通过利用从游戏设计、忠诚度计划和行为经济学中所汲取......一起来看看 《游戏化革命:未来商业模式的驱动力》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器