using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PMC_Excel_Email_Automate.RPA
{
public partial class MouseRPA : Form
{
//[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
//public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", EntryPoint = "FindWindow")]
//窗口的类型或者窗口的标题,只需要传一个
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);//查找窗口内控件句柄
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, string lParam);//发送消息
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessageButton(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);// 设置窗口为激活状态
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern IntPtr WindowFromPoint(Point point);
[DllImport("user32.dll", EntryPoint = "ChildWindowFromPointEx")]//指定坐标处窗体句柄
private static extern IntPtr ChildWindowFromPointEx(IntPtr hwnd, Point pt, uint flags);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern int SetCursorPos(int x, int y);
//移动鼠标
const int MOUSEEVENTF_MOVE = 0x0001;
//模拟鼠标左键按下
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
//模拟鼠标左键抬起
const int MOUSEEVENTF_LEFTUP = 0x0004;
//模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
//模拟鼠标右键抬起
const int MOUSEEVENTF_RIGHTUP = 0x0010;
//模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
//模拟鼠标中键抬起
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
//标示是否采用绝对坐标
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
//模拟鼠标滚轮滚动操作,必须配合dwData参数
const int MOUSEEVENTF_WHEEL = 0x0800;
//SendMessage参数
private const int WM_KEYDOWN = 0X100;
private const int WM_KEYUP = 0X101;
private const int WM_SYSCHAR = 0X106;
private const int WM_SYSKEYUP = 0X105;
private const int WM_SYSKEYDOWN = 0X104;
private const int WM_CHAR = 0X102;
const uint WM_SETTEXT = 0x000C;//设置文本框内容的消息
private const int BM_CLICK = 0xF5;
static Class.Mouse Mouse = new Class.Mouse();
public MouseRPA()
{
InitializeComponent();
}
//public Form1 fm1;
private void button1_Click(object sender, EventArgs e)
{
try
{
//fm1.WindowState = FormWindowState.Minimized;
//string saproute = @"C:\Users\haiyul1\OneDrive - kochind.com\Desktop\M04 SAP_M04.sap";
System.Diagnostics.Process p1 = System.Diagnostics.Process.Start(textBox1.Text);
//p1.WaitForExit();
System.Threading.Thread.Sleep(2000);
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null, "SAP_M04");
//判断这个窗体是否有效
if (ParenthWnd != IntPtr.Zero)
{
//var t = findallchild(ParenthWnd);
//MessageBox.Show("找到窗口");
//获取输入框的控件句柄,Spy++进行查询。比如C语言编写的程序中,文本框的句柄类型一般为“EDIT”,C#写的程序则不是。
//IntPtr hwndQ = FindWindowEx(ParenthWnd, IntPtr.Zero, null, null);
//IntPtr hwndP = FindWindowEx(ParenthWnd, hwndQ, null, null); //获取密码输入框的控件句柄
//1.因为winform窗体上有很多控件,无法唯一标识,用spy++查看contol id发现不是唯一的,每次重启都会变化。所以就无法区分哪个edit控件对应哪个字段,
//2.现在发现用EnumChildsWindow来遍历控件发现取得的控件不是按Tab的顺序来的,所以这种方式也无法区分每个edit。
// 解决思路:根据控件的位置获取控件的句柄
Point pointQ = new Point { X = 188, Y = 65 };
Point pointP = new Point { X = 188, Y = 89 };
Point confirm = new Point { X = 187, Y = 126 };
IntPtr hwndQ = ChildWindowFromPointEx(ParenthWnd, pointQ, 0x0000);
IntPtr hwndP = ChildWindowFromPointEx(ParenthWnd, pointP, 0x0000);
IntPtr conf = ChildWindowFromPointEx(ParenthWnd, confirm, 0x0000);
//将窗口设置为激活
SetForegroundWindow(ParenthWnd);
System.Threading.Thread.Sleep(1000); //暂停1秒让你看到效果
SendMessage(hwndQ, WM_SETTEXT, IntPtr.Zero, "MXTANG03");//发送文本框1里面的内容
System.Threading.Thread.Sleep(1000); //暂停1秒让你看到效果
SendMessage(hwndP, WM_SETTEXT, IntPtr.Zero, "B1234567");//发送文本框2里面的内容
System.Threading.Thread.Sleep(1000);
//IntPtr childHwnd = FindWindowEx(ParenthWnd, IntPtr.Zero, null, "&Log On"); //获得按钮的句柄
//var p = PointToScreen(childHwnd.Location);
//webBrowser1.Focus();
#region success
//Mouse.MoveTo(442 + 273 + 30, 237 + 126 + 30);
//Mouse.LeftClick();
#endregion
IntPtr childHwnd = FindWindowEx(ParenthWnd, IntPtr.Zero, null, "&Logon");//按钮控件标题
if (childHwnd != IntPtr.Zero)
{
SendMessageButton(childHwnd, BM_CLICK, 0, 0);
}
//Point p_orignal = Control.MousePosition;
//SetCursorPos(200, 250);//这是用Spy++直接获取的,VS都带这个工具
//mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 200, 250, 0, 0);
//const int BM_CLICK = 0xF5;
//SendMessage(conf, BM_CLICK, IntPtr.Zero, "test");
//IntPtr childHwnd = FindWindowEx(ParenthWnd, IntPtr.Zero, null, "&Log On"); //获得按钮的句柄
//if (childHwnd != IntPtr.Zero)
//{
// SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, ""); //发送点击按钮的消息
//}
//else
//{
// MessageBox.Show("没有找到子窗口");
//}
//SetCursorPos(187, 126);
//System.Threading.Thread.Sleep(2 * 1000);
//mouse_event(MOUSEEVENTF_LEFTDOWN, 187, 126, 0, conf);
//mouse_event(MOUSEEVENTF_LEFTUP, 187, 126, 0, conf);
mouse_event(MOUSEEVENTF_LEFTDOWN, 188, 127, 0, 0);
}
else
{
MessageBox.Show("NOT Find Window");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void MouseRPA_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
}
}
}