刚才找到了只打开一个winform窗口的代码,
不知道如何实现点击这个最小化窗口.exe执行文件时激活这个窗口。            if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                //这里怎么写才能能激活这个窗口呢???????????
            }
            else
            {
                Application.Run(new Form1());
            }   
不知如何写,请教前辈们,谢谢!

解决方案 »

  1.   

    好像是用这个Activate()方法
    具体的用方法忘记了
      

  2.   

    参考:
    http://www.lordong.cn/blog/post/17.html
      

  3.   

    4楼的方法
    C#单实例运行实现
    在某些情况我们要求应用程序只能运行一次,后运行的实例要把之前运行的程序激活并自己退出。
    在网上搜索了一些实现并根据自己的理解重新整理得出下面的实现代码:1. API函数的声明
    // Uses to active the exist window
    [DllImport("User32.dll")]
    public static extern void SetForegroundWindow(IntPtr hwnd);[DllImport("User32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);// 0-Hidden, 1-Centered, 2-Minimized, 3-Maximized
    private const int WS_SHOWNORMAL = 3;2. 查找并激活已经存在的程序(仅应用于进程名相同的程序)
    ///
    /// Finds the running instance.
    ///
    /// true if exist a running instace, otherwise false
    private static bool ExistRunningInstance()
    {
    Process currentProcess = Process.GetCurrentProcess();
    Process[] procList = Process.GetProcessesByName(currentProcess.ProcessName);foreach (Process proc in procList)
    {
    // Found a running instance
    if (proc.Id != currentProcess.Id)
    {
    // Active the running instance
    ShowWindowAsync(proc.MainWindowHandle, WS_SHOWNORMAL);
    SetForegroundWindow(proc.MainWindowHandle);return true;
    }
    }return false;
    }3. 在Main函数入口判断
    [STAThread]
    static void Main()
    {
    // Control only one instance with the same process name can run
    if (ExistRunningInstance())
    {
    Environment.Exit(1); // App is running, exit
    }// 程序真正运行的代码
    }
      

  4.   

    DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
    private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
    [DllImport ( "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true )]
    private static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );[DllImport ( "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true )]
    private static extern void SetForegroundWindow( IntPtr hwnd );
    IntPtr hwndC = FindWindow ( null, "" ); 
    if ( hwndCalc != IntPtr.Zero )
    {
    SetForegroundWindow ( hwndC );    
    System.Threading.Thread.Sleep ( 2000 );   
    }[DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
            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()
            {
                bool createdNew = true;
                using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
                {
                    if (createdNew)
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new FrmMain());
                    }
                    else
                    {
                        Process current = Process.GetCurrentProcess();
                        foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                        {
                            if (process.Id != current.Id)
                            {
                                SetForegroundWindow(process.MainWindowHandle);
                                ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
                                break;
                            }
                        }
                    }
                }
            }