找回密码
 加入
搜索
查看: 6304|回复: 11

[AU3基础] 【已解决】请教如何简化如下Switch条件选择语句代码?

  [复制链接]
发表于 2014-3-23 02:19:48 | 显示全部楼层 |阅读模式
本帖最后由 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条件选择语句相关代码能否简化?如果可以,该如何修改?谢谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2014-3-23 02:20:11 | 显示全部楼层
国际惯例,自己沙发
发表于 2014-3-23 07:02:19 | 显示全部楼层
如果只是判断CheckBox的选中和不选中状态的话,判断GUICtrlRead($Checkbox1)=$GUI_CHECKED即可,BitAND方法是用于分离GUICtrlGetStatus获取的多种状态的。
发表于 2014-3-23 07:13:57 | 显示全部楼层
#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[0][0] = 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[0][0]
                If $nMsg = $aCheckbox[$i][0] Then
                        If GUICtrlRead($aCheckbox[$i][0]) = $GUI_CHECKED Then
                                $FontStyle = $FontStyle + $aCheckbox[$i][1]
                        Else
                                $FontStyle = $FontStyle - $aCheckbox[$i][1]
                        EndIf
                        GUICtrlSetFont($Label1, 10, 400, $FontStyle, "微软雅黑")
                EndIf
        Next
WEnd
发表于 2014-3-23 10:22:13 | 显示全部楼层
回复 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


发表于 2014-3-23 10:42:39 | 显示全部楼层
不错     学习了
发表于 2014-3-23 11:30:40 | 显示全部楼层
                Case $Checkbox1
                        $iWeight = 400
                        If GUICtrlRead($Checkbox1) = 1 Then $iWeight = 800
                        GUICtrlSetFont($Label1, 10, $iWeight, $FontStyle, "微软雅黑")
发表于 2014-3-23 11:37:25 | 显示全部楼层
顶顶  顶顶    A大及时啊
 楼主| 发表于 2014-3-24 00:28:29 | 显示全部楼层
skyfree 发表于 2014-3-23 07:13


谢谢skyfree 这就是我要的效果
 楼主| 发表于 2014-3-24 00:29:45 | 显示全部楼层
回复  fengjie


    可以使用函数的方式来精简代码

代码如下:
lanfengc 发表于 2014-3-23 10:22


谢谢lanfengc,这个方法也很好
 楼主| 发表于 2014-3-24 00:31:00 | 显示全部楼层
afan 发表于 2014-3-23 11:30



   谢谢afan,多谢指点!
发表于 2014-3-26 22:11:47 | 显示全部楼层
学习了~~~~~~~~~~~~~~~~~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 11:22 , Processed in 0.081757 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表