5

WPF 在窗口的 Deactivated 使用 Mouse 的 Capture 将会让进程失去交互

 3 years ago
source link: https://lindexi.gitee.io/post/WPF-%E5%9C%A8%E7%AA%97%E5%8F%A3%E7%9A%84-Deactivated-%E4%BD%BF%E7%94%A8-Mouse-%E7%9A%84-Capture-%E5%B0%86%E4%BC%9A%E8%AE%A9%E8%BF%9B%E7%A8%8B%E5%A4%B1%E5%8E%BB%E4%BA%A4%E4%BA%92.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
WPF 在窗口的 Deactivated 使用 Mouse 的 Capture 将会让进程失去交互

如果在某个窗口的 Deactivated 事件里面,使用 Mouse.Capture 方法,让这个窗口重新捕获鼠标,那么将会让进程的所有窗口都失去鼠标交互,点击无效,只有在切换到其他进程的窗口之后,才能让窗口继续交互

实现这个坑的逻辑很简单,假定有两个窗口,分别是 MainWindow 和 Window1 两个窗口,在 Window1 的 Deactivated 事件里面,使用 Mouse.Capture 方法,让这个窗口重新捕获鼠标

        public Window1()
        {
            InitializeComponent();

            Deactivated += Window1_Deactivated;
        }

        private void Window1_Deactivated(object sender, EventArgs e)
        {
            Mouse.Capture(this);
        }

先点击 Window1 激活,然后点击 MainWindow 的空白,切换到 MainWindow 窗口。接着点击任何的窗口或窗口的按钮等,都没有响应

在 WPF 发现鼠标点击失效,或者触摸失效等时,可以全局搜一下 Mouse.Capture 方法,看是否在窗口的 Deactivated 事件里面调用

本来还想聊聊为什么这样做就会失去鼠标焦点的,然而 10 点多了


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/WPF-%E5%9C%A8%E7%AA%97%E5%8F%A3%E7%9A%84-Deactivated-%E4%BD%BF%E7%94%A8-Mouse-%E7%9A%84-Capture-%E5%B0%86%E4%BC%9A%E8%AE%A9%E8%BF%9B%E7%A8%8B%E5%A4%B1%E5%8E%BB%E4%BA%A4%E4%BA%92.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK