c#单一程序运行续-最小化后显示

这段代码展示了如何在C#中检查并处理已最小化的程序。通过`FindWindow`和`ShowWindow`函数,当用户再次点击程序时,会最大化已存在的窗口,避免创建新的实例。
摘要由CSDN通过智能技术生成

程序最小化后,再点击程序,程序最大化。


 static class Program
    {
        [DllImport("user32.dll ", CharSet = CharSet.Unicode)]


        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);


        [DllImport("User32.dll ", EntryPoint = "FindWindow")]


        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//关键方法


        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]


        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);//关键方法
        

        /// <summary>

        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());


            //判断之前是否已经打开了相同的进程 
            //new   Bestzone.MIS.Report.F1BookContainer();
            IntPtr handle;
            IntPtr ishandle = new IntPtr(0);


            handle = FindWindow(null, "集中控制系统");
            if (handle == ishandle)
            {
                handle = FindWindow(null, "集中控制系统-车身控制");
            }
            if (handle == ishandle)
            {
                handle = FindWindow(null, "集中控制系统-配电监测");
            }
            if (handle == ishandle)
            {
                handle = FindWindow(null, "集中控制系统-环境监控");
            }
            if (handle == ishandle)
            {
                handle = FindWindow(null, "集中控制系统-视频监测");
            }


            bool alreadyExist = false;
            try
            {
                System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
                System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in ps)
                {
                    if (p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
                    {
                        alreadyExist = true;
                    }
                }
            }
            catch (System.PlatformNotSupportedException ex) { ex.ToString(); }
            catch (System.InvalidOperationException ex) { ex.ToString(); }


            //如果已经存在,则放弃本进程 
            if (alreadyExist)
            {
                Messag

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值