2010-05-24 97 views
2

有人可以告诉我为什么我的WndProc方法如下实现没有收到任何消息?如果我把这个类放在一个WinForms应用程序中,并通过应用程序的句柄,WndProc接收消息,如我所料。但是,如下所示,使用从GetForegroundWindow()返回的IntPtr不会产生相同的结果。 (FWIW,当我的应用程序隐藏时,我的代码已设置为执行GetForegroundWindow(),所以我确定IntPtr指的是外部应用程序。)我的目标是监视来自外部应用程序的某些事件。NativeWindow WndProc没有收到消息

public class MyNativeWindow : NativeWindow 
{ 
    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] 
    private static extern IntPtr GetForegroundWindow(); 

    public MyNativeWindow() 
    { 
     this.AssignHandle(GetForegroundWindow()); 
    } 

    // Never called... I set a breakpoint 
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    protected override void WndProc(ref Message m) 
    { 
     base.WndProc(ref m); 
    } 
} 

回答

2

您将不会收到其他进程的消息。

+0

对我如何接收其他进程的消息有任何建议吗? – 2010-05-24 05:24:11

+1

我能想到的唯一方法是在目标进程中注入一个dll。搜索“dll injection” – deerchao 2010-05-24 05:27:22

+0

声音混乱。 ;)我会检查出来...... – 2010-05-24 05:31:13