您的位置:首页 > 其它

程序进入托盘,运行Exe打开窗口

2012-02-14 18:50 363 查看
程序进入托盘,运行Exe打开窗口,打开原先程序的窗口(未测试)

static class Program

{

private static string strProcessName = "Form1" ;

private static string strAppName = "WindowsFormsApplication1";

static int hWnd = 0;

const int SW_SHOW = 5;

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

public static extern int ShowWindow(int hwnd, int nCmdShow);

[DllImport("user32")]

public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32")]

public static extern int EnumWindows(CallBack x, int y);

public delegate bool CallBack(int hWnd, int lParam);

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

Process[] ps = Process.GetProcessesByName(strAppName);

CallBack myCallBack = new CallBack(FineAppWindow);

EnumWindows(myCallBack, 0);

if (ps.Length > 1) { ShowWindow(hWnd, SW_SHOW); return;

}

else

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

public static bool FineAppWindow(int hwnd, int lParam)

{

StringBuilder sb = new StringBuilder(200);

int n = GetWindowText(hwnd, sb, 200);

if (sb.ToString() == strProcessName)

{

hWnd = hwnd;

}

return true;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