C# 如何使热键触发Windows窗体按钮?
我在表单上有一个按钮,希望为其分配热键:C# 如何使热键触发Windows窗体按钮?,c#,winforms,hotkeys,C#,Winforms,Hotkeys,我在表单上有一个按钮,希望为其分配热键: namespace WebBrowser { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } int GetPixel(int x, int y) { Bitmap bmp = new Bit
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窗体项目:WebBrowser
在有焦点的任何时候都会“吃掉击键”,即使Form KeyPreview设置为“true”
使用WebBrowser PreviewKeyDown事件调用按钮单击:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs event)
{
// Possibly filter here for certain keystrokes?
// Using e.KeyCode, e.KeyData or whatever.
button1.PerformClick();
}
好奇:这个按钮的真正用途是什么?当最终用户的光标位于WebBrowser控件边界内时,是否只希望执行当前ButtonClick处理程序中的代码?或者是否要“快照”窗体边界中光标下的任何位置的颜色,包括按钮本身上的颜色?覆盖窗体的颜色。