本帖最後由 rictirse 於 2012-11-6 10:34 編輯
ini 教學
BMI計算機應用ini存檔數值
IniRead ( "檔案名稱", "區段", "關鍵字", "預設值" )
從標準設定檔 (*.ini)中讀取某個數值。
IniWrite ( "檔案名稱", "區段", "關鍵字", "數值" )
向標準設定檔 (*.ini)中寫入某個數值。
繼上一個 BMI計算機第三個DEMO
[code=autoit]#include <GUIConstantsEx.au3>
Opt ( "GUIOnEventMode", 1)
Dim $BMI, $w, $h
Dim $tmpW, $tmpH;;; 宣告 h 、 w 的暫存變數
Dim Const $ini = "BMI.ini" ;; 宣告預設 ini檔名與位置,噹噹這邊建議ini檔名最好都宣告成 const 以後方便維護
GUICreate ( "", 300, 115)
GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit") ;;OnEvent Mod call func _Exit
GUISetFont ( 10, 800);; 宣告預設的字體 與 大小
GUICtrlCreateLabel ( "Height (cm)", 20, 20, 120) ;; 建立 Label
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "Width (kg)", 110, 20)
GUICtrlSetColor ( -1, 0x1260FF)
GUICtrlCreateLabel ( "BMI", 225, 20)
GUICtrlSetColor ( -1, 0x1260FF)
$iw = GUICtrlCreateInput ( IniRead ( $ini, "BMI", "Width", 0), 20, 50, 80, 25);; 建立 Input W ; 會讀取ini 內 Width 數值
GUICtrlSetColor ( -1, 0xEE7600)
$ih = GUICtrlCreateInput ( IniRead ( $ini, "BMI", "Height", 0), 110, 50, 80, 25);; 建立 Input H ; 會讀取ini 內 Height 數值
GUICtrlSetColor ( -1, 0xEE7600)
$sol = GUICtrlCreateInput ( "", 200, 50, 80, 25)
GUICtrlSetColor ( -1, 0xEE7600)
$review = GUICtrlCreateLabel ( "評語:", 25, 85, 250, 25)
GUICtrlSetColor ( -1, 0xEE7600)
_sol() ;; 如果 ini 讀出的Width 與(and) Height 不等於0 或 空白 就運算一次
GUISetState ()
While True
$tmpH = IniRead ( $ini, "BMI", "Height", 0);; 讀取ini 內的Height 到 tmpH
$tmpW = IniRead ( $ini, "BMI", "Width", 0);; 讀取ini 內的Width 到 tmpW
If GUICtrlRead ($ih) <> $tmpH Or GUICtrlRead ($iw) <> $tmpW Then;; 判斷 GUI 視窗內的 W 「或」 H 是否跟 暫存區的 tmpW tmpH 資料是否相同 如果不相同 才執行 下面動作
IniWrite ( $ini, "BMI", "Height", GUICtrlRead ($ih)) ;; 將gui 內的ih 寫到 ini 內
IniWrite ( $ini, "BMI", "Width", GUICtrlRead ($iw)) ;; 將gui 內的iw 寫到 ini 內
_sol(); 計算
EndIf
WEnd
Func _sol();; 計算
If GUICtrlRead ($iw) <> "" And GUICtrlRead ($ih) <> "" Then;; input (iw)、(ih) 內容不等於 空白時執行
$h = GUICtrlRead ($ih)
$w = GUICtrlRead ($iw)
$w /= 100;; 單位換算 cm 轉 m
$BMI = $h/($w^2) ;; 公式 BMI = 身高(kg) / 體重平方(m)
If $BMI < 18.5 Then
$Comment = "體重過輕"
GUICtrlSetColor ( $review, 0x00CD00);; 體重過輕 綠色
ElseIf $BMI <= 24 Then
$Comment = "正常體重"
GUICtrlSetColor ( $review, 0x87CEFF);; 正常體重 天藍色
ElseIf $BMI <= 27 Then
$Comment = "過重"
GUICtrlSetColor ( $review, 0xEE9A00);; 過重 橙色
ElseIf $BMI <= 30 Then
$Comment = "輕度過胖"
GUICtrlSetColor ( $review, 0xEE4000);; 輕度過重 橘紅色
ElseIf $BMI <= 35 Then
$Comment = "中度過胖"
GUICtrlSetColor ( $review, 0xFF0000);; 中度過重 鮮紅色
Else
$Comment = "重度過胖"
GUICtrlSetColor ( $review, 0xBF3EFF);; 重度過重 紫色
EndIf
GUICtrlSetData ( $sol, StringFormat ( "%f", $BMI)) ;;修改 Input (sol)內的資料 使用 BMI 取代
GUICtrlSetData ( $review, StringFormat ( "評語:%s", $Comment)) ;;修改 Input (review)內的資料 填入評語 %f = 小數點下 六位數
EndIf
EndFunc
Func _Exit()
Exit
EndFunc[/code] |