zoukankan      html  css  js  c++  java
  • 一个程序只能启动一次实现

    原文网址: http://free56.cn/post/3.html
    using System;
    using System.Runtime.InteropServices;

    namespace JKLib
    {
        
        
    /// <summary>
        
    /// 一个程序只能启动一次实现
        
    /// </summary>

        public class SingleInstance
        
    {
            [DllImport(
    "user32.dll")] 
            
    private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
            [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);

            
    /// <summary>
            
    /// 显示窗体命令值 
            
    /// </summary>

            private const int SW_RESTORE = 9;

            
    /// <summary>
            
    /// 窗体名称
            
    /// </summary>

            private string WinTitle;

            
    /// <summary>
            
    /// 一个程序只能启动一次实现
            
    /// </summary>
            
    /// <param name="_WinTitle">程序名</param>

            public SingleInstance(string _WinTitle)
            
    {
                WinTitle 
    = _WinTitle;
            }


            
    private IntPtr hWnd = (System.IntPtr)null;

            
    /// <summary>
            
    /// 是否只有一个窗口
            
    /// </summary>

            public bool IsSingleInstance
            
    {
                
    get
                
    {
                    hWnd 
    = FindWindow(null,WinTitle);
                    
    return hWnd == (System.IntPtr)null;
                }

            }

            
    /// <summary>
            
    /// 使当前程序进程处于活动状态
            
    /// </summary>

            public void RaiseOtherProcess()
            
    {
                
    if(hWnd == (System.IntPtr)nullreturn;
                
    else
                
    {
                    
    if (IsIconic(hWnd))
                    
    {
                        ShowWindowAsync(hWnd,SW_RESTORE);
                    }

                    SetForegroundWindow(hWnd);
                    
    return;
                }

            }

        }

    }


    原文网址:  http://free56.cn/post/3.html 
  • 相关阅读:
    2020/10/10周总结
    2020/10/02周总结
    2020/9/28周总结
    第十二周总结
    第十一周总结
    人月神话阅读笔记03
    人月神话阅读笔记02
    人月神话阅读笔记01
    冲刺一8
    冲刺一7
  • 原文地址:https://www.cnblogs.com/skywind/p/470682.html
Copyright © 2011-2022 走看看