WinFrom 只启动一个exe,并且获得焦点

只启动一个exe方法:
WinFrom 只启动一个exe,并且获得焦点
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Runtime.InteropServices;
 4 using System.Windows.Forms;
 5 
 6 namespace StringToImage
 7 {
 8     static class Program
 9     {
10         [DllImport("user32.dll")]
11         public static extern IntPtr FindWindow(String classname, String title);
12         [DllImport("user32.dll")]
13         public static extern void SetForegroundWindow(IntPtr hwnd);
14 
15         [STAThread]
16         static void Main()
17         {
18             Application.EnableVisualStyles();
19             Application.SetCompatibleTextRenderingDefault(false);
20 
21             string p = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
22             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(p);
23             if (processes.Length < 2)
24                 Application.Run(new Form1());
25             else
26             {
27                 //MessageBox.Show("程序已经运行!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
28                 IntPtr ptr = FindWindow(null, "文字转换成图片");//"文字转换成图片"为对应窗体的Text值
29                 if (ptr.ToString() != "0")//不等于0表示找到了窗体
30                     SetForegroundWindow(ptr);
31 
32                 Application.Exit();
33             }
34         }
35     }
36 }
View Code

 

WinFrom 只启动一个exe,并且获得焦点

上一篇:剑指offer28 对称的二叉树


下一篇:C# DllImport的用法