• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# 使用Win32 API模拟键盘鼠标操作网页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#开源框架发布时间:2022-07-10
下一篇:
剑指Offer的学习笔记(C#篇)-- 平衡二叉树(二叉树后序遍历递归详解版) ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap