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

来源:互联网 发布:杀插屏广告软件 编辑:程序博客网 时间:2024/06/06 15:39
using System;using System.Diagnostics;using System.IO;using System.Runtime.InteropServices;using System.Windows.Forms;namespace 发消息{    public partial class FrmMain : Form    {        public FrmMain()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            try            {                var procecess = Process.GetProcessesByName("SSDS");                if (procecess.Length <= 0)                {                    procecess = Process.GetProcessesByName("SSDS.vshost");                }                if (procecess.Length <= 0)                {                    MessageBox.Show("请打开程序SSDS");                    return;                }                var msgId = Convert.ToInt32(textBox1.Text);                var ssdProcess = procecess[0];                //发消息                PostMessage(ssdProcess.MainWindowHandle, (uint)msgId, IntPtr.Zero, IntPtr.Zero);                //等待退出                ssdProcess.WaitForExit();                //读数据                MessageBox.Show(File.ReadAllText("data.txt"));            }            catch (Exception ex)            {                                MessageBox.Show(ex.Message);            }                 }        [DllImport("User32.dll")]        static extern IntPtr PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);        private void FrmMain_Load(object sender, EventArgs e)        {        }    }}


 

原创粉丝点击