C#监听窗体新建/鼠标移入移出

在新建window窗体时会激活方法,并循环所有窗体,鼠标移动在重写方法的页面中也会激活

winform直接在继承了From窗体cs中

protected override void WndProc(ref Message m)
{
    if (m.Msg == 23)// m.Msg是窗体消息号
    {
    }
}

 wpf在继承WIndows窗体cs中

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}

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

    return IntPtr.Zero;
}

  

posted @ 2018-08-22 18:58  xiao贝  阅读(1445)  评论(0编辑  收藏  举报