代码改变世界

Winform/WPF实例中的相互操作

2011-03-22 12:57  观海看云  阅读(784)  评论(0编辑  收藏  举报

介绍一下如何在实例中相互操作,比如在程序A中调用程序B中的方法,看起来不太可能,不过我们可以利用Win32API来实现。我们需要用到2个Win32函数:

uint RegisterWindowsMessage(string lpString);
注册一个新的窗口消息,该消息确保在系统中是唯一的,返回消息值,可在下面SendMessage函数中调用,如果该消息已经存在,则返回它的消息值

IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
向指定的窗口发送消息

IntPtr FindWindow(string IpClassName,string IpWindowName);
查找与指定类名或窗口名相同的顶层窗口句柄

IntPtr WindowProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam, ref bool handled);
处理发送给窗口的消息

有了这两个程序就可以实现了,比如说我们在程序B中通过RegisterWindowsMessage函数定义一个字符串为“TestInvoke”的系统消息,再在程序A中,通过RegisterWindowMessage函数获得“TestInvoke”的消息值,再通过FindWindow获得程序B的句柄,通过SendMessage发送给窗口B,窗口B再通过WindowProc处理这个消息,调用相关函数。这个过程看似很复杂,只要我们多加思考,还是很容易理解的。

WindowApp1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int handler = FindWindow(null, "WindowApp2");   //查找窗口句柄
        int testInvokeMsg = RegisterWindowMessage("TestInvoke");    //获得TestInvoke消息值
        SendMessage((IntPtr)handler, (uint)testInvokeMsg, IntPtr.Zero, IntPtr.Zero);    //发送消息
    }
    [DllImport("user32.dll")]
    public static extern int RegisterWindowMessage(string msgName);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
}

WindowApp2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public partial class MainWindow : Window
{
    int testInvokeMsg = 0;
    public MainWindow()
    {
        InitializeComponent();
        testInvokeMsg = RegisterWindowMessage("TestInvoke");    //获得TestInvoke消息值
        this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);
    }
    void MainWindow_SourceInitialized(object sender, EventArgs e)
    {
        HwndSource hd = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        hd.AddHook(WindowProc); //添加消息处理函数
    }
    [DllImport("user32.dll")]
    public static extern int RegisterWindowMessage(string msgName);
    public IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == testInvokeMsg)
        {
            //TestInvoke处理
            MessageBox.Show("TestInvoke");
        }
        return IntPtr.Zero;
    }
}

代码下载