2010-05-24 99 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