afan 发表于 2023-9-11 16:03:25

邪恶海盗 发表于 2023-9-11 15:58
好像这屎山代码还有很大的优化空间...

重复的比较多,可能可以精简掉80%...

邪恶海盗 发表于 2023-9-11 17:59:19

afan 发表于 2023-9-11 16:03
重复的比较多,可能可以精简掉80%...

不重要,能运行起来就行,以后再说...{:1_617:}

邪恶海盗 发表于 2023-9-12 16:05:05

afan 发表于 2023-9-11 16:03
重复的比较多,可能可以精简掉80%...

想到一个精简办法,比如先把每个Radio的GroupID/RadioID/Text/Color/Value写到一个数组$Radios[$n]里,比如这样


GroupID RadioID Text Color Value
1 1 a b c
1 2 df e f
2 3 xz ui 99
4 1 wa jn 00
5 5 c5 ee 11

然后再用for to来循环创建Radio,关键是看了半天数组,多维的完全看不懂...:face (13):

afan 发表于 2023-9-12 19:14:44

邪恶海盗 发表于 2023-9-12 16:05
想到一个精简办法,比如先把每个Radio的GroupID/RadioID/Text/Color/Value写到一个数组$Radios[$n]里,比如 ...

感觉选色单选框控件太多了,估计你自己都觉得繁琐
只写一组颜色控件就行了,点击电阻图示的色环>选定颜色。就跟下拉框差不多,只不过选择颜色下拉框代码较复杂,没必要,就用Label或Radio更简单。

邪恶海盗 发表于 2023-9-12 19:59:30

afan 发表于 2023-9-12 19:14
感觉选色单选框控件太多了,估计你自己都觉得繁琐
只写一组颜色控件就行了,点击电阻图示的色环>选定颜 ...

数组捣鼓成功了,定义的时候要写太多,现在可以批量把Radio都创建完成,但用case $Radio to $Radio好像不对头???

慢慢来,抓到一点学一点先...

邪恶海盗 发表于 2023-9-13 22:43:26

afan 发表于 2023-9-12 19:14
感觉选色单选框控件太多了,估计你自己都觉得繁琐
只写一组颜色控件就行了,点击电阻图示的色环>选定颜 ...

"优化"了一点,代码行数降低了一部分,不过感觉创建Radio还可以再优化,奈何一次性把所有Radios添加进数组可以创建成功,但捕捉消息会有问题,明明只有54个Radio,捕捉到Case to Case 的消息可以超过这个数量,估计是循环的时候创建Group占用了,不会玩又改回单个创建Group和Radio组了


另外还有一个问题,XP上Radio的颜色是正常的,Win7下Radio文字颜色没生效,啥情况???


#region AutoIt3Wrapper 预编译参数(常用参数)
#AutoIt3Wrapper_Icon=F:\AU3脚本\图标\hidao.ico;图标,支持EXE,DLL,ICO
#AutoIt3Wrapper_OutFile=R_Calc.exe   ;输出文件名
#AutoIt3Wrapper_OutFile_Type=exe   ;文件类型
#AutoIt3Wrapper_Compression=9;压缩等级
#AutoIt3Wrapper_UseUPX=y;使用压缩
#AutoIt3Wrapper_Res_Comment=R_Calc ;注释
#AutoIt3Wrapper_Res_Description=电阻计算器;详细信息
#AutoIt3Wrapper_Res_Fileversion=0.2.2023.913
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=;p    ;自动更新版本
#AutoIt3Wrapper_Res_LegalCopyright=邪恶海盗;版权
#endregion AutoIt3Wrapper 预编译参数(常用参数)
;#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
;#include <TabConstants.au3>
;#include <WindowsConstants.au3>
#NoTrayIcon
#region ### START Koda GUI section ### Form=f:\au3脚本\色环电阻.kxf

Dim $Radios0 = [,,,,, _
                ,,,,]
Dim $Radios1 = [,,,,, _
                ,,,,]
Dim $Radios2 = [,,,,, _
                ,,,,]
Dim $Radios3 = [,,,,, _
                ,,,,]
Dim $Radios4 = [,,,,, _
                ,,]
Dim $Radios5 = [,,,,, _
                ]
Dim $i1 = 0, $i2 = 0, $i3 = 0, $i4 = 0, $i5 = 0, $i6 = 0
Dim $RdoR
Dim $Group1_6 = ['第1位', '第2位', '第3位', '倍率(10^n)', '误差(%)', '温度系数(/℃)']

$Form1_1 = GUICreate("电阻计算器 v0.2 20230913", 640, 375)
$oeminfo = GUICtrlCreateLabel('邪恶海盗 www.hidao.org', 480, 1, 160, 18, $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 10)
GUICtrlSetColor(-1, 0x0000FF)
;GUISetFont(14)

