WPF里怎么让一个方法响应整个窗体内的键盘事件?

SYGKT 2014-07-26 06:55:13
想实现的效果是当这个窗体被激活时,按下Ctrl+某个键让一个隐藏控件显示这样的效果,请问如何实现?
...全文
226 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
梧桐168 2015-01-31
  • 打赏
  • 举报
回复
注册系统热键 [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
SYGKT 2014-07-26
  • 打赏
  • 举报
回复
引用 1 楼 Ptrtoptr 的回复:
1在<Widnow>标签下加入下面标签,意思是Ctrl+Q ,引用的资源是Command ExpnColapSwt 下一步就来添加这个命令资源 <Window.InputBindings> <KeyBinding Modifiers="Control" Key="Q" Command="{StaticResource ExpnColapSwt}" /> </Window.InputBindings> 2添加一个命令资源 <Window.Resources> <RoutedUICommand x:Key="ExpnColapSwt" Text="Expaned or Collapse side bar"/> </Window.Resources> 3.还要定义,当命令发生时,执行的方法动作 private void SomeCommandAction(object sender, RoutedEventArgs e) { } 4把第二步和第三步的东西绑定到一块来. <Window.CommandBindings> <CommandBinding Command="{StaticResource ExpnColapSwt}" Executed="SomeCommandAction"/> </Window.CommandBindings> 总结' 当按下ctrl+q时,引发了ExpnColapSwt命令 而 ExpnColapSwt和SomeCommandAction绑定到了一起,所以SomeCommandAction将被执行,你现在就可以在 SomeCommandAction里隐藏自己想隐藏或者想显示的控件.这个很简单,你搞的定
强 请问知不知道怎么遍历一个窗口里所有控件?谢谢 (我刚用window的keryup事件发现也是全局的)
Ptrtoptr 2014-07-26
  • 打赏
  • 举报
回复
1在<Widnow>标签下加入下面标签,意思是Ctrl+Q ,引用的资源是Command ExpnColapSwt 下一步就来添加这个命令资源 <Window.InputBindings> <KeyBinding Modifiers="Control" Key="Q" Command="{StaticResource ExpnColapSwt}" /> </Window.InputBindings> 2添加一个命令资源 <Window.Resources> <RoutedUICommand x:Key="ExpnColapSwt" Text="Expaned or Collapse side bar"/> </Window.Resources> 3.还要定义,当命令发生时,执行的方法动作 private void SomeCommandAction(object sender, RoutedEventArgs e) { } 4把第二步和第三步的东西绑定到一块来. <Window.CommandBindings> <CommandBinding Command="{StaticResource ExpnColapSwt}" Executed="SomeCommandAction"/> </Window.CommandBindings> 总结' 当按下ctrl+q时,引发了ExpnColapSwt命令 而 ExpnColapSwt和SomeCommandAction绑定到了一起,所以SomeCommandAction将被执行,你现在就可以在 SomeCommandAction里隐藏自己想隐藏或者想显示的控件.这个很简单,你搞的定

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