【IT168 技术文章】
当时看到这个问题,想到两个的解决方法一个是利用对象的方法,赋值,编写程函数,输入列和行,然后由这个函数处理,这样做直接,高效率,还有一个思路是利用坐标定位然后输入数据,也是便写函数,具体计算由函数处理,这种方法虽然不能做到通用,但简单实用。
按照第一个思路开始行动,因为那位朋友测试的软件用delphi开发,其中gird是个第三方控件,这里我用TDBGrid来代替他的第三方控件做到问题简单化,先把实际问题解决,自己动手做了个程序,用ADO连接数据库,利用DataGrid显示数据,来模拟实际情况。录制脚本如下:
Sub Main
Dim Result As Integer
'Initially Recorded: 2004-4-22 23:01:19
'Script Name: First
StartApplication """C:\Documents and Settings\Administrator\桌面\T\Project1.exe"""
Window SetContext, "Caption=Form1", ""
GenericObject DblClick, "Class=TDBGrid;ClassIndex=1", "Coords=168,27"
InputKeys "good"
GenericObject Click, "Class=TDBGrid;ClassIndex=1", "Coords=369,33"
Window CloseWin, "", ""
End Sub
用过delphi的人都知道TstringGrid有个Cell属性,我们可以利用robot函数SqaSetProperty给cell赋值完成上边要的功能,首先用robot来的gui insert的object properties来抓取对象TDBGrid的属性,这样做是因为函数SqaSetProperty只能对robot抓取道的属性起作用,也就是说即使有cell属性,robot的object properties抓取不到这个属性,我们也无法实现前边的想法。用object properties抓取TDBGrid的属性后发现,只抓取到了部分属性,根本没有可以利用的cell属性,怎么办,利用delphi enabler来做一次看是否可以得到更多的属性,我把sqasrv.pas(其实delphi enabler的核心就是这个delphi单元文件)加入到工程文件中,重新编译(可能编译的时候有一些问题,我遇到的问题是Unit 'oleAuto' is deprecated,用$WARN UNIT_DEPRECATED ON/OFF编译开关,就可以解决了),重新录制脚本如下:(这回TDBGrid的属性基本都抓取到了)。
Sub Main
Dim Result As Integer
'Initially Recorded: 2004-4-22 23:38:55
'Script Name: Second
StartApplication """C:\Documents and Settings\Administrator\桌面\T\Project1.exe"""
Window SetContext, "Name=Form1;Type=Form", ""
GenericObject DblClick, "Name=DBGrid1;Type=CustomGrid", "Coords=93,30"
InputKeys "jack"
GenericObject Click, "Name=DBGrid1;Type=CustomGrid", "Coords=338,26"
Window CloseWin, "", ""
End Sub
可是仍然没有我们要的cell属性,只有columns属性,没有我需要的cell属性,怎么办?于是研究delphi的TDBGrid的源代码发现TDBGrid继承的TcustomGrid中有HideEdit,UpdateEdit等方法,说明在输入数据的时候TDBGird在后台是生成了一个输入文本框,焦点改变后这个文本框自动消失把数据输入到TDBGrid。这样我们只要能给这个EDIT的TEXT属性赋值同样可以实现,因为TDBGrid没有CELL属性,不能直接利用SQASetProperty函数给cell的focus属性赋值,只能利用点击这个cell来赋予它焦点,因为只有焦点在这个cell上后才会出现浮动的文本框,修改上边的脚本:
Sub Main
Dim Result As Integer
Dim Var as String
'Initially Recorded: 2004-4-22 23:38:55
'Script Name: Second
StartApplication """C:\Documents and Settings\Administrator\桌面\T\Project1.exe"""
Window SetContext, "Name=Form1;Type=Form", ""
GenericObject DblClick, "Name=DBGrid1;Type=CustomGrid", "Coords=93,30"
Result = SQASetProperty ("Name=;Type=CustomGrid;Index=1", "Text", "Mr.")
Result = SQAGetProperty("Name=;Type=CustomGrid;Index=1", "Text", VAR)
GenericObject Click, "Name=DBGrid1;Type=CustomGrid", "Coords=338,26"
if Result = 0 then
MsgBox "Hello"
End if
GenericObject Click, "Name=DBGrid1;Type=CustomGrid", "Coords=338,26"
'Window CloseWin, "", ""
End Sub
虽然赋值成功,但是datagrid无法显示你需要的数据(这里需要研究一下),因为时间的缘故,就用第二种方式坐标的方式来实现,考虑多平台共享使用,写SetGridFocus函数,思路这样得到tdbgrid对象,得到其中column的属性宽高,计算坐标(这些都在函数内部实现),其中移动有个固定值24是追踪tdbgrid代码中defaultHeight为24
最后代码如下: Sub SetGridFocus(strObject as String, strCol as String, strRow as String, strValue as String)
Dim iResult as Integer
Dim iCount as Integer
Dim ColWidth() as integer
Dim iWidth as Integer
Dim strColPos as Integer
Dim strRowPos as Integer
Dim strCor as String
Dim iTemp as integer
strColPos = 0
strRowPos = 0
iTemp = 0
SQAGetProperty strObject ,"Columns.Count", iCount
if iCount <> 0 then
Redim ColWidth(iCount - 1)
While iTemp < iCount
iResult = SQAGetProperty (strObject, "Columns.Items(" + CStr(iTemp) +").Width",iWidth)
ColWidth(iTemp) = iWidth
iTemp = iTemp + 1
wend
End if
strColPos = 20
if CInt(strCol) <> 1 then
for iTemp = 1 to CInt(strCol)-1
strColPos = strColPos+ ColWidth(iTemp-1)
next iTemp
End if
for iTemp = 1 to CInt(strRow)
strRowPos = 24 +strRowPos
next iTemp
strCor = "Coords=" & strColPos &","+CStr(strRowPos)
GenericObject Click, strObject, strCor
InputKeys strValue
stop
strCor = "Coords=" & strColPos &","+CStr(strRowPos + 24)
GenericObject Click, strObject, strCor
End Sub
上编写的还有很多疏漏的地方,对于浮动文本框赋值成功,却无法改动tdbgrid中的值没有深入研究,该函数在实际应用中还有个小问题,没有做到很完全“周到“ ,下回有时间改正。
备注:
最后我把代码进行优化和扩展,开发了针对grid的相关函数,下边是其中之一
//得到列数
Function GetColCount(sGridRecogStr as String) as Integer
Dim Result as Integer
Dim vValue as Variant
Dim sLogMessage as String
Dim sErrorMessage as String
grdGetRowCount = -1
Result = SQAGetProperty(sGridRecogStr, "Column.Count", vValue)
If Result = sqaSuccess Then
GetRowCount = CInt(vValue)
Else
sLogMessage = "无法得到Grid的列数”
sErrorMessage = "[" & CStr(Result) & " = " & Error(Result) &"]"
SQALogMessage sqaFail, sLogMessage, sErrorMessage
End If
End Function