惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
V
V2EX
美团技术团队
H
Help Net Security
月光博客
月光博客
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
U
Unit 42
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - Franky
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
MyScale Blog
MyScale Blog
B
Blog
雷峰网
雷峰网
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss

博客园 - 侯垒

中转站余额为什么掉得快?我拆了一次 AI 编程任务的真实消耗 我让 Claude 写了一个贪吃蛇游戏,然后用 ccglass 看清它发给模型的真实请求 AI 编程 Agent 不是黑箱了:用 ccglass 看清 Claude Code 和 Codex 的真实请求 软件工程--软件过程模型 UML 类图 WebService基于SoapHeader实现安全认证 (转)深入理解Javascript闭包(closure) 数据库中使用自增量字段与Guid字段作主键的性能对比(补充篇) SQL Server 查询处理中的各个阶段 敏捷软件开发--敏捷宣言 asp.net抛出System.Data.OleDb.OleDbException:未指定的错误 架构师 通用的数据库操作类(支持多种数据库) 数据库中使用自增量字段与Guid字段作主键的性能对比 将GridView中的数据导出到Excel中下载并且解决乱码的问题 设计模式----建造者模式(Builder Pattern) 大学生联盟开发团队需要我们共同的努力 - 侯垒 - 博客园 SQL 数据库操作类 如何将自己的代码自动添加版权信息的及其扩展 如何将自己的代码自动添加版权信息 轻松学习适配器模式(Adapter Pattern) 设计模式-----桥接模式(Bridge Pattern)
只启动一个窗体,如果再次启动则激活该窗体
侯垒 · 2008-04-15 · via 博客园 - 侯垒

只启动一个窗体,如果再次启动则激活该窗体

     以前就很想实现这个功能,不过不知从何处下手,初步做了一下只实现了只启动一个窗体,却无法实现激活该窗体,这是一个很大的遗憾呀!也是唯一美中不足之处.在网上找了好久,终于实现了这个功能,在这和大家一起分享一下.

 1using System;
 2using System.Collections.Generic;
 3using System.Windows.Forms;
 4using System.Diagnostics;
 5using System.Runtime.InteropServices;
 6
 7namespace WindowsApplication2
 8{
 9    static class Program
10    {
11        /// <summary>
12        /// 应用程序的主入口点。
13        /// </summary>

14        
15        [DllImport("User32.dll")]
16        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
17        [DllImport("User32.dll")]
18        private static extern bool SetForegroundWindow(IntPtr hWnd);
19        private const int WS_SHOWNORMAL = 1;
20
21        [STAThread]
22        static void Main()
23        {
24            //得到正在运行的例程
25            Process instance = RunningInstance();
26            if (instance == null)
27            {
28                //如果没有其它例程,就新建一个窗体
29                Application.EnableVisualStyles();
30                Application.SetCompatibleTextRenderingDefault(false);
31                Application.Run(new Form1());
32            }

33            else
34            {
35                //处理发现的例程
36                HandleRunningInstance(instance);
37            }

38        }

39        /// <summary>
40        /// 得到正在运行的进程
41        /// </summary>
42        /// <returns></returns>

43        public static Process RunningInstance()
44        {
45            Process current = Process.GetCurrentProcess();
46            Process[] processes = Process.GetProcessesByName(current.ProcessName);
47
48            //遍历正在有相同名字运行的进程
49            foreach (Process process in processes)
50            {
51                //忽略现有的进程
52                if (process.Id != current.Id)
53                {
54                    //确保进程从EXE文件运行
55                    if (process.MainModule.FileName == current.MainModule.FileName)
56                    {
57                        // 返回另一个进程实例
58                        return process;
59                    }

60                }

61            }

62            //没有其它的进程,返回Null
63            return null;
64        }

65        /// <summary>
66        /// 处理正在运行的进程,也就是将其激活
67        /// </summary>
68        /// <param name="instance">要处理的进程</param>

69        public static void HandleRunningInstance(Process instance)
70        {
71            //确保窗口没有被最小化或最大化
72            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
73            //设置真实进程为foreground window
74            SetForegroundWindow(instance.MainWindowHandle);
75        }

76    }

77}