C# 使用Win32 API模拟键盘鼠标操作网页
在webbrowser控件中的document complete 事件中設置鏈接到自身
??????? private void ieFrame_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
??????? {
??????????? foreach (HtmlElement link in ieFrame.Document.Links)
??????????? {
??????????????? link.SetAttribute("target", "_self");
??????????? }
??????????? foreach (HtmlElement form in ieFrame.Document.Forms)
??????????? {
??????????????? form.SetAttribute("target", "_self");
??????????? }
??????? }
使用下面封裝完的函數模擬操作即可
#region 系統API申明區域
??????? [DllImport("user32.dll")]
??????? public static extern bool SetCursorPos(int x, int y);
??????? [Flags]
??????? public enum MouseEventFlag : uint
??????? {
??????????? ABSOLUTE = 0x8000,
??????????? MOVE = 0x0001,
??????????? LEFTDOWN = 0x0002,
??????????? LEFTUP = 0x0004,
??????????? RIGHTDOWN = 0x0008,
??????????? RIGHTUP = 0x0010,
??????????? MIDDLEDOWN = 0x0020,
??????????? MIDDLEUP = 0x0020,
??????????? XDOWN = 0x0080,
??????????? XUP = 0x0100,
??????????? WHEEL = 0x0800,
??????????? HWHEEL = 0x01000,
??????????? VIRTUALDESK = 0x4000
??????? }
??????? [DllImport("user32.dll")]
??????? public static extern bool mouse_event(MouseEventFlag dwFlags, int dx, int dy, uint cButton, UIntPtr dwExtraInfo);
??????? [DllImport("user32.dll")]
??????? public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, UIntPtr dwExtraInfo);
??????? /// <summary>
??????? /// 模擬按左鍵
??????? /// </summary>
??????? /// <param name="x"></param>
??????? /// <param name="y"></param>
??????? public void LeftClick(int x, int y)
??????? {
??????????? Point p = Control.MousePosition;
??????????? SetCursorPos(x, y);
??????????? mouse_event(MouseEventFlag.LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
??????????? mouse_event(MouseEventFlag.LEFTUP, 0, 0, 0, UIntPtr.Zero);
??????????? SetCursorPos(p.X, p.Y);
??????? }
??????? /// <summary>
??????? /// 模擬鍵盤操作
??????? /// </summary>
??????? /// <param name="k"></param>
??????? public void PressKey(Keys k)
??????? {
??????????? keybd_event((byte)k, 0, 0, UIntPtr.Zero);
??????????? keybd_event((byte)k, 0, 0x2, UIntPtr.Zero);
??????? }
??????? #endregion
總結
以上是生活随笔為你收集整理的C# 使用Win32 API模拟键盘鼠标操作网页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 市场营销影响消费者购买行为的因素有哪些(
- 下一篇: 江米(说一说江米的简介)