【问题标题】:WPF: Non focusable windowWPF:不可聚焦的窗口
【发布时间】:2010-04-19 19:58:38
【问题描述】:

我正在开发 WPF 触摸屏键盘。

我需要知道如何使主窗口无法聚焦,因此当我单击虚拟键盘按钮时其他窗口将接收输入。

简单地将“Focusable="False"”应用于主窗口,所有子控件都不起作用。

【问题讨论】:

    标签: wpf focus


    【解决方案1】:

    我认为有一个 clickable 属性可以设置为 false,它会阻止表单接收点击消息。

    【讨论】:

      【解决方案2】:

      问题已通过使用 Popup 而不是 Window 解决,当您单击它时,它不会抓住焦点。

      【讨论】:

        【解决方案3】:

        从这里:https://social.msdn.microsoft.com/Forums/vstudio/en-US/41ca3605-247c-4c5b-ac5d-74ce5abd7b92/making-a-window-invisible-to-mouse-events-ishittestvisiblefalse-not-working?forum=wpf

        我已经想出了如何做到这一点。关键是窗口扩展样式的 WS_EX_TRANSPARENT 标志。您可以像往常一样设置最顶层的属性,然后此代码负责使窗口对鼠标单击透明:

        代码片段

        public const int WS_EX_TRANSPARENT = 0x00000020;
        public const int GWL_EXSTYLE = (-20);
        
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hwnd, int index);
        
        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
        
        protected override void OnSourceInitialized(EventArgs e)
        {
        base.OnSourceInitialized(e);
        
        // Get this window's handle
        IntPtr hwnd = new WindowInteropHelper(this).Handle;
        
        // Change the extended window style to include WS_EX_TRANSPARENT
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
        }
        

        【讨论】:

        • 如果你想要一个不可聚焦的窗口,你似乎想设置WS_EX_NOACTIVATE = 0x08000000;(没有运气使用WS_EX_TRANSPARENT),至少在Windows 10上工作。
        猜你喜欢
        • 2011-06-05
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多