我有一个包含很多控件的父表单.我要做的是过滤该表单的所有按键.麻烦的是,如果焦点在表单上的一个控件上,那么父表单没有获得按键事件,那么如何捕获按键事件?
小智 5
这仅适用于表单,但如果其他任何组件都处于焦点,则无效
public partial class ChildForm : Form
{
public ChildForm()
{
KeyPress += KeyPressHandler;
}
public KeyPressHandler(object sender, KeyPressEventArgs e)
{
if (_parent != null)
{
_parent.NotifyKeyPress(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使关注其他组件,这也将起作用
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
MessageBox.Show("You pressed the F1 key");
return true; // indicate that you handled this keystroke
}
// Call the base class
return base.ProcessCmdKey(ref msg, keyData);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31192 次 |
| 最近记录: |