C # using Win32 API simulate keyboard and mouse operation page

document complete event in webbrowser control in the Settings link to itself

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

After packaging using the following function to simulate the operation

#region system API stated region
        [the DllImport ( "User32.dll")]
        public static extern BOOL the SetCursorPos (int X, Y int);

        [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>
        /// simulated keyboard operation
        /// </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

Guess you like

Origin www.cnblogs.com/soundcode/p/12523905.html