如何使热键触发Windows窗体按钮? [英] How can I make a hotkey trigger a Windows Forms button?
本文介绍了如何使热键触发Windows窗体按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在希望为其分配热键的表单上有一个按钮:
I have a button on a form to which I wish to assign a hot-key:
namespace WebBrowser
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
int GetPixel(int x, int y)
{
Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppPArgb);
Graphics grp = Graphics.FromImage(bmp);
grp.CopyFromScreen(new Point(x,y), Point.Empty, new Size(1,1));
grp.Save();
return bmp.GetPixel(0, 0).ToArgb();
}
// THIS! How can I make a hot-key trigger this button?
//
void Button1Click(object sender, EventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
int pixel = GetPixel(x,y);
textBox1.Text = pixel.ToString();
}
void MainFormLoad(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.com");
}
}
}
推荐答案
假定这是一个带有WebBrowser
控件的Windows Forms项目:WebBrowser
会在有焦点的任何时候吃掉击键",即使Form KeyPreview也是如此.设置为"true".
Assuming this is a Windows Forms project with a WebBrowser
control: the WebBrowser
will "eat the keystrokes" anytime it has focus, even if Form KeyPreview is set to 'true'.
使用WebBrowser PreviewKeyDown事件调用按钮单击:
Use the WebBrowser PreviewKeyDown event to call the button click:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs event)
{
// Possibly filter here for certain keystrokes?
// Using e.KeyCode, e.KeyData or whatever.
button1.PerformClick();
}
这篇关于如何使热键触发Windows窗体按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文