1

RegisterWindowMessage("SHELLHOOK") を使用して、ウィンドウの作成イベントと破棄イベントを登録します。私のコードは C# で書かれています。コードをデバッグしているとき、コードは完全に機能します。しかし、デバッグせずにプログラムを実行すると、デバッグ中に以前に取得したような WndProc メッセージが表示されません。Windows はフックをブロックしますか?

class ShellHook:NativeWindow
{
    public ShellHook(IntPtr hWnd)
    {
        if (Win32.RegisterShellHookWindow(this.Handle))
        {
            WM_ShellHook    = Win32.RegisterWindowMessage("SHELLHOOK");
        }
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_ShellHook)
        {
            switch((ShellEvents)m.WParam)
            {
                    //m.lparam
                case ShellEvents.HSHELL_WINDOWCREATED:
                    if (windowCreatedEvent != null)
                    {
                        windowCreatedEvent(m.LParam);
                    }
                    break;

                case ShellEvents.HSHELL_WINDOWDESTROYED:
                    if (windowDestroyedEvent != null)
                    {
                        windowDestroyedEvent(m.LParam);
                    }
                    break;
            }
        }
        base.WndProc(ref m);
    }
}

これが私のwpfアプリの起動方法です。

 public partial class App : Application
 {
    MainWindow mainWindowView;

    public App()
    {
        Startup += new StartupEventHandler(App_Startup);
    }

    void App_Startup(object sender, StartupEventArgs e)
    {
        mainWindowView = new MainWindow();
        MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
        mainWindowView.DataContext = mainWindowViewModel;
        mainWindowView.ShowDialog();
    }
 }

私の MainWindowViewModel コンストラクターは次のとおりです。

 public MainWindowViewModel()
    {
        EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
        System.Windows.Forms.Form f = new System.Windows.Forms.Form();
        ShellHook shellHookObject = new ShellHook(f.Handle);
        shellHookObject.windowCreatedEvent += shellHookObject_windowCreatedEvent;
        shellHookObject.windowDestroyedEvent += shellHookObject_windowDestroyedEvent;
    }
4

1 に答える 1