C# 单击按钮并在外部应用程序中执行操作
我正在尝试创建一个C#Windows表单应用程序,它将把CSV文件中的数据插入Sage 50 accounts应用程序。我已经创建了自动向应用程序发送登录名和密码的按钮:C# 单击按钮并在外部应用程序中执行操作,c#,C#,我正在尝试创建一个C#Windows表单应用程序,它将把CSV文件中的数据插入Sage 50 accounts应用程序。我已经创建了自动向应用程序发送登录名和密码的按钮: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; u
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
string login = "user";
string pass = "password";
IntPtr WindowHandle = FindWindow(null, "Logon");
if (WindowHandle == IntPtr.Zero)
{
MessageBox.Show("Window doesn't exist");
return;
}
SetForegroundWindow(WindowHandle);
SendKeys.SendWait(login);
SendKeys.SendWait("{TAB}");
SendKeys.SendWait(pass);
SendKeys.SendWait("{ENTER}");
}
private void button2_Click(object sender, EventArgs e)
{
IntPtr WindowHandle = FindWindow(null, "Sage 50 Accounts Professional");
SetForegroundWindow(WindowHandle);
}
}
}
我尝试使用SendMessage函数在Sage应用程序中单击按钮,还使用Spy++获取按钮ID,但我不确定是否正确使用了它:
private void button2_Click(object sender, EventArgs e)
{
IntPtr WindowHandle = FindWindow(null, "Sage 50 Accounts Professional");
SetForegroundWindow(WindowHandle);
const int WM_COMMAND = 0x0111;
const int btn = 0x12EEA720;
SendMessage(WindowHandle, WM_COMMAND, btn, 0);
}
我该怎么做才能让它工作?如何向所选按钮/标签等发送操作。?我在MS文档中搜索过,但这对我来说似乎不清楚
我希望我的应用程序单击此按钮:
我该怎么做才能让它工作?如何将操作发送到选定的服务器
按钮/标签等。?我在MS文档中搜索过,但这对我来说似乎不清楚
我
在您的程序中,您正在向“Sage 50 Accounts Professional window”发送消息WM_命令,而不是特定的UI控件,如按钮或标签
来自MSDN
SendMessage()将指定的消息发送到一个或多个窗口。发送消息
函数调用指定窗口的窗口过程,并执行以下操作
直到窗口过程处理完消息后才返回
spy++自动化的主要步骤
- 确定您的应用程序类型,要调查64位应用程序,您需要启动spyxx_amd64.exe,否则您可以使用常规的spyxx.exe
- 首先研究希望向其发送消息的UI控件的窗口处理程序。我已经使用spy++调查了按钮控件,并生成了下图
- 识别
的值,这是lpszWindow
的第四个参数。这可能很棘手,有时您可能不知道按钮的真实名称,有时它可能会以FindWindowEx()
作为前缀,有时它可能不会有什么不同。 在上面的示例中,&buttonext
的lpszWindow的值是按钮
按钮文本
- 识别可从Spy++中找到的
的第二个参数值,即SendMessage()
。根据下图,其值为uInt32 Msg
0x0201
- 识别要向其发送消息的控件的第一个参数的值,该参数是
窗口hwnd句柄
解决方案:
- 与WinForm应用程序交互的控制台应用程序(请参阅代码中的内联注释)
- 简单Winform App重新创建问题
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int counter = 0;
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = counter++.ToString();
}
private void label1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("You got me !!");
}
}
}
WinForm Form1.Designer.CS
using System.Drawing;
namespace WindowsFormsApp2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(178, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(318, 200);
this.button1.TabIndex = 1;
this.button1.Text = "ButtonText";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(641, 34);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "TextBoxText";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(400, 25);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(119, 21);
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "CheckBoxText";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(615, 132);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 17);
this.label1.TabIndex = 4;
this.label1.Text = "LabelText";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label1;
}
}
使用系统图;
命名空间WindowsFormsApp2
{
部分类Form1
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
this.textBox1=new System.Windows.Forms.TextBox();
this.checkBox1=new System.Windows.Forms.CheckBox();
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
//
//按钮1
//
此.button1.Location=新系统图纸点(178,84);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(318200);
this.button1.TabIndex=1;
this.button1.Text=“ButtonText”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
//
//文本框1
//
this.textBox1.Location=新系统.图纸.点(641,34);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(100,22);
this.textBox1.TabIndex=2;
this.textBox1.Text=“TextBoxText”;
//
//复选框1
//
this.checkBox1.AutoSize=true;
this.checkBox1.Location=新系统图纸点(400,25);
this.checkBox1.Name=“checkBox1”;
this.checkBox1.Size=新系统图纸尺寸(119,21);
this.checkBox1.TabIndex=3;
this.checkBox1.Text=“CheckBoxText”;
this.checkBox1.UseVisualStyleBackColor=true;
//
//标签1
//
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(615132);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(46,17);
this.label1.TabIndex=4;
this.label1.Text=“LabelText”;
this.label1.Click+=newsystem.EventHandler(this.label1\u Click);
//
//表格1
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(8F,16F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图尺寸(800450);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
using System.Drawing;
namespace WindowsFormsApp2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(178, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(318, 200);
this.button1.TabIndex = 1;
this.button1.Text = "ButtonText";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(641, 34);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 22);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "TextBoxText";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(400, 25);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(119, 21);
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "CheckBoxText";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(615, 132);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 17);
this.label1.TabIndex = 4;
this.label1.Text = "LabelText";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label1;
}
}