无法在c#.net中接收窗口消息

时间:2017-01-18 07:19:06

标签: c# .net window

我想将消息发送到已经运行的其他进程。怎么了......?为什么我无法接收消息 我的发件人代码如下

public partial class RegisterWindowMessage : Form
    {
        [DllImport("User32.dll", EntryPoint = "SendMessage")]

        //private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        private static extern int SendMessage(IntPtr hWnd, int Msg, string s, int i);

        const int WM_SETTEXT = 0X000C;
        public RegisterWindowMessage()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Process[] procs = Process.GetProcesses();
            foreach (Process p in procs)
            {
                if (p.ProcessName.Equals("TaskScheduling"))
                {

                    IntPtr hWnd = p.MainWindowHandle;

                    Thread.Sleep(1000);
                    SendMessage(hWnd, WM_SETTEXT, "This is the new Text!!!", 0);

                    MessageBox.Show("Inside");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

    }

我的收件人代码是

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

        }
        private void frmReceiver_KeyDown(object sender, KeyEventArgs e)
        {
          //  this.lsvMsgList.Items.Add(e.KeyValue.ToString());
        }


        protected override void WndProc(ref Message m)
        {
            MessageBox.Show(m.Msg.ToString());
            MessageBox.Show(m.LParam.ToString());
            MessageBox.Show(m.WParam.ToString());
            if (m.LParam.ToInt32() == 1)
            {

            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }

我想知道为什么我无法收到消息。让我知道我错在哪里

0 个答案:

没有答案