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()先激活窗体,然后再进行点击操作。

转载于:https://www.cnblogs.com/upshania/p/7492348.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值