C# 模擬點擊


在網上看到有人問C#中模擬鼠標點擊按鈕的帖子,在VB中用API實現的代碼網上不少,可用C#寫的基本就沒有了,在這里簡單寫個事例。

1、首先建一個Demo項目。只有一個表單,標題是"Demo"。里面放一個按鈕Button1,Text設置為"Click Me"。編譯成exe。做事例用,這個代碼就不用貼出來了吧 呵呵。

2、新建項目,添加表單,拖個按鈕。代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ClickButton_SendMessage
{
    public delegate bool CallBack(IntPtr hwnd, int lParam);

    
    public partial class frmMain : Form
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr FindWindow(string strClass, string strWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowRect(IntPtr hwnd,RECT lpRect);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int SetCursorPos(int x,int y);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern void mouse_event(int dwFlags,int dx,int dy,int cButtons, int dwExtraInfo);
        public const int MOUSEEVENTF_LEFTDOWN = 0x2;
        public const int MOUSEEVENTF_LEFTUP = 0x4;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hwnd, string lpString, int cch);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowTextLength(IntPtr hwnd);

 


        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        public struct POINT
        {
            public int x;
            public int y;
        }

        public static int nCount;

        public frmMain()
        {
            InitializeComponent();
            
        }

 


        public static bool EnumWindowsProc(IntPtr h, int lParam)
        {

            string strText=new string((char)0,255);
            RECT hRect=new RECT();

            nCount = nCount + 1;
            
            int Ret=0;
            
            Ret = GetWindowTextLength(h);
            string sSave = new string((char)9, Ret);
            GetWindowText(h, sSave, Ret + 1);
            if(sSave.IndexOf("Click Me",0) > -1)
            {
                //GetWindowRect(h, hRect);
                //SetCursorPos((hRect.Left + hRect.Right) / 2, (hRect.Top + hRect.Bottom) / 2);
                SetCursorPos(200, 160);//這是用Spy++直接獲取的,VS都帶這個工具
                SetForegroundWindow(h);
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                return false;
            }
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr h;
            string strName = "Demo";
            h = FindWindow(null, strName);
            Debug.Print(h.ToString());
            if (h.ToInt32() != 0)
            {
                CallBack myCallBack = new CallBack(frmMain.EnumWindowsProc);
                EnumChildWindows(h, myCallBack, 0);
            }
        }
    }
}
3、如果想不停的點擊 加個Timer不停點擊。 或者加個變量IsLogin = FindWindow(null,"登陸成功后的標題 或 登錄失敗提示標題")
另外,只有登錄按鈕所在的窗體在激活時才能點中。 可以用ShowWindow()先激活窗體,然后再進行點擊操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM