[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


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

查看所有标签

猜你喜欢:

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

从算法到程序

从算法到程序

徐子珊 / 2013-3 / 59.00元

《从算法到程序:从应用问题编程实践全面体验算法理论》第1章讨论算法设计、分析的基本概念,第2章讨论算法设计中最常用的几个数据结构,包括链表、栈、队列、二叉搜索数、散列表等。第3章讨论了算法设计的两个基本策略:渐增策略与分支策略。这3章的内容,为读者阅读本书以后的内容奠定了基础。第4章讨论了几个代数计算的基本问题及其算法,包括矩阵运算、解线性方程组、多项式运算等。第5章讨论了几个关于计算几何的基本问......一起来看看 《从算法到程序》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

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

Markdown 在线编辑器