C# 给指定应用程序发送Windows Message

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