群发淘宝消息 另外的思路 使用 UI Automation 和 api

本文介绍了一个使用C#实现的UI自动化操作案例,包括通过进程ID和自动化ID查找元素、发送文本消息等步骤。该案例展示了如何利用UIAutomationClient等库进行窗口定位及交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 添加引用。 需要添加UIAutomationClient.dll,UIAutomationClientSideProvider.dll,UIAutomationTypes.dll

2. 添加相应的命名空间System.Windows.Automation。

使用 ui spy工具查看 processId 和automationId

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.Windows.Automation;
 
using System.Runtime.InteropServices;
using System.Windows;

using System.Diagnostics;
namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
        const int WM_SETTEXT = 0x000C;//发送文本 
        private void button1_Click(object sender, EventArgs e)
        {

            AutomationElement element = FindElementById(3572, "1172");
            MessageBox.Show(element.Current.ClassName);
           
            SendMessage((IntPtr)element.Current.NativeWindowHandle, WM_SETTEXT, 0, "淘宝群发工具");
           
        }
        //public static void ClickLeftMouse(int processId, string automationId)
        //{
        //    AutomationElement element = FindElementById(processId, automationId);

        //    if (element == null)
        //    {
        //        throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.",
        //            element.Current.AutomationId, element.Current.Name));
        //    }

        //    Rect rect = element.Current.BoundingRectangle;
        //    int IncrementX = (int)(rect.Left + rect.Width / 2);
        //    int IncrementY = (int)(rect.Top + rect.Height / 2);

        //    //Make the cursor position to the element.
        //    SetCursorPos(IncrementX, IncrementY);

        //    //Make the left mouse down and up.
        //    mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
        //    mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero);
        //}
        public  AutomationElement FindWindowByProcessId(int processId)
        {
            AutomationElement targetWindow = null;
            int count = 0;
            try
            {
                Process p = Process.GetProcessById(processId);
                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
                return targetWindow;
            }
            catch (Exception ex)
            {
                count++;
                StringBuilder sb = new StringBuilder();
                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
                if (count > 5)
                {
                    throw new InvalidProgramException(message, ex);
                }
                else
                {
                    return FindWindowByProcessId(processId);
                }
            }
        }
        public  AutomationElement FindElementById(int processId, string automationId)
        {
            AutomationElement aeForm = FindWindowByProcessId(processId);
            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
            return tarFindElement;
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunjay117

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值