Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在c#.net中接收窗口消息_C#_.net_Window - Fatal编程技术网

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

无法在c#.net中接收窗口消息,c#,.net,window,C#,.net,Window,我想向已在运行的其他进程发送消息。怎么了。。。?为什么我不能接收消息 我的发件人代码如下 public partial class RegisterWindowMessage : Form { [DllImport("User32.dll", EntryPoint = "SendMessage")] //private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, I

我想向已在运行的其他进程发送消息。怎么了。。。?为什么我不能接收消息 我的发件人代码如下

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);
            }
        }
    }

我想知道为什么我不能接收消息。让我知道我错在哪里

看起来
Form1
不是
MainWindowHandle
。您可以尝试
EnumWindows
枚举所有窗口,或注册新消息并使用广播。能否提供示例代码