从另一个WPF应用程序向WPF应用程序发送窗口消息

时间:2016-12-14 12:30:36

标签: c# .net wpf wndproc hwndsource

我在服务器端使用了这段代码

    void Window_Loaded(object sender, RoutedEventArgs e)
    {

        HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
        source.AddHook(new HwndSourceHook(WndProc));

    }
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        // Handle messages...

            var htLocation = DefWindowProc(hwnd, msg, wParam, lParam).ToInt32();

            if (msg == 1)
            {
            MessageBox.Show("" + msg);
            }


        return new IntPtr(1);
    }

我从客户端发送消息就像这样

SendMessage(m_Process.MainWindowHandle, 1, (IntPtr)(-1), (IntPtr)(-1));

问题是服务器端无法收到此消息,为什么?

1 个答案:

答案 0 :(得分:1)

我发现了错误

我发送的消息ID必须是0x0112而不是1 这是用于Windows命令