在 C# 中,我想点击一个按钮。我不想使用像 button.click+=... 这样的按钮事件,我想点击表单应用程序中的按钮,我的电脑的任何键都会被点击。我尝试了 SendKeys,但效果不佳。
我需要更多的想法如何做到这一点。我试过了:
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.Threading;
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
Button[,] b;
List<char> chrlist = new List<char>();
public Form5()
{
InitializeComponent();
start();
}
public void start()
{
panel1.Controls.Clear();
int m = 13;
int n = 2;
b = new Button[n, m];
int x = 0;
int i, j;
int y = 0;
// int count = 0;
int chr1=(int)'A';
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
// count++;
b[i, j] = new Button();
b[i, j].SetBounds(x, y, panel1.Size.Width / m, panel1.Size.Height / n);
b[i, j].Name = i + "," + j;
b[i, j].Text = ((char)chr1).ToString();
b[i, j].Click += new EventHandler(ButtonClick);
panel1.Controls.Add(b[i, j]);
x = x + panel1.Size.Width / m;
chr1++;
}
y = y + panel1.Size.Height / n;
x = 0;
}
}
public void ButtonClick(Object sender, EventArgs e)
{
Button a = (Button)sender;
MessageBox.Show(a.Text);
}
public void started()
{
while (true)
{
int sec = 1000;
Thread.Sleep(sec * 3);
SendKeys.SendWait("{TAB}");
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread workerThread = new Thread(started);
workerThread.Start();
}
}
}
这在我的其他应用程序中不起作用。