【已解决】请教如何简化如下Switch条件选择语句代码?
本帖最后由 fengjie 于 2014-3-24 00:33 编辑问题已经解决,谢谢楼下的几位高手!简化后的代码看着就是舒服啊!
本菜鸟想请教大家一个问题,如下Switch条件选择语句代码能否简化?
代码如下:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=D:\百度云同步盘\Autoit\Checkbox_FontStyle_Test.kxf
$Form1 = GUICreate("字体设置测试", 285, 167, 192, 124)
$Checkbox1 = GUICtrlCreateCheckbox("粗体", 24, 24, 41, 17)
$Checkbox2 = GUICtrlCreateCheckbox("斜体", 80, 24, 41, 17)
$Checkbox3 = GUICtrlCreateCheckbox("下划线", 136, 24, 57, 17)
$Checkbox4 = GUICtrlCreateCheckbox("删除线", 208, 24, 57, 17)
$Button1 = GUICtrlCreateButton("查看字体代码", 96, 104, 83, 25)
$Label1 = GUICtrlCreateLabel("读取当前设置的字体", 88, 72, 112, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;初始化字体样式0=普通 1=粗体 2=斜体 4=下划线 8=删除线
$FontStyle = "0"
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Checkbox1
If BitAND(GUICtrlRead($Checkbox1), $GUI_CHECKED) = 1 Then
$FontStyle = $FontStyle + 1
Else
$FontStyle = $FontStyle - 1
EndIf
GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
Case $Checkbox2
If BitAND(GUICtrlRead($Checkbox2), $GUI_CHECKED) = 1 Then
$FontStyle = $FontStyle + 2
Else
$FontStyle = $FontStyle - 2
EndIf
GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
Case $Checkbox3
If BitAND(GUICtrlRead($Checkbox3), $GUI_CHECKED) = 1 Then
$FontStyle = $FontStyle + 4
Else
$FontStyle = $FontStyle - 4
EndIf
GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
Case $Checkbox4
If BitAND(GUICtrlRead($Checkbox4), $GUI_CHECKED) = 1 Then
$FontStyle = $FontStyle + 8
Else
$FontStyle = $FontStyle - 8
EndIf
GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
Case $Button1
MsgBox("", "字体设置测试", "当前字体设置的编号为:" & $FontStyle, 100)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
运行效果:
目的:
实时取得$FontStyle的值,并根据此值设置Label1的字体样式
问题:
以上Switch条件选择语句相关代码能否简化?如果可以,该如何修改?谢谢! 国际惯例,自己沙发 如果只是判断CheckBox的选中和不选中状态的话,判断GUICtrlRead($Checkbox1)=$GUI_CHECKED即可,BitAND方法是用于分离GUICtrlGetStatus获取的多种状态的。 #include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=D:\百度云同步盘\Autoit\SelectBox_FontStyle_Test.kxf
Local $Form1 = GUICreate("字体设置测试", 285, 167, -1, -1)
Local $Checkbox1 = GUICtrlCreateCheckbox("粗体", 24, 24, 41, 17)
Local $Checkbox2 = GUICtrlCreateCheckbox("斜体", 80, 24, 41, 17)
Local $Checkbox3 = GUICtrlCreateCheckbox("下划线", 136, 24, 57, 17)
Local $Checkbox4 = GUICtrlCreateCheckbox("删除线", 208, 24, 57, 17)
Local $Button1 = GUICtrlCreateButton("查看字体代码", 96, 104, 83, 25)
Local $Label1 = GUICtrlCreateLabel("读取当前设置的字体", 88, 72, 112, 17)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $aCheckbox[][] = [['', ''],[$Checkbox1, 1],[$Checkbox2, 2],[$Checkbox3, 4],[$Checkbox4, 8]]
$aCheckbox = UBound($aCheckbox, 1) - 1
;初始化字体样式0=普通 1=粗体 2=斜体 4=下划线 8=删除线
Local $FontStyle = 0
Local $i
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Button1
MsgBox("", "字体设置测试", "当前字体设置的编号为:" & $FontStyle, 100)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
For $i = 1 To $aCheckbox
If $nMsg = $aCheckbox[$i] Then
If GUICtrlRead($aCheckbox[$i]) = $GUI_CHECKED Then
$FontStyle = $FontStyle + $aCheckbox[$i]
Else
$FontStyle = $FontStyle - $aCheckbox[$i]
EndIf
GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
EndIf
Next
WEnd
回复 1# fengjie
可以使用函数的方式来精简代码
代码如下:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=D:\百度云同步盘\Autoit\SelectBox_FontStyle_Test.kxf
$Form1 = GUICreate("字体设置测试", 285, 167, 192, 124)
$Checkbox1 = GUICtrlCreateCheckbox("粗体", 24, 24, 41, 17)
$Checkbox2 = GUICtrlCreateCheckbox("斜体", 80, 24, 41, 17)
$Checkbox3 = GUICtrlCreateCheckbox("下划线", 136, 24, 57, 17)
$Checkbox4 = GUICtrlCreateCheckbox("删除线", 208, 24, 57, 17)
$Button1 = GUICtrlCreateButton("查看字体代码", 96, 104, 83, 25)
$Label1 = GUICtrlCreateLabel("读取当前设置的字体", 88, 72, 112, 17)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Global $FontStyle = "0"
;初始化字体样式0=普通 1=粗体 2=斜体 4=下划线 8=删除线
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Checkbox1
SetFontStyle($Label1,$Checkbox1,1)
Case $Checkbox2
SetFontStyle($Label1,$Checkbox2,2)
Case $Checkbox3
SetFontStyle($Label1,$Checkbox3,4)
Case $Checkbox4
SetFontStyle($Label1,$Checkbox4,8)
Case $Button1
MsgBox("", "字体设置测试", "当前字体设置的编号为:" & $FontStyle, 100)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func SetFontStyle($_handletoset,$_chkboxhandle=0,$_styleID=0)
;$_handle 要检测的复选框句柄 $_styleID 设置的格式
If BitAND(GUICtrlRead($_chkboxhandle), $GUI_CHECKED) = 1 Then
$FontStyle = $FontStyle + $_styleID
Else
$FontStyle = $FontStyle - $_styleID
EndIf
GUICtrlSetFont($_handletoset, 10, 400, $FontStyle, "微软雅黑")
EndFunc
不错 学习了 Case $Checkbox1
$iWeight = 400
If GUICtrlRead($Checkbox1) = 1 Then $iWeight = 800
GUICtrlSetFont($Label1, 10, $iWeight, $FontStyle, "微软雅黑") 顶顶顶顶 A大及时啊
skyfree 发表于 2014-3-23 07:13 http://www.autoitx.com/images/common/back.gif
谢谢skyfree 这就是我要的效果 回复fengjie
可以使用函数的方式来精简代码
代码如下:
lanfengc 发表于 2014-3-23 10:22 http://www.autoitx.com/images/common/back.gif
谢谢lanfengc,这个方法也很好
afan 发表于 2014-3-23 11:30 http://www.autoitx.com/images/common/back.gif
谢谢afan,多谢指点! 学习了~~~~~~~~~~~~~~~~~~
页:
[1]