當時看到這個問題,想到兩個的解決方法一個是利用對象的方法,賦值,編寫程函數,輸入列和行,然后由這個函數處理,這樣做直接,高效率,還有一個思路是" name="description" />

Rational 識別 Delphi TDBGrid的解決方法

發表于:2007-04-29來源:作者:點擊數: 標簽:rational識別TDBGrid解決delphi
MI LY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">當時看到這個問題,想到兩個的解決方法一個是利用對象的方法,賦值,編寫程函數,輸入列和行,然后由這個函數處理,這樣做直接,高效率,還有一個思路是

MILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">當時看到這個問題,想到兩個的解決方法一個是利用對象的方法,賦值,編寫程函數,輸入列和行,然后由這個函數處理,這樣做直接,高效率,還有一個思路是利用坐標定位然后輸入數據,也是便寫函數,具體計算由函數處理,這種方法雖然不能做到通用,但簡單實用。

按照第一個思路開始行動,因為那位朋友javascript:;" onClick="javascript:tagshow(event, '%B2%E2%CA%D4');" target="_self">測試的軟件用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函數SqaSetPropertycell賦值完成上邊要的功能,首先用robot來的gui insertobject properties來抓取對象TDBGrid的屬性,這樣做是因為函數SqaSetProperty只能對robot抓取道的屬性起作用,也就是說即使有cell屬性,robotobject 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屬性,怎么辦?于是研究delphiTDBGrid的源代碼發現TDBGrid繼承的TcustomGrid中有HideEdit,UpdateEdit等方法,說明在輸入數據的時候TDBGird在后臺是生成了一個輸入文本框,焦點改變后這個文本框自動消失把數據輸入到TDBGrid。這樣我們只要能給這個EDITTEXT屬性賦值同樣可以實現,因為TDBGrid沒有CELL屬性,不能直接利用SQASetProperty函數給cellfocus屬性賦值,只能利用點擊這個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代碼中defaultHeight24

最后代碼如下:

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 = sqaSuclearcase/" target="_blank" >ccess Then

        GetRowCount = CInt(vValue)

    Else

        sLogMessage = "無法得到Grid的列數

        sErrorMessage = "[" & CStr(Result) & " = " & Error(Result) &"]"

        SQALogMessage sqaFail, sLogMessage, sErrorMessage

    End If

   

End Function

 

就到這里吧。


原文轉自:http://www.anti-gravitydesign.com

...

熱門標簽

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97