testBarcodeTextBox.Focus(); SendKeys.Send(barcode); SendKeys.Send("{ENTER}");并且怎么获取测试条码框的焦点,不知道测试软件的条码框名称
时间: 2024-02-13 21:06:34 浏览: 62
如果你不知道测试软件的条码框名称,可以使用Windows API来查找测试软件窗口,并获取它的子窗口句柄。然后,可以使用SendMessage或PostMessage函数向该子窗口发送消息,从而将条码发送到测试软件中。具体步骤如下:
1. 使用FindWindow函数查找测试软件的顶层窗口,该函数的第一个参数是窗口类名,第二个参数是窗口标题,可以根据具体情况选择。
```csharp
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr testWnd = FindWindow(null, "测试软件窗口标题");
```
2. 使用EnumChildWindows函数列举测试软件窗口的子窗口(即条码框),并找到条码框的句柄。
```csharp
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumChildProc(IntPtr hwnd, IntPtr lParam);
IntPtr testBarcodeWnd = IntPtr.Zero;
bool EnumChildProcCallback(IntPtr hwnd, IntPtr lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, className.Capacity);
if (className.ToString() == "Edit")
{
testBarcodeWnd = hwnd;
return false; // 找到了,停止遍历
}
return true;
}
EnumChildWindows(testWnd, EnumChildProcCallback, IntPtr.Zero);
```
其中,EnumChildWindows函数用于列举子窗口,EnumChildProc是一个回调函数,用于处理每个子窗口,GetClassName函数用于获取窗口类名。
在上述代码中,我们遍历测试软件窗口的所有子窗口,查找类名为"Edit"的窗口,即文本框窗口。如果找到了测试软件的条码框,就将它的句柄保存在testBarcodeWnd变量中,并返回false停止遍历。
3. 发送条码到测试软件的条码框中,可以使用SendMessage或PostMessage函数。
```csharp
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
SendMessage(testBarcodeWnd, WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(barcode));
SendMessage(testBarcodeWnd, WM_KEYDOWN, (IntPtr)VK_RETURN, IntPtr.Zero);
SendMessage(testBarcodeWnd, WM_KEYUP, (IntPtr)VK_RETURN, IntPtr.Zero);
```
其中,WM_SETTEXT消息用于设置文本框内容,WM_KEYDOWN和WM_KEYUP消息用于模拟按下和释放Enter键。VK_RETURN是Enter键的虚拟键码。
以上代码中使用的常量和函数需要先声明,可以将它们放在类的静态区域,或者使用using static语句将它们导入到命名空间中。完整代码如下:
```csharp
using System;
using System.Runtime.InteropServices;
using System.Text;
public class TestBarcodeSender
{
const uint WM_SETTEXT = 0x000C;
const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const int VK_RETURN = 0x0D;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
public delegate bool EnumChildProc(IntPtr hwnd, IntPtr lParam);
public static void SendBarcodeToTestSoftware(string barcode, string testSoftwareWindowTitle)
{
IntPtr testWnd = FindWindow(null, testSoftwareWindowTitle);
if (testWnd != IntPtr.Zero)
{
IntPtr testBarcodeWnd = IntPtr.Zero;
bool EnumChildProcCallback(IntPtr hwnd, IntPtr lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, className.Capacity);
if (className.ToString() == "Edit")
{
testBarcodeWnd = hwnd;
return false; // 找到了,停止遍历
}
return true;
}
EnumChildWindows(testWnd, EnumChildProcCallback, IntPtr.Zero);
if (testBarcodeWnd != IntPtr.Zero)
{
SendMessage(testBarcodeWnd, WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(barcode));
SendMessage(testBarcodeWnd, WM_KEYDOWN, (IntPtr)VK_RETURN, IntPtr.Zero);
SendMessage(testBarcodeWnd, WM_KEYUP, (IntPtr)VK_RETURN, IntPtr.Zero);
}
else
{
throw new Exception("未找到测试软件的条码框!");
}
}
else
{
throw new Exception("未找到测试软件的主窗口!");
}
}
}
```
使用时,直接调用SendBarcodeToTestSoftware方法即可:
```csharp
TestBarcodeSender.SendBarcodeToTestSoftware("1234567890", "测试软件窗口标题");
```
阅读全文