C#模拟按键,而无需将鼠标移到Forms Webbrowser

时间:2019-02-23 09:41:30

标签: c# windows forms click mouse

如果要按下一个按钮,我想模拟一个木斯克。 应该在同一程序代码中的Windowsforms Webbrowser元素上执行mouseclick。它还不应移动我自己的鼠标(电缆鼠标)。到目前为止,我的问题是它可以移动鼠标。最后,即使Form应用程序最小化,我也希望做到这一点。 这是我尝试过的代码:

public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;

        public static void LeftMouseClick(int xpos, int ypos)
        {
            SetCursorPos(xpos, ypos);
            mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void btnVerbinden_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(txtInternetAdresse.Text);
        }

        private void MousTest_Click(object sender, EventArgs e)
        {
            LeftMouseClick(600, 500);
            SendKeys.Send("{ENTER}");
        }

Coudn`t找不到像这样的帖子:(