2

我希望为其分配热键的表单上有一个按钮:

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");
        }
    }
}
4

1 回答 1

1

假设这是一个带有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();
    }
于 2010-03-19T02:47:29.517 回答