益阳网站建设网站,wordpress个人中心页,腾讯搜索引擎入口,网站改版提升总结引言
Winform中如何引入WPF控件的教程很多#xff0c;对于我们直接通过ElementHost引入的直接显示控件#xff0c;它是可以响应键盘输入消息的#xff0c;但对于在WFP中弹出的窗体来说#xff0c;此时是无法响应我们的键盘输入的。我们需要给它使能键盘输入。
1、使能键盘…引言
Winform中如何引入WPF控件的教程很多对于我们直接通过ElementHost引入的直接显示控件它是可以响应键盘输入消息的但对于在WFP中弹出的窗体来说此时是无法响应我们的键盘输入的。我们需要给它使能键盘输入。
1、使能键盘输入消息
简单来说就只有下面一句代码
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);
其中window是WPF窗体。
但我们在原有的WPF控件库中引入Winform的代码好像不太合适这两个是完全不同的界面框架。
2、通用方法
通常我们的弹窗需要时单例模式我这里采用一个静态类管理所有的单例。通过它的Add方法添加并返回WPF窗体。 /// summary/// 创建单例窗体/// /summarypublic class SingleInstance{static Hashtable s_typeList new Hashtable();/// summary/// 全局唯一窗口单例要求无参构造/// 懒加载模式/// /summary/// typeparam nameT/typeparam/// typeparam nameW/typeparam/// returns/returnspublic static T CreateT, W()where T : LazyW, 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 NETFRAMEWORKWinformAction?.Invoke(t.Value);
#endifreturn t;}}else{T t new T();s_typeList.Add(typeof(T), t);t.Value.Closed (s, e) s_typeList[typeof(T)] null;
#if NETFRAMEWORKWinformAction?.Invoke(t.Value);
#endifreturn t;}}/// summary/// 单例窗体容器 不要求无参构造/// 存在且不为空则返回现有实例否则添加到容器并返回当前实例/// /summarypublic static T AddT(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 NETFRAMEWORKWinformAction?.Invoke(window);
#endifreturn window;}}else{s_typeList.Add(key, window);window.Closed (s, e) s_typeList[key] null;
#if NETFRAMEWORKWinformAction?.Invoke(window);
#endifreturn window;}}#if NETFRAMEWORK/// summary/// 帮助在初始化窗体是执行一些操作比如注册键盘输入/// /summarypublic static ActionWindow WinformAction;
#endif}
这里使用了前置处理器指示词C# 前置處理器指示詞 | Microsoft Learn
通过给WinformAction事件赋值达到自动注册目的
SingleInstance.SingleInstance.WinformAction window
{ System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);
};
文章转载自: http://www.morning.jnzfs.cn.gov.cn.jnzfs.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.ftdlg.cn.gov.cn.ftdlg.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.wfyzs.cn.gov.cn.wfyzs.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn