5,927
社区成员




var
i, j, iHeight: Integer;
Str: string; //设置需要输入的表格列值,这里的示例就是一个固定的值
StrList: TStringList; //这个是存储表格列的所有列的位置
begin
iHeight := 0;
//添加测试表格列的位置
StrList := TStringList.Create;
StrList.Add('330');
StrList.Add('480');
StrList.Add('550');
StrList.Add('610');
StrList.Add('670');
StrList.Add('730');
StrList.Add('800');
StrList.Add('860');
StrList.Add('920');
//根据句柄设置窗体前置
SetForegroundWindow(198156);
//根据表格句柄设置表格获取焦点
Windows.SetFocus(67248);
//这里模拟的是3行数据
for j := 0 to 2 do
begin
iHeight := 380 + j * 23; //这里设置的行高是23,每增一行增加相应的高度
for i := 0 to StrList.Count - 1 do
begin
//将鼠标移动到相应的表格列
SetCursorPos(StrToInt(StrList[i]), iHeight);
//模拟鼠标左键点击
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//设置表格列的数据,这里模拟的是一个固定值
Str := 'TEST';
//将值拷贝到剪贴板
Clipboard.AsText := Str;
//因为我这模拟的是TStringGrid,需要先获取输入状态(试过,模拟鼠标再次点击不能进入输入状态)
keybd_event(Ord('A')+i, 0, 0, 0);
keybd_event(Ord('A')+i, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_BACK, 0, 0, 0);
keybd_event(VK_BACK, 0, KEYEVENTF_KEYUP, 0);
--模拟键盘CTRL+V粘帖数据到表格列
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event(Ord('V'), MapVirtualKey(Ord('V'), 0), 0, 0);
keybd_event(Ord('V'), MapVirtualKey(Ord('V'), 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
--等待20毫秒
Sleep(20);
end;
end;
//清空对象
FreeAndNil(StrList);
//设置当前自己的程序前置
SetForegroundWindow(Handle);
end;
var
ChildHandle, MainHandle: THandle;
P: TPoint;
begin
P.X := 60;
P.Y := 30;
MainHandle := StringGrid1.Handle; //获取StringGrid1的句柄
//MainHandle := Handle; //获取窗体的句柄
ChildHandle := ChildWindowFromPoint(MainHandle, P);
end;