Winform中引入WPF控件后键盘输入无响应

引言

Winform中如何引入WPF控件的教程很多,对于我们直接通过ElementHost引入的直接显示控件,它是可以响应键盘输入消息的,但对于在WFP中弹出的窗体来说,此时是无法响应我们的键盘输入的。我们需要给它使能键盘输入。

1、使能键盘输入消息

简单来说就只有下面一句代码:

System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);

其中window是WPF窗体。

但我们在原有的WPF控件库中引入Winform的代码好像不太合适,这两个是完全不同的界面框架。

2、通用方法

通常我们的弹窗需要时单例模式,我这里采用一个静态类管理所有的单例。通过它的Add方法添加并返回WPF窗体。

    /// <summary>
    /// 创建单例窗体
    /// </summary>
    public class SingleInstance
    {
        static Hashtable s_typeList = new Hashtable();

        /// <summary>
        /// 全局唯一窗口单例,要求无参构造
        /// 懒加载模式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="W"></typeparam>
        /// <returns></returns>
        public static T Create<T, W>()
            where T : Lazy<W>, new()
            where W : Window, new()
        {
            if (s_typeList.ContainsKey(typeof(T)))
            {
                if (s_typeList[typeof(T)] is not null)
                    return (T)s_typeList[typeof(T)];
                else
                {
                    T t = new T();

                    s_typeList[typeof(T)] = t;
                    t.Value.Closed += (s, e) => s_typeList[typeof(T)] = null;
#if NETFRAMEWORK
                    WinformAction?.Invoke(t.Value);
#endif
                    return t;
                }
            }
            else
            {
                T t = new T();

                s_typeList.Add(typeof(T), t);
                t.Value.Closed += (s, e) => s_typeList[typeof(T)] = null;
#if NETFRAMEWORK
                WinformAction?.Invoke(t.Value);
#endif
                return t;
            }
        }

        /// <summary>
        /// 单例窗体容器 不要求无参构造
        /// 存在且不为空则返回现有实例,否则添加到容器并返回当前实例
        /// </summary>
        public static T Add<T>(T window, string key)
            where T : Window
        {
            if (s_typeList.ContainsKey(key))
            {
                if (s_typeList[key] is not null)
                    return (T)s_typeList[key];
                else
                {
                    s_typeList[key] = window;
                    window.Closed += (s, e) => s_typeList[key] = null;
#if NETFRAMEWORK
                    WinformAction?.Invoke(window);
#endif
                    return window;
                }
            }
            else
            {
                s_typeList.Add(key, window);
                window.Closed += (s, e) => s_typeList[key] = null;
#if NETFRAMEWORK
                WinformAction?.Invoke(window);
#endif
                return window;
            }
        }

#if NETFRAMEWORK
        /// <summary>
        /// 帮助在初始化窗体是执行一些操作,比如注册键盘输入
        /// </summary>
        public static Action<Window> WinformAction;
#endif
    }

这里使用了前置处理器指示词C# 前置處理器指示詞 | Microsoft Learn

通过给WinformAction事件赋值达到自动注册目的

SingleInstance.SingleInstance.WinformAction = window =>
{                                         
    System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James.TCG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值