$Tab1 = GUICtrlCreateTab(0, 0, 640, 375)
$TabSheet1 = GUICtrlCreateTabItem("色环转阻值")

$Group0 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 0, 85, 85, 275)
For $Rn1_6 = 0 To 9
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios0[$Rn1_6], 15 + 0 * 95, 105 + 25 * $Rn1_6, 75, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios0[$Rn1_6])
Next
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group1 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 1, 85, 85, 275)
For $Rn1_6 = 0 To 9
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios1[$Rn1_6], 15 + 1 * 95, 105 + 25 * $Rn1_6, 75, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios1[$Rn1_6])
Next
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group2 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 2, 85, 85, 275)
For $Rn1_6 = 0 To 9
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios2[$Rn1_6], 15 + 2 * 95, 105 + 25 * $Rn1_6, 75, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios2[$Rn1_6])
Next
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group3 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 3, 85, 85, 275)
For $Rn1_6 = 0 To 9
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios3[$Rn1_6], 15 + 3 * 95, 105 + 25 * $Rn1_6, 75, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios3[$Rn1_6])
Next
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group4 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 4, 85, 115, 275)
For $Rn1_6 = 0 To 7
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios4[$Rn1_6], 15 + 4 * 95, 105 + 25 * $Rn1_6, 105, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios4[$Rn1_6])
Next
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group5 = GUICtrlCreateGroup($Group1_6, 10 + 95 * 4 + 115 + 10, 85, 115, 275)
For $Rn1_6 = 0 To 5
        $RdoR[$Rn1_6] = GUICtrlCreateRadio($Radios5[$Rn1_6], 15 + 4 * 95 + 115 + 10, 105 + 25 * $Rn1_6, 105, 25)
        GUICtrlSetColor(-1, 0xFFFFFF)
        GUICtrlSetBkColor(-1, $Radios5[$Rn1_6])
Next
GUICtrlSetColor($RdoR, 0x000000)
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUICtrlSetState($Group2, $GUI_HIDE)
GUICtrlSetState($Group4, $GUI_HIDE)
GUICtrlSetState($Group5, $GUI_HIDE)
For $Ri = 0 To 9
        GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
        GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
        GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
Next

