C# 给指定应用程序发送消息

来源:互联网 发布:帝国cms内容页js调用 编辑:程序博客网 时间:2024/05/16 19:22
[csharp] view plaincopy
  1. public partial class FrmMain : Form  
  2. {  
  3.     public FrmMain()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.     private void button1_Click(object sender, EventArgs e)  
  9.     {  
  10.   
  11.         try  
  12.         {  
  13.             var procecess = Process.GetProcessesByName("SSDS");  
  14.             if (procecess.Length <= 0)  
  15.             {  
  16.                 procecess = Process.GetProcessesByName("SSDS.vshost");  
  17.             }  
  18.   
  19.             if (procecess.Length <= 0)  
  20.             {  
  21.                 MessageBox.Show("请打开程序SSDS");  
  22.                 return;  
  23.             }  
  24.   
  25.             var msgId = Convert.ToInt32(textBox1.Text);  
  26.   
  27.             var ssdProcess = procecess[0];  
  28.             //发消息  
  29.             PostMessage(ssdProcess.MainWindowHandle, (uint)msgId, IntPtr.Zero, IntPtr.Zero);  
  30.   
  31.             //等待退出  
  32.             ssdProcess.WaitForExit();  
  33.   
  34.             //读数据  
  35.             MessageBox.Show(File.ReadAllText("data.txt"));  
  36.   
  37.         }  
  38.         catch (Exception ex)  
  39.         {  
  40.               
  41.             MessageBox.Show(ex.Message);  
  42.         }  
  43.        
  44.   
  45.     }  
  46.     [DllImport("User32.dll")]  
  47.     static extern IntPtr PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);  
  48. }  
0 0
原创粉丝点击