Example #1
0
 /// <summary>
 /// 加载外部应用程序
 /// </summary>
 /// <param name="pProcessName">程序进程名称</param>
 /// <param name="pMainWindowName">主窗体名称</param>
 /// <param name="pAppPath">应用程序路径</param>
 /// <param name="pArgs">参数(多个用空格隔开)</param>
 public static bool LoadApplication(string pProcessName, string pAppPath, string pArgs, out string pError)
 {
     pError = "";
     try
     {
         System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
         bool   isRunning = false;
         IntPtr iHandle   = new IntPtr();
         foreach (System.Diagnostics.Process myProcess in myProcesses)
         {
             if ((myProcess.ProcessName == pProcessName))
             {
                 isRunning = true;
                 iHandle   = myProcess.MainWindowHandle;
                 break;
             }
         }
         if (isRunning)
         {
             WndProcMsgAPI.PostMessage(iHandle, 1157, 1, 1);
             return(true);
         }
         else
         {
             if (pAppPath != null && File.Exists(pAppPath))
             {
                 Process          myprocess = new Process();
                 ProcessStartInfo startInfo = new ProcessStartInfo(pAppPath, pArgs);
                 startInfo.WindowStyle = ProcessWindowStyle.Normal;
                 myprocess.StartInfo   = startInfo;
                 myprocess.StartInfo.UseShellExecute = false;
                 myprocess.Start();
                 return(true);
             }
             else
             {
                 pError = "程序路径错";
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         pError = ex.Message;
         return(false);
     }
 }
Example #2
0
 public static bool SendMessage(string ProcessName, int msg, string WindowName, string ClassName, out string Error)
 {
     Error = "";
     try
     {
         if (ISProgramRunning(ProcessName))
         {
             IntPtr iHandle = WndProcMsgAPI.FindWindow(ClassName, WindowName);
             WndProcMsgAPI.PostMessage(iHandle, msg, 1, 1);
             return(true);
         }
         else
         {
             Error = "程序未启动";
             return(false);
         }
     }
     catch (Exception ex)
     {
         Error = ex.Message;
         throw;
     }
 }