$Radio30 = GUICtrlCreateRadio("3环电阻", 10, 30, 90, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio40 = GUICtrlCreateRadio("4环电阻", 10, 55, 90, 25)
$Radio50 = GUICtrlCreateRadio("5环电阻", 105, 30, 90, 25)
$Radio60 = GUICtrlCreateRadio("6环电阻", 105, 55, 90, 25)
GUICtrlCreateLabel(" ", 200, 54, 10, 2, $SS_ETCHEDHORZ)
GUICtrlCreateLabel(" ", 205, 35, 180, 40, $SS_GRAYFRAME)
GUICtrlCreateLabel(" ", 385, 54, 10, 2, $SS_ETCHEDHORZ)
$Label01 = GUICtrlCreateLabel(" ", 220, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios0)
$Label02 = GUICtrlCreateLabel(" ", 245, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios1)
$Label03 = GUICtrlCreateLabel(" ", 270, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios2)
$Label04 = GUICtrlCreateLabel(" ", 295, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios3)
$Label05 = GUICtrlCreateLabel(" ", 330, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios4)
$Label06 = GUICtrlCreateLabel(" ", 355, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios5)
GUICtrlSetState($Label03, $GUI_HIDE)
GUICtrlSetState($Label05, $GUI_HIDE)
GUICtrlSetState($Label06, $GUI_HIDE)

$Lab1 = GUICtrlCreateLabel("计算过程及结果", 400, 25, 230, 25, $SS_SUNKEN + $SS_CENTERIMAGE)
;GUICtrlSetFont(-1,10)
$Lab2 = GUICtrlCreateLabel("计算过程及结果", 400, 50, 230, 25, $SS_SUNKEN + $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 11)

$TabSheet4 = GUICtrlCreateTabItem("串并联阻值计算")
;GUICtrlSetState(-1, $GUI_SHOW)
$Group7 = GUICtrlCreateGroup("串联阻值计算", 10, 35, 620, 135)
$InputR11 = GUICtrlCreateInput("", 24, 55, 80, 25, $SS_CENTER)
$Combo11 = GUICtrlCreateCombo("", 104, 55, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR12 = GUICtrlCreateInput("", 174, 55, 80, 25, $SS_CENTER)
$Combo12 = GUICtrlCreateCombo("", 254, 55, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
GUICtrlCreateLabel("=", 314, 55, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR13 = GUICtrlCreateInput("?", 354, 55, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonR1 = GUICtrlCreateButton("计算", 500, 54, 65, 25)

$InputR21 = GUICtrlCreateInput("", 24, 95, 80, 25, $SS_CENTER)
$Combo21 = GUICtrlCreateCombo("", 104, 95, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR22 = GUICtrlCreateInput("?", 174, 95, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("=", 314, 97, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR23 = GUICtrlCreateInput("", 354, 95, 80, 25, $SS_CENTER )
$Combo23 = GUICtrlCreateCombo("", 434, 95, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$ButtonR2 = GUICtrlCreateButton("计算", 500, 94, 65, 25)
GUICtrlCreateLabel('原理: R1 + R2 = R3', 30, 140, 200, 25)
GUICtrlSetFont(-1, 12)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group8 = GUICtrlCreateGroup("并联阻值计算", 10, 180, 620, 135)
$InputR31 = GUICtrlCreateInput("", 24, 200, 80, 25, $SS_CENTER )
$Combo31 = GUICtrlCreateCombo("", 104, 200, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR32 = GUICtrlCreateInput("", 174, 200, 80, 25, $SS_CENTER )
$Combo32 = GUICtrlCreateCombo("", 254, 200, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
GUICtrlCreateLabel("=", 314, 200, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR33 = GUICtrlCreateInput("?", 354, 200, 130, 25, $SS_CENTER )
GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonR3 = GUICtrlCreateButton("计算", 500, 200, 65, 25)

$InputR41 = GUICtrlCreateInput("", 24, 240, 80, 25, $SS_CENTER )
$Combo41 = GUICtrlCreateCombo(" ", 104, 240, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR42 = GUICtrlCreateInput("?", 174, 240, 130, 25, $SS_CENTER )
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("=", 314, 240, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR43 = GUICtrlCreateInput("", 354, 240, 80, 25, $SS_CENTER )
$Combo43 = GUICtrlCreateCombo("", 434, 240, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$ButtonR4 = GUICtrlCreateButton("计算", 500, 240, 65, 25)
GUICtrlCreateLabel('原理: 1/R1 + 1/R2 = 1/R3', 30, 285, 200, 25)
GUICtrlSetFont(-1, 12)
GUICtrlCreateTabItem("")

GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $oeminfo
                        ShellExecute('www.hidao.org')
                Case $Radio30
                        GUICtrlSetState($Label03, $GUI_HIDE)
                        GUICtrlSetState($Label05, $GUI_HIDE)
                        GUICtrlSetState($Label06, $GUI_HIDE)
                        GUICtrlSetState($Group2, $GUI_HIDE)
                        GUICtrlSetState($Group4, $GUI_HIDE)
                        GUICtrlSetState($Group5, $GUI_HIDE)
                        For $Ri = 0 To 9
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                        Next
                        _Calc()
                Case $Radio40
                        GUICtrlSetState($Label03, $GUI_HIDE)
                        GUICtrlSetState($Label05, $GUI_SHOW)
                        GUICtrlSetState($Label06, $GUI_HIDE)
                        GUICtrlSetState($Group2, $GUI_HIDE)
                        GUICtrlSetState($Group4, $GUI_SHOW)
                        GUICtrlSetState($Group5, $GUI_HIDE)
                        For $Ri = 0 To 9
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                        Next
                        For $Ri = 0 To 7
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                        Next
                        For $Ri = 0 To 5
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                        Next
                        _Calc()
                Case $Radio50
                        GUICtrlSetState($Label03, $GUI_SHOW)
                        GUICtrlSetState($Label05, $GUI_SHOW)
                        GUICtrlSetState($Label06, $GUI_HIDE)
                        GUICtrlSetState($Group2, $GUI_SHOW)
                        GUICtrlSetState($Group4, $GUI_SHOW)
                        GUICtrlSetState($Group5, $GUI_HIDE)
                        For $Ri = 0 To 9
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                        Next
                        For $Ri = 0 To 5
                                GUICtrlSetState($RdoR[$Ri], $GUI_HIDE)
                        Next
                        _Calc()
                Case $Radio60
                        GUICtrlSetState($Label03, $GUI_SHOW)
                        GUICtrlSetState($Label05, $GUI_SHOW)
                        GUICtrlSetState($Label06, $GUI_SHOW)
                        GUICtrlSetState($Group2, $GUI_SHOW)
                        GUICtrlSetState($Group4, $GUI_SHOW)
                        GUICtrlSetState($Group5, $GUI_SHOW)
                        For $Ri = 0 To 9
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                        Next
                        For $Ri = 0 To 5
                                GUICtrlSetState($RdoR[$Ri], $GUI_SHOW)
                        Next
                        _Calc()
                Case $RdoR To $RdoR
                        $i1 = $nMsg - $RdoR
                        ;MsgBox(0,'',$Radios0[$i1])
                        _Calc()
                Case $RdoR To $RdoR
                        $i2 = $nMsg - $RdoR
                        ;MsgBox(0,'',$Radios1[$i2])
                        _Calc()
                Case $RdoR To $RdoR
                        $i3 = $nMsg - $RdoR
                        _Calc()
                Case $RdoR To $RdoR
                        $i4 = $nMsg - $RdoR
                        _Calc()
                Case $RdoR To $RdoR
                        $i5 = $nMsg - $RdoR
                        _Calc()
                Case $RdoR To $RdoR
                        $i6 = $nMsg - $RdoR
                        _Calc()

                Case $ButtonR1
                        $R11 = GUICtrlRead($InputR11) * _Rx(GUICtrlRead($Combo11))
                        $R12 = GUICtrlRead($InputR12) * _Rx(GUICtrlRead($Combo12))
                        GUICtrlSetData($InputR13, _R2R($R11 + $R12))
                Case $ButtonR2
                        $R21 = GUICtrlRead($InputR21) * _Rx(GUICtrlRead($Combo21))
                        $R23 = GUICtrlRead($InputR23) * _Rx(GUICtrlRead($Combo23))
                        GUICtrlSetData($InputR22, _R2R($R23 - $R21))
                Case $ButtonR3
                        $R31 = GUICtrlRead($InputR31) * _Rx(GUICtrlRead($Combo31))
                        $R32 = GUICtrlRead($InputR32) * _Rx(GUICtrlRead($Combo32))
                        GUICtrlSetData($InputR33, _R2R(($R31 * $R32) / ($R31 + $R32)))
                Case $ButtonR4
                        $R41 = GUICtrlRead($InputR41) * _Rx(GUICtrlRead($Combo41))
                        $R43 = GUICtrlRead($InputR43) * _Rx(GUICtrlRead($Combo43))
                        GUICtrlSetData($InputR42, _R2R(($R41 * $R43) / ($R41 - $R43)))
        EndSwitch
WEnd

Func _Calc()
        GUICtrlSetBkColor($Label01, $Radios0[$i1])
        GUICtrlSetBkColor($Label02, $Radios1[$i2])
        GUICtrlSetBkColor($Label03, $Radios2[$i3])
        GUICtrlSetBkColor($Label04, $Radios3[$i4])
        GUICtrlSetBkColor($Label05, $Radios4[$i5])
        GUICtrlSetBkColor($Label06, $Radios5[$i6])
        If GUICtrlRead($Radio30) = $GUI_CHECKED Then
                $Var1 = ' ' & $Radios0[$i1] & ' ' & $Radios1[$i2] & ' ×10^' & $Radios3[$i4] & " = "
                $Var2 = ($Radios0[$i1] & $Radios1[$i2]) * 10 ^ $Radios3[$i4]
                $Var3 = '(误差:' & '±20' & '%)'
                ;GUICtrlSetData($Label1, ' ' & $i1 & ' ' & $i2 & ' ×10^' & $i4 & " = " & ($i1 & $i2) * 10 ^ $i4 & " Ω" & ' (误差:' & '20%' & ')')
        EndIf
        If GUICtrlRead($Radio40) = $GUI_CHECKED Then
                $Var1 = ' ' & $Radios0[$i1] & ' ' & $Radios1[$i2] & ' ×10^' & $Radios3[$i4] & " = "
                $Var2 = ($Radios0[$i1] & $Radios1[$i2]) * 10 ^ $Radios3[$i4]
                $Var3 = '(误差:' & $Radios4[$i5] & '%)'
                ;GUICtrlSetData($Label1, ' ' & $i1 & ' ' & $i2 & ' ×10^' & $i4 & " = " & ($i1 & $i2) * 10 ^ $i4 & " Ω" & ' (误差:' & $i5 & ')')
        EndIf
        If GUICtrlRead($Radio50) = $GUI_CHECKED Then
                $Var1 = ' ' & $Radios0[$i1] & ' ' & $Radios1[$i2] & ' ' & $Radios2[$i3] & ' ×10^' & $Radios3[$i4] & " = "
                $Var2 = ($Radios0[$i1] & $Radios1[$i2] & $Radios2[$i3]) * 10 ^ $Radios3[$i4]
                $Var3 = '(误差:' & $Radios4[$i5] & '%)'
                ;GUICtrlSetData($Label1, ' ' & $i1 & ' ' & $i2 & ' ' & $i3 & ' ×10^' & $i4 & " = " & ($i1 & $i2 & $i3) * 10 ^ $i4 & " Ω" & ' (误差:' & $i5 & ')')
        EndIf
        If GUICtrlRead($Radio60) = $GUI_CHECKED Then
                $Var1 = ' ' & $Radios0[$i1] & ' ' & $Radios1[$i2] & ' ' & $Radios2[$i3] & ' ×10^' & $Radios3[$i4] & " = "
                $Var2 = ($Radios0[$i1] & $Radios1[$i2] & $Radios2[$i3]) * 10 ^ $Radios3[$i4]
                $Var3 = '(误差:' & $Radios4[$i5] & '% 温度系数:' & $Radios5[$i6] & ')'
                ;GUICtrlSetData($Label1, ' ' & $i1 & ' ' & $i2 & ' ' & $i3 & ' ×10^' & $i4 & " = " & ($i1 & $i2 & $i3) * 10 ^ $i4 & " Ω" & ' (误差:±' & $i5 & ',温度:)' & $i6)
        EndIf
       
        GUICtrlSetData($Lab1, $Var1 & _R2R($Var2))
        GUICtrlSetData($Lab2, $Var3)
EndFunc   ;==>_Calc

Func _R2R($R2R)
        Switch $R2R
                Case $R2R < 999
                        Return Round($R2R, 2) & ' Ω'
                Case 1000 To 999999
                        Return Round($R2R / 1000, 2) & " KΩ"
                Case Else
                        Return Round($R2R / 1000000, 2) & " MΩ"
        EndSwitch
EndFunc   ;==>_R2R

Func _Rx($Rx)
        Switch $Rx
                Case 'Ω'
                        Return 1
                Case 'KΩ'
                        Return 1000
                Case 'MΩ'
                        Return 1000000
        EndSwitch
EndFunc   ;==>_Rx



afan 发表于 2023-9-13 23:12:23

本帖最后由 afan 于 2023-9-13 23:23 编辑

邪恶海盗 发表于 2023-9-13 22:43
"优化"了一点,代码行数降低了一部分,不过感觉创建Radio还可以再优化,奈何一次性把所有Radios添加进数组可 ...
你的XP是经典主题,没有高级样式,才能设置文本颜色,否则也不行。当然也可以调用 uxtheme.dll 的 SetThemeAppProperties 函数设置窗口主题,只是控件较多,创建顺序要安排好,否则不一定好看或不符合预期。
Case to Case 估计你是没理解数组的定义,你要设计好具体的维数意义,一般不会这样跨行列to...

邪恶海盗 发表于 2023-9-14 00:25:51

本帖最后由 邪恶海盗 于 2023-9-14 00:27 编辑

afan 发表于 2023-9-13 23:12
你的XP是经典主题,没有高级样式,才能设置文本颜色,否则也不行。当然也可以调用 uxtheme.dll 的 SetThe ...

dim $Radios=
dim $Radio
for $Group = 0 to5
guictrlctreategroup($Group)

if $Radios[$Group]=$Group then


for $Radion = 0 to 53
$Radio[$Group][$Radion]=guictrlcreateradio($Radio[$Radion])或者$Radio[$Radion]=
guictrlsetbkcolor(-1,$Radios[$Radion]
endif
guictrlctreategroup('')

case $Radio to $Radio或者case $Radio to $Radio
这两种好像都不对,捕捉到的消息值都大于54...


afan 发表于 2023-9-14 09:35:49

邪恶海盗 发表于 2023-9-14 00:25
dim $Radios=
dim $Radio
for $Group = 0 to5


你这已经进入死胡同了,帮你改或解释会很费时费力。主题问题早已解决,结贴吧,剩下的自己慢慢研究。

邪恶海盗 发表于 2023-9-14 11:18:04

afan 发表于 2023-9-14 09:35
你这已经进入死胡同了,帮你改或解释会很费时费力。主题问题早已解决,结贴吧,剩下的自己慢慢研究。

:face (30):

邪恶海盗 发表于 2023-9-14 22:28:25

afan 发表于 2023-9-14 09:35
你这已经进入死胡同了,帮你改或解释会很费时费力。主题问题早已解决,结贴吧,剩下的自己慢慢研究。

搞定了,原来是循环创建Group,然后在Group循环中创建Radio,这样导致Radio控件不是连续的所以消息ID不连续,现在改成了先循环创建Group,完成后再循环创建Radio并且隐藏分区,这样Radio就是连续的了...


#region AutoIt3Wrapper 预编译参数(常用参数)
#AutoIt3Wrapper_Icon=F:\AU3脚本\图标\hidao.ico;图标,支持EXE,DLL,ICO
#AutoIt3Wrapper_OutFile=R_Calc.exe   ;输出文件名
#AutoIt3Wrapper_OutFile_Type=exe   ;文件类型
#AutoIt3Wrapper_Compression=9;压缩等级
#AutoIt3Wrapper_UseUPX=y;使用压缩
#AutoIt3Wrapper_Res_Comment=R_Calc ;注释
#AutoIt3Wrapper_Res_Description=电阻计算器;详细信息
#AutoIt3Wrapper_Res_Fileversion=0.3.2023.914
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=;p    ;自动更新版本
#AutoIt3Wrapper_Res_LegalCopyright=邪恶海盗;版权
#endregion AutoIt3Wrapper 预编译参数(常用参数)
;#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
;#include <TabConstants.au3>
;#include <WindowsConstants.au3>
#NoTrayIcon
#region ### START Koda GUI section ### Form=f:\au3脚本\色环电阻.kxf

Dim $Radios = [,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,,,, _
                ,,, _
                ,,,,, _
                ]
Dim $i = 0, $i1 = 0, $i2 = 0, $i3 = 0, $i4 = 0, $i5 = 0, $i6 = 0
Dim $RdoR
Dim $Group1_6 = ['第1位', '第2位', '第3位', '倍率(10^n)', '误差(%)', '温度系数(/℃)']
Dim $Group
Dim $left, $width

$Form1_1 = GUICreate("电阻计算器 v0.2 20230913", 640, 375)
$oeminfo = GUICtrlCreateLabel('邪恶海盗 www.hidao.org', 480, 1, 160, 18, $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 10)
GUICtrlSetColor(-1, 0x0000FF)
;GUISetFont(14)

$Tab1 = GUICtrlCreateTab(0, 0, 640, 375)
$TabSheet1 = GUICtrlCreateTabItem("色环转阻值")

$Radio30 = GUICtrlCreateRadio("3环电阻", 10, 30, 90, 25)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio40 = GUICtrlCreateRadio("4环电阻", 10, 55, 90, 25)
$Radio50 = GUICtrlCreateRadio("5环电阻", 105, 30, 90, 25)
$Radio60 = GUICtrlCreateRadio("6环电阻", 105, 55, 90, 25)
GUICtrlCreateLabel(" ", 200, 54, 10, 2, $SS_ETCHEDHORZ)
GUICtrlCreateLabel(" ", 205, 35, 180, 40, $SS_GRAYFRAME)
GUICtrlCreateLabel(" ", 385, 54, 10, 2, $SS_ETCHEDHORZ)
$Label01 = GUICtrlCreateLabel(" ", 220, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
$Label02 = GUICtrlCreateLabel(" ", 245, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
$Label03 = GUICtrlCreateLabel(" ", 270, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
$Label04 = GUICtrlCreateLabel(" ", 295, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
$Label05 = GUICtrlCreateLabel(" ", 330, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
$Label06 = GUICtrlCreateLabel(" ", 355, 34, 15, 42, $SS_SUNKEN)
GUICtrlSetBkColor(-1, $Radios)
GUICtrlSetState($Label03, $GUI_HIDE)
GUICtrlSetState($Label05, $GUI_HIDE)
GUICtrlSetState($Label06, $GUI_HIDE)

$Lab1 = GUICtrlCreateLabel("计算过程及结果", 400, 25, 230, 25, $SS_SUNKEN + $SS_CENTERIMAGE)
;GUICtrlSetFont(-1,10)
$Lab2 = GUICtrlCreateLabel("计算过程及结果", 400, 50, 230, 25, $SS_SUNKEN + $SS_CENTERIMAGE)
GUICtrlSetFont(-1, 11)

For $GpID = 0 To 5
        _Left_Width()
        $Group[$GpID] = GUICtrlCreateGroup($Group1_6[$GpID], 10 + $left, 85, $width + 10, 275);创建所有Group
        ;GUICtrlCreateGroup("", -99, -99, 1, 1)
Next

For $GpID = 0 To 5
        _Left_Width()
        GUIStartGroup()
        For $R_ID = 0 To 53
                If $GpID = $Radios[$R_ID] Then
                        $RdoR[$R_ID] = GUICtrlCreateRadio($Radios[$R_ID], 15 + $left, 105 + 25 * $Radios[$R_ID], $width, 25);创建所有Radio
                        If $Radios[$R_ID] = 0 Then GUICtrlSetState(-1, $GUI_CHECKED)
                        GUICtrlSetColor(-1, 0xFFFFFF)
                        GUICtrlSetBkColor(-1, $Radios[$R_ID])
                EndIf
        Next
        GUIStartGroup()
        ;GUICtrlCreateGroup("", -99, -99, 1, 1)
Next
_GroupAndRadio_Control($GUI_HIDE, $GUI_HIDE, $GUI_HIDE)

$TabSheet4 = GUICtrlCreateTabItem("串并联阻值计算")
;GUICtrlSetState(-1, $GUI_SHOW)
$Group7 = GUICtrlCreateGroup("串联阻值计算", 10, 35, 620, 135)
$InputR11 = GUICtrlCreateInput("", 24, 55, 80, 25, $SS_CENTER)
$Combo11 = GUICtrlCreateCombo("", 104, 55, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR12 = GUICtrlCreateInput("", 174, 55, 80, 25, $SS_CENTER)
$Combo12 = GUICtrlCreateCombo("", 254, 55, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
GUICtrlCreateLabel("=", 314, 55, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR13 = GUICtrlCreateInput("?", 354, 55, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonR1 = GUICtrlCreateButton("计算", 500, 54, 65, 25)

$InputR21 = GUICtrlCreateInput("", 24, 95, 80, 25, $SS_CENTER)
$Combo21 = GUICtrlCreateCombo("", 104, 95, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR22 = GUICtrlCreateInput("?", 174, 95, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("=", 314, 97, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR23 = GUICtrlCreateInput("", 354, 95, 80, 25, $SS_CENTER)
$Combo23 = GUICtrlCreateCombo("", 434, 95, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$ButtonR2 = GUICtrlCreateButton("计算", 500, 94, 65, 25)
GUICtrlCreateLabel('原理: R1 + R2 = R3', 30, 140, 200, 25)
GUICtrlSetFont(-1, 12)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group8 = GUICtrlCreateGroup("并联阻值计算", 10, 180, 620, 135)
$InputR31 = GUICtrlCreateInput("", 24, 200, 80, 25, $SS_CENTER)
$Combo31 = GUICtrlCreateCombo("", 104, 200, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR32 = GUICtrlCreateInput("", 174, 200, 80, 25, $SS_CENTER)
$Combo32 = GUICtrlCreateCombo("", 254, 200, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
GUICtrlCreateLabel("=", 314, 200, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR33 = GUICtrlCreateInput("?", 354, 200, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
$ButtonR3 = GUICtrlCreateButton("计算", 500, 200, 65, 25)

$InputR41 = GUICtrlCreateInput("", 24, 240, 80, 25, $SS_CENTER)
$Combo41 = GUICtrlCreateCombo(" ", 104, 240, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$InputR42 = GUICtrlCreateInput("?", 174, 240, 130, 25, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("=", 314, 240, 25, 25, $SS_CENTER + $SS_CENTERIMAGE)
$InputR43 = GUICtrlCreateInput("", 354, 240, 80, 25, $SS_CENTER)
$Combo43 = GUICtrlCreateCombo("", 434, 240, 50, 25)
GUICtrlSetData(-1, "Ω|KΩ|MΩ", 'Ω')
$ButtonR4 = GUICtrlCreateButton("计算", 500, 240, 65, 25)
GUICtrlCreateLabel('原理: 1/R1 + 1/R2 = 1/R3', 30, 285, 200, 25)
GUICtrlSetFont(-1, 12)
GUICtrlCreateTabItem("")

GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $oeminfo
                        ShellExecute('www.hidao.org')
                Case $Radio30
                        _GroupAndRadio_Control($GUI_HIDE, $GUI_HIDE, $GUI_HIDE)
                        _Calc()
                Case $Radio40
                        _GroupAndRadio_Control($GUI_HIDE, $GUI_SHOW, $GUI_HIDE)
                        _Calc()
                Case $Radio50
                        _GroupAndRadio_Control($GUI_SHOW, $GUI_SHOW, $GUI_HIDE)
                        _Calc()
                Case $Radio60
                        _GroupAndRadio_Control($GUI_SHOW, $GUI_SHOW, $GUI_SHOW)
                        _Calc()
                Case $RdoR To $RdoR
                        ;MsgBox(0, '', $nMsg - $RdoR)
                        _Calc()
                Case $ButtonR1
                        $R11 = GUICtrlRead($InputR11) * _Rx(GUICtrlRead($Combo11))
                        $R12 = GUICtrlRead($InputR12) * _Rx(GUICtrlRead($Combo12))
                        GUICtrlSetData($InputR13, _R2R($R11 + $R12))
                Case $ButtonR2
                        $R21 = GUICtrlRead($InputR21) * _Rx(GUICtrlRead($Combo21))
                        $R23 = GUICtrlRead($InputR23) * _Rx(GUICtrlRead($Combo23))
                        GUICtrlSetData($InputR22, _R2R($R23 - $R21))
                Case $ButtonR3
                        $R31 = GUICtrlRead($InputR31) * _Rx(GUICtrlRead($Combo31))
                        $R32 = GUICtrlRead($InputR32) * _Rx(GUICtrlRead($Combo32))
                        GUICtrlSetData($InputR33, _R2R(($R31 * $R32) / ($R31 + $R32)))
                Case $ButtonR4
                        $R41 = GUICtrlRead($InputR41) * _Rx(GUICtrlRead($Combo41))
                        $R43 = GUICtrlRead($InputR43) * _Rx(GUICtrlRead($Combo43))
                        GUICtrlSetData($InputR42, _R2R(($R41 * $R43) / ($R41 - $R43)))
        EndSwitch
WEnd

Func _Calc()
        For $i = 0 To 9
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label01, $Radios[$i])
                        $i1 = $Radios[$i]
                EndIf
        Next
        For $i = 10 To 19
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label02, $Radios[$i])
                        $i2 = $Radios[$i]
                EndIf
        Next
        For $i = 20 To 29
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label03, $Radios[$i])
                        $i3 = $Radios[$i]
                EndIf
        Next
        For $i = 30 To 39
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label04, $Radios[$i])
                        $i4 = $Radios[$i]
                EndIf
        Next
        For $i = 40 To 47
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label05, $Radios[$i])
                        $i5 = $Radios[$i]
                EndIf
        Next
        For $i = 48 To 53
                If GUICtrlRead($RdoR[$i]) = $GUI_CHECKED Then
                        GUICtrlSetBkColor($Label06, $Radios[$i])
                        $i6 = $Radios[$i]
                EndIf
        Next

        If GUICtrlRead($Radio30) = $GUI_CHECKED Then
                $Var1 = ' ' & $i1 & ' ' & $i2 & ' ×10^' & $i4 & " = "
                $Var2 = ($i1 & $i2) * (10 ^ $i4)
                $Var3 = '(误差:' & '±20' & '%)'
        EndIf
        If GUICtrlRead($Radio40) = $GUI_CHECKED Then
                $Var1 = ' ' & $i1 & ' ' & $i2 & ' ×10^' & $i4 & " = "
                $Var2 = $i1 & $i2 * (10 ^ $i4)
                $Var3 = '(误差:' & $i5 & '%)'
        EndIf
        If GUICtrlRead($Radio50) = $GUI_CHECKED Then
                $Var1 = ' ' & $i1 & ' ' & $i2 & ' ' & $i3 & ' ×10^' & $i4 & " = "
                $Var2 = ($i1 & $i2 & $i3) * (10 ^ $i4)
                $Var3 = '(误差:' & $i5 & '%)'
        EndIf
        If GUICtrlRead($Radio60) = $GUI_CHECKED Then
                $Var1 = ' ' & $i1 & ' ' & $i2 & ' ' & $i3 & ' ×10^' & $i4 & " = "
                $Var2 = ($i1 & $i2 & $i3) * (10 ^ $i4)
                $Var3 = '(误差:' & $i5 & '% 温度系数:' & $i6 & ')'
        EndIf

        GUICtrlSetData($Lab1, $Var1 & _R2R($Var2))
        GUICtrlSetData($Lab2, $Var3)

EndFunc   ;==>_Calc

Func _R2R($R2R)
        Switch $R2R
                Case $R2R < 999
                        Return Round($R2R, 2) & ' Ω'
                Case 1000 To 999999
                        Return Round($R2R / 1000, 2) & " KΩ"
                Case Else
                        Return Round($R2R / 1000000, 2) & " MΩ"
        EndSwitch
EndFunc   ;==>_R2R

Func _Rx($Rx)
        Switch $Rx
                Case 'Ω'
                        Return 1
                Case 'KΩ'
                        Return 1000
                Case 'MΩ'
                        Return 1000000
        EndSwitch
EndFunc   ;==>_Rx

Func _Left_Width()
        If $GpID < 4 Then
                $left = 85 * $GpID
                $width = 85 - 10
        Else
                $left = 85 * 4 + 115 * ($GpID - 4)
                $width = 115 - 10
        EndIf
EndFunc   ;==>_Left_Width

Func _GroupAndRadio_Control($GR2, $GR4, $GR5)
        GUICtrlSetState($Label03, $GR2)
        GUICtrlSetState($Label05, $GR4)
        GUICtrlSetState($Label06, $GR5)
        GUICtrlSetState($Group, $GR2)
        GUICtrlSetState($Group, $GR4)
        GUICtrlSetState($Group, $GR5)
        For $Ri = 20 To 29
                GUICtrlSetState($RdoR[$Ri], $GR2)
        Next
        For $Ri = 40 To 47
                GUICtrlSetState($RdoR[$Ri], $GR4)
        Next
        For $Ri = 48 To 53
                GUICtrlSetState($RdoR[$Ri], $GR5)
        Next
EndFunc   ;==>_GroupAndRadio_Control


rdw167 发表于 2023-9-15 20:15:25

; 导入必要的库文件
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; 创建GUI窗口和组件
$Form1 = GUICreate("Form1", 432, 145, 192, 114)
$Input1 = GUICtrlCreateInput("", 56, 112, 97, 21)

; 定义数组
Local $aRadioLabels = ["Radio1", "Radio2", "Radio3", "Radio4"]
Local $aRadioValues = ["a", "b", "c", "d"]
Local $aRadio

For $i = 0 To 3
    $aRadio[$i] = GUICtrlCreateRadio($aRadioLabels[$i], 40 + $i * 89, 32, 81, 25)
Next

GUISetState(@SW_SHOW)

; 主循环,监听GUI事件
While 1
    $nMsg = GUIGetMsg()
    If $nMsg = $GUI_EVENT_CLOSE Then Exit
    For $i = 0 To 3
      If $nMsg = $aRadio[$i] Then GUICtrlSetData($Input1, $aRadioValues[$i])
    Next
WEnd
页: 1 [2]
查看完整版本: 一组单选框选中即执行命令,如何简单点写?