------------------------------------------------------------------
// 1. 結構體介紹

結構在 C/C++ 上面很常使用,Win32 API 上也定義了許多結構,但 AutoIt 卻必須使用 Dll 相關函式才可以去定義、設定與讀取。先聲明,AutoIt 在結構操作上並不非常方便 (但有特例),但如果想學好 AutoIt 調用 Dll,這個就一定要學。在繼續介紹 dll 相關函式時,要先講結構的概念。

如果班老師要紀錄一個學生的分數的話,他可能會紀錄這些東西 - 學生姓名、學生學號、國文分數、英文分數、數學分數,如果用 AutoIt 的話可能要寫成這樣

$Name = "EdisonX"  ; 最大長度為 20 之字串,姓名
$NO = "B0000001"   ; 最大長度為 20 之字串,學號
$CH = 90                  ; 整數,國文成績
$ENG = 80                ; 整數,英文成績
$Math = 100             ; 整數,數學成績

------------------------------------------------------------------
// 2. 結構體製作

第一步,我們把它包成一個結構體(Struct),這個結構體我們叫 "學生",它就包含了上面五個成員。在 AutoIt 上可以這麼寫


$struct_student = "char name[20];char no[20];int ch;int eng;int math"
$student = DllStructCreate($struct_student)
if @error Then
    MsgBox(0,"","在 DllStructCreate 內發生錯誤" & @error);
    exit
endif

這裡有三點要注意,
1. 最後要去看 @error ,如果不等於 0 的話就出錯,下面就不用再做了;
2. 事實上在 C 語言沒有所謂的 "字串",只有 "字元陣列",上面的 char name[20],就是把那個 name 當成是一個字串,它的最大長度是 20。
3. $struct_student = "char name[20];char no[20];int ch;int eng;int math",要注意,資料型態和變數名稱中間會空一個空白 (char name[20]   /   char no[20] / int ch ...),分隔是用分號 (;),其它地方絕不可以擅自加上空白。

另外有第二種方式,但第二種方式以後使用很不方便。就是把那些變數名稱全都省掉,這樣就不知道結構體每個變數所代表的實際意義為何,便變成

$struct_student = "char[20];char[20];int;int;int"
$student = DllStructCreate($struct_student)
if @error Then
    MsgBox(0,"","在 DllStructCreate 內發生錯誤" & @error);
    exit
endif

因此建議不要用第二種宣告建立方式

------------------------------------------------------------------
// 3. 結構體設定資料

剛剛我們有設定了 $student 這個結構體了,接下來我們要為它設定分數,這裡用到的是 DllStructSetData


; 結構體製作
$struct_student = "char name[20];char no[20];int ch;int eng;int math"
$Student = DllStructCreate($struct_student)
if @error Then
    MsgBox(0,"","在 DllStructCreate 內發生錯誤" & @error);
    exit
endif

; 設定結構體資料
DllStructSetData($Student, "name", "EdisonX") ; 學生姓名:EdisonX
DllStructSetData($Student, "no", "B0000001") ; 學生學號:B0000001
DllStructSetData($Student, "ch", 90) ; 國文成績:90
DllStructSetData($Student, "eng", 80) ; 英文成績:80
DllStructSetData($Student, "math", 100) ; 數學成績 100

有第二種設定方式,但比較不好 (如果在上一步驟的時候就用第二種宣告,你也只有用這種方式去設定它了)

 DllStructSetData($Student, 1, "EdisonX") ; 學生姓名:EdisonX, 第一個參數
DllStructSetData($Student, 2, "B0000001") ; 學生學號:B0000001, 第二個參數
DllStructSetData($Student, 3, 90) ; 國文成績:90, 第三個參數
DllStructSetData($Student, 4, 80) ; 英文成績:80, 第四個參數

DllStructSetData($Student, 5, 100) ; 數學成績 100, 第五個參數

------------------------------------------------------------------
// 4. 讀取結構體資料

現在最後一個步驟 - 讀取結構體資料,只有調用 DllStructGetData,不過和上一個步驟一樣,有二種方式,這次我們用 MsgBox 把剛剛設定好的叫出來看



MsgBox(0, "", _
  DllStructGetData($Student, "name") & @CRLF & _
  DllStructGetData($Student, "no") & @CRLF & _
  DllStructGetData($Student, "ch") & @CRLF & _
  DllStructGetData($Student, "eng") & @CRLF & _
  DllStructGetData($Student, "math") )

第二種方式可想而知

MsgBox(0, "", _
  DllStructGetData($Student, 1) & @CRLF & _
  DllStructGetData($Student, 2) & @CRLF & _
  DllStructGetData($Student, 3) & @CRLF & _
  DllStructGetData($Student, 4) & @CRLF & _
  DllStructGetData($Student, 5) )

結構體到這裡暫時告一段落,最後附上完成的碼

; 結構體製作
$struct_student = "char name[20];char no[20];int ch;int eng;int math"
$Student = DllStructCreate($struct_student)
if @error Then
    MsgBox(0,"","在 DllStructCreate 內發生錯誤" & @error);
    exit
endif

; 設定結構體資料
DllStructSetData($Student, "name", "EdisonX") ; 學生姓名:EdisonX
DllStructSetData($Student, "no", "B0000001") ; 學生學號:B0000001
DllStructSetData($Student, "ch", 90) ; 國文成績:90
DllStructSetData($Student, "eng", 80) ; 英文成績:80
DllStructSetData($Student, "math", 100) ; 數學成績 100

MsgBox(0, "", _
  DllStructGetData($Student, "name") & @CRLF & _
  DllStructGetData($Student, "no") & @CRLF & _
  DllStructGetData($Student, "ch") & @CRLF & _
  DllStructGetData($Student, "eng") & @CRLF & _
  DllStructGetData($Student, "math") )

如果對於結構體不熟的話,請詳加練習,不然下去會很難學。

arrow
arrow
    全站熱搜

    edisonx 發表在 痞客邦 留言(0) 人氣()