C# 如何在Awesomium中实现鼠标单击?

C# 如何在Awesomium中实现鼠标单击?,c#,awesomium,C#,Awesomium,我写这段代码是为了测试注入鼠标方法,但它不适合我。测试应该点击谷歌文本框搜索区域,但该框永远不会突出显示。知道为什么吗 谷歌的页面确实可以加载。代码运行(通过断点确认),但什么也没发生 public partial class Form1 : Form { private IWebView webView; public Form1() { InitializeComponent(); ini

我写这段代码是为了测试注入鼠标方法,但它不适合我。测试应该点击谷歌文本框搜索区域,但该框永远不会突出显示。知道为什么吗

谷歌的页面确实可以加载。代码运行(通过断点确认),但什么也没发生

public partial class Form1 : Form
    {
        private IWebView webView;
        public Form1()
        {
            InitializeComponent();
            initiate();

        }

        private void button1_Click(object sender, EventArgs e)
         {
            click(650, 405);
        }

        private async void initiate()
        {

            WebSession session = WebCore.CreateWebSession(
@"C:\SessionDataPath", WebPreferences.Default);
            webView = WebCore.CreateWebView(
      this.ClientSize.Width,
      this.ClientSize.Height, session, WebViewType.Window
     );
            webView.ParentWindow = this.Handle;
            webView.Source = new Uri("http://www.google.com");
            await Task.Delay(30000);
            click(650, 405);

        }

        public void click(int x, int y)
        {
            webView.InjectMouseMove(x, y);
            webView.InjectMouseDown(MouseButton.Left);
            webView.InjectMouseUp(MouseButton.Left);
        }
    }
我试图通过查看正确的chromium类来让这段代码与chromium handle一起工作,但没有成功

  private async Task<bool> clickCoorindate(Point point)
        {
            webView.FocusView();
            int x = point.X; // X coordinate of the click
            int y = point.Y; // Y coordinate of the click
            IntPtr handle = webView.ProcessHandle;
            StringBuilder className = new StringBuilder(100);
            while (className.ToString() != "Chrome_RenderWidgetHostHWND") // The class control for the browser
            {
                handle = GetWindow(handle, 5); // Get a handle to the child window
                GetClassName(handle, className, className.Capacity);
                if (className.ToString() == "Chrome_RenderWidgetHostHWND")
                    handle = Handle;

            }

            IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
            IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
            const uint downCode = 0x201; // Left click down code
            const uint upCode = 0x202; // Left click up code
            const uint moveCode = 0x200;

            SendMessage(handle, downCode, wParam, lParam); // Mouse button down
            SendMessage(handle, upCode, wParam, lParam); // Mouse button up
            Thread.Sleep(20);
            SendMessage(handle, downCode, wParam, lParam); // Mouse button down
            SendMessage(handle, upCode, wParam, lParam); // Mouse button up

            return true;

        }
专用异步任务clickCoorindate(点)
{
FocusView();
int x=point.x;//单击的x坐标
int y=point.y;//单击的y坐标
IntPtr handle=webView.ProcessHandle;
StringBuilder类名称=新StringBuilder(100);
while(className.ToString()!=“Chrome\u RenderWidgetHostHWND”)//浏览器的类控件
{
handle=GetWindow(handle,5);//获取子窗口的句柄
GetClassName(句柄、类名、类名、容量);
if(className.ToString()=“Chrome\u RenderWidgetHostHWND”)
把手=把手;
}

IntPtr lParam=(IntPtr)((y在我的XNA游戏中,我使用了很多Awesomium,这是我在Awesomium组件中实现的输入系统,它工作得非常好。 请注意,这只是我的类的一部分,所以这里没有一些方法,但不需要它们来理解这个过程 基本上是在我的线程中。基本上,我在我的表单中钩住消息并将它们转发到WebView。希望这能有所帮助

  public partial class BasicAwesomiumComponent : DrawableGameComponent {

  private delegate Int32 ProcessMessagesDelegate(Int32 code, Int32 wParam, ref Message lParam);

  private static class User32 {
     [DllImport("user32.dll", SetLastError = true)]
     internal static extern IntPtr SetWindowsHookEx(Int32 windowsHookId, ProcessMessagesDelegate function, IntPtr mod, Int32 threadId);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Int32 UnhookWindowsHookEx(IntPtr hook);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Int32 CallNextHookEx(IntPtr hook, Int32 code, Int32 wParam, ref Message lParam);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Boolean TranslateMessage(ref Message message);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern IntPtr FindWindow(String className, String windowName);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern int RegisterWindowMessage(String msg);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern IntPtr SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern bool SystemParametersInfo(Int32 nAction, Int32 nParam, ref Int32 value, Int32 ignore);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern int GetSystemMetrics(Int32 nIndex);
  }

  private static class Kernel32 {
     [DllImport("kernel32.dll", SetLastError = true)]
     internal static extern Int32 GetCurrentThreadId();
  }

  private static class SystemMetrics {
     internal static Int32 MouseWheelScrollDelta {
        get {
           return 120;
        }
     }

     internal static Int32 MouseWheelScrollLines {
        get {
           var scrollLines = 0;

           if (User32.GetSystemMetrics(75) == 0) {
              var hwnd = User32.FindWindow("MouseZ", "Magellan MSWHEEL");
              if (hwnd != IntPtr.Zero) {
                 var windowMessage = User32.RegisterWindowMessage("MSH_SCROLL_LINES_MSG");
                 scrollLines = (Int32)User32.SendMessage(new HandleRef(null, hwnd), windowMessage, 0, 0);
                 if (scrollLines != 0) {
                    return scrollLines;
                 }
              }
              return 3;
           }
           User32.SystemParametersInfo(104, 0, ref scrollLines, 0);
           return scrollLines;
        }
     }
  }

  private enum WindowsMessage {
     KeyDown = 0x0100,
     KeyUp = 0x0101,
     Char = 0x0102,

     MouseMove = 0x0200,
     LeftButtonDown = 0x0201,
     LeftButtonUp = 0x0202,
     LeftButtonDoubleClick = 0x0203,

     RightButtonDown = 0x0204,
     RightButtonUp = 0x0205,
     RightButtonDoubleClick = 0x0206,

     MiddleButtonDown = 0x0207,
     MiddleButtonUp = 0x0208,
     MiddleButtonDoubleClick = 0x0209,

     MouseWheel = 0x020A,
  }

  private struct Message {
     internal IntPtr HWnd;
     internal Int32 Msg;
     internal IntPtr WParam;
     internal IntPtr LParam;
     internal IntPtr Result;
  }

  private IntPtr hookHandle;
  private ProcessMessagesDelegate processMessages;

  private Int32 ProcessMessages(Int32 code, Int32 wParam, ref Message lParam) {
     if (this.Enabled && code == 0 && wParam == 1) {

        bool processed = false;

        switch ((WindowsMessage)lParam.Msg) {
           case WindowsMessage.KeyDown:
           case WindowsMessage.KeyUp:
           case WindowsMessage.Char:
              WebKeyboardEvent keyboardEvent = new WebKeyboardEvent((uint)lParam.Msg, lParam.WParam, lParam.LParam, 0);

              awesomiumContext.Post(state => {
                 if (!WebView.IsLive) return;
                 WebView.InjectKeyboardEvent(keyboardEvent);
              }, null);

              processed = true;
              break;

           case WindowsMessage.MouseWheel:
              var delta = (((Int32)lParam.WParam) >> 16);
              awesomiumContext.Post(state => {
                 if (!WebView.IsLive) return;
                 WebView.InjectMouseWheel(delta / SystemMetrics.MouseWheelScrollDelta * 16 * SystemMetrics.MouseWheelScrollLines, 0);
              }, null);

              processed = true;
              break;
        }

        if (!processed) {
           WindowsMessage message = (WindowsMessage)lParam.Msg;
           awesomiumContext.Post(state => {
              if (!WebView.IsLive) return;

              switch (message) {
                 case WindowsMessage.MouseMove:
                    var mouse = Mouse.GetState();
                    WebView.InjectMouseMove(mouse.X - area.X, mouse.Y - area.Y);
                    break;

                 case WindowsMessage.LeftButtonDown:
                    WebView.InjectMouseDown(MouseButton.Left);
                    break;
                 case WindowsMessage.LeftButtonUp:
                    WebView.InjectMouseUp(MouseButton.Left);
                    break;
                 case WindowsMessage.LeftButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Left);
                    break;

                 case WindowsMessage.RightButtonDown:
                    WebView.InjectMouseDown(MouseButton.Right);
                    break;
                 case WindowsMessage.RightButtonUp:
                    WebView.InjectMouseUp(MouseButton.Right);
                    break;
                 case WindowsMessage.RightButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Right);
                    break;

                 case WindowsMessage.MiddleButtonDown:
                    WebView.InjectMouseDown(MouseButton.Middle);
                    break;
                 case WindowsMessage.MiddleButtonUp:
                    WebView.InjectMouseUp(MouseButton.Middle);
                    break;
                 case WindowsMessage.MiddleButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Middle);
                    break;
              }
           }, null);
        }
        User32.TranslateMessage(ref lParam);
     }

     return User32.CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam);
  }
}
更新: 注意,在我的组件中,为了钩住消息泵,我使用

int currentThread = Kernel32.GetCurrentThreadId();
// Create the message hook.
hookHandle = User32.SetWindowsHookEx(3, ProcessMessages, IntPtr.Zero, currentThread);

我的表面在屏幕外的网络视图中,所以更复杂,这应该也适用于你

在我的XNA游戏中,我使用了很多Awesomium,这是我在Awesomium组件中实现的输入系统,它工作得非常好。 请注意,这只是我的类的一部分,所以这里没有一些方法,但不需要它们来理解这个过程 基本上是在我的线程中。基本上,我在我的表单中钩住消息并将它们转发到WebView。希望这能有所帮助

  public partial class BasicAwesomiumComponent : DrawableGameComponent {

  private delegate Int32 ProcessMessagesDelegate(Int32 code, Int32 wParam, ref Message lParam);

  private static class User32 {
     [DllImport("user32.dll", SetLastError = true)]
     internal static extern IntPtr SetWindowsHookEx(Int32 windowsHookId, ProcessMessagesDelegate function, IntPtr mod, Int32 threadId);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Int32 UnhookWindowsHookEx(IntPtr hook);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Int32 CallNextHookEx(IntPtr hook, Int32 code, Int32 wParam, ref Message lParam);

     [DllImport("user32.dll", SetLastError = true)]
     internal static extern Boolean TranslateMessage(ref Message message);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern IntPtr FindWindow(String className, String windowName);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern int RegisterWindowMessage(String msg);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern IntPtr SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern bool SystemParametersInfo(Int32 nAction, Int32 nParam, ref Int32 value, Int32 ignore);

     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     internal static extern int GetSystemMetrics(Int32 nIndex);
  }

  private static class Kernel32 {
     [DllImport("kernel32.dll", SetLastError = true)]
     internal static extern Int32 GetCurrentThreadId();
  }

  private static class SystemMetrics {
     internal static Int32 MouseWheelScrollDelta {
        get {
           return 120;
        }
     }

     internal static Int32 MouseWheelScrollLines {
        get {
           var scrollLines = 0;

           if (User32.GetSystemMetrics(75) == 0) {
              var hwnd = User32.FindWindow("MouseZ", "Magellan MSWHEEL");
              if (hwnd != IntPtr.Zero) {
                 var windowMessage = User32.RegisterWindowMessage("MSH_SCROLL_LINES_MSG");
                 scrollLines = (Int32)User32.SendMessage(new HandleRef(null, hwnd), windowMessage, 0, 0);
                 if (scrollLines != 0) {
                    return scrollLines;
                 }
              }
              return 3;
           }
           User32.SystemParametersInfo(104, 0, ref scrollLines, 0);
           return scrollLines;
        }
     }
  }

  private enum WindowsMessage {
     KeyDown = 0x0100,
     KeyUp = 0x0101,
     Char = 0x0102,

     MouseMove = 0x0200,
     LeftButtonDown = 0x0201,
     LeftButtonUp = 0x0202,
     LeftButtonDoubleClick = 0x0203,

     RightButtonDown = 0x0204,
     RightButtonUp = 0x0205,
     RightButtonDoubleClick = 0x0206,

     MiddleButtonDown = 0x0207,
     MiddleButtonUp = 0x0208,
     MiddleButtonDoubleClick = 0x0209,

     MouseWheel = 0x020A,
  }

  private struct Message {
     internal IntPtr HWnd;
     internal Int32 Msg;
     internal IntPtr WParam;
     internal IntPtr LParam;
     internal IntPtr Result;
  }

  private IntPtr hookHandle;
  private ProcessMessagesDelegate processMessages;

  private Int32 ProcessMessages(Int32 code, Int32 wParam, ref Message lParam) {
     if (this.Enabled && code == 0 && wParam == 1) {

        bool processed = false;

        switch ((WindowsMessage)lParam.Msg) {
           case WindowsMessage.KeyDown:
           case WindowsMessage.KeyUp:
           case WindowsMessage.Char:
              WebKeyboardEvent keyboardEvent = new WebKeyboardEvent((uint)lParam.Msg, lParam.WParam, lParam.LParam, 0);

              awesomiumContext.Post(state => {
                 if (!WebView.IsLive) return;
                 WebView.InjectKeyboardEvent(keyboardEvent);
              }, null);

              processed = true;
              break;

           case WindowsMessage.MouseWheel:
              var delta = (((Int32)lParam.WParam) >> 16);
              awesomiumContext.Post(state => {
                 if (!WebView.IsLive) return;
                 WebView.InjectMouseWheel(delta / SystemMetrics.MouseWheelScrollDelta * 16 * SystemMetrics.MouseWheelScrollLines, 0);
              }, null);

              processed = true;
              break;
        }

        if (!processed) {
           WindowsMessage message = (WindowsMessage)lParam.Msg;
           awesomiumContext.Post(state => {
              if (!WebView.IsLive) return;

              switch (message) {
                 case WindowsMessage.MouseMove:
                    var mouse = Mouse.GetState();
                    WebView.InjectMouseMove(mouse.X - area.X, mouse.Y - area.Y);
                    break;

                 case WindowsMessage.LeftButtonDown:
                    WebView.InjectMouseDown(MouseButton.Left);
                    break;
                 case WindowsMessage.LeftButtonUp:
                    WebView.InjectMouseUp(MouseButton.Left);
                    break;
                 case WindowsMessage.LeftButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Left);
                    break;

                 case WindowsMessage.RightButtonDown:
                    WebView.InjectMouseDown(MouseButton.Right);
                    break;
                 case WindowsMessage.RightButtonUp:
                    WebView.InjectMouseUp(MouseButton.Right);
                    break;
                 case WindowsMessage.RightButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Right);
                    break;

                 case WindowsMessage.MiddleButtonDown:
                    WebView.InjectMouseDown(MouseButton.Middle);
                    break;
                 case WindowsMessage.MiddleButtonUp:
                    WebView.InjectMouseUp(MouseButton.Middle);
                    break;
                 case WindowsMessage.MiddleButtonDoubleClick:
                    WebView.InjectMouseDown(MouseButton.Middle);
                    break;
              }
           }, null);
        }
        User32.TranslateMessage(ref lParam);
     }

     return User32.CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam);
  }
}
更新: 注意,在我的组件中,为了钩住消息泵,我使用

int currentThread = Kernel32.GetCurrentThreadId();
// Create the message hook.
hookHandle = User32.SetWindowsHookEx(3, ProcessMessages, IntPtr.Zero, currentThread);

我的表面在屏幕外的网络视图中,所以越复杂,这也应该适合你

我发布了一个单独的答案,给出了另一个方向: 请看以下要点:

这似乎是一个建议

webView.InjectMouseMove(x,y);
webView.InjectMouseDown(MouseButton.Left);
webView.InjectMouseMove(x,y);
webView.InjectMouseUp(MouseButton.Left);
因此,在两个mousedown/up事件之间移动(到相同的位置) 顺便说一句,我认为这不需要

您正在运行哪个版本的awesomium

更新: 在注入输入之前,请记住在WebView上设置焦点

webView.Focus();

我发布了一个单独的答案,给出了另一个方向: 请看以下要点:

这似乎是一个建议

webView.InjectMouseMove(x,y);
webView.InjectMouseDown(MouseButton.Left);
webView.InjectMouseMove(x,y);
webView.InjectMouseUp(MouseButton.Left);
因此,在两个mousedown/up事件之间移动(到相同的位置) 顺便说一句,我认为这不需要

您正在运行哪个版本的awesomium

更新: 在注入输入之前,请记住在WebView上设置焦点

webView.Focus();

我将viewtype设置为offscreen,injectclick工作正常,当设置为window时它不工作。我不知道为什么,但我可以使用它。

我将viewtype设置为offscreen,injectclick工作正常,当设置为window时它不工作。我不知道为什么,但我可以使用它。

如文档中所述(请参阅:),一个窗口化视图捕获所有输入本身,并且您无法使用Awesomium API以编程方式注入输入(您可以按尝试的方式,通过将本机Windows消息发送到适当的HWND来实现这一点,但这不是建议的简单过程)


为了能够使用
InjectXXX
方法以编程方式注入输入,请确保您的视图的类型为

,如文档中所述(请参阅:),窗口视图会捕获所有输入本身,并且您不能使用Awesomium API以编程方式注入输入(您可以按尝试的方式执行此操作,将本机Windows消息发送到相应的HWND,但这不是建议的简单过程)


为了能够使用
InjectXXX
方法以编程方式注入输入,请确保您的视图的类型为

我知道这种方法,我使用Flash游戏的webbrowser控件执行类似操作,但Flash与webbrowser控件有问题,因此我正在尝试退出。我无法将其发送到使用chromium,因为我无法获得视图的句柄。代码如下。请阅读我的更新,获取当前表单线程id并设置一个钩子到itI。我知道这种方法,我使用Flash游戏的webbrowser控件执行类似操作,但Flash与webbrowser控件有问题,因此我正在尝试chromium。我无法让它与chromium一起工作,因为我无法获得视图的句柄。代码如下。请阅读我的更新,获取当前表单线程id并设置一个钩子到itI。我使用的是最新版本。另外,我看到了那篇文章,我尝试了,但没有成功。有人能提供我一个示例工作代码,可以单击任何网站上的某个区域吗需要一个网络视图,或者你可以使用网络控制?我更喜欢网络视图,但我可以从网络控制开始,直到我了解更多关于awesomium的信息。我也希望以后做一个屏幕外的游戏,但无论什么让我开始,我都可以朝着这个目标前进。我正在尝试自动化flash游戏,忘了说吧“在注入输入之前,请记住在WebView上设置焦点".updatedI已经尝试过了,但是没有什么不同,所以我把它从代码中删除了。你能用谷歌页面点击文本框区域来尝试吗?我使用的是最新版本。我也看到了那篇文章,我试过了,但没有用。有人能给我提供一个示例工作代码,可以点击任何页面上的某个区域吗网站?你需要网络视图还是可以使用网络控件?我更喜欢网络视图,但我可以从网络控件开始,直到