We! 发表于 2010-4-1 12:08:29

GUI 內部控件由外部文件產生。[已解決]

本帖最后由 We! 于 2010-4-13 02:59 编辑

最佳答案 #21 afan 前輩所寫。

GUI 有辦法從外部文件產生所需要的控件嗎?

例如我有一個 INI 文件


name=▉▉▉▉▉▉.ini
ip=127.0.0.1

要產生控件為$Group1 = GUICtrlCreateGroup("", 0, -6, 271, 29)
$Label1 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 4, 129, 17)
GUICtrlSetOnEvent(-1, "Label1Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)文件內容有二個控件需要產生,就成為下面的代碼,Opt("GUIOnEventMode", 1)
$Form1_1 = GUICreate("Form1", 273, 507, 375, 173)
GUISetFont(10, 400, 0, "新細明體")

$Group1 = GUICtrlCreateGroup("", 0, -6, 271, 29)
$Label1 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 4, 129, 17)
GUICtrlSetOnEvent(-1, "Label1Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group2 = GUICtrlCreateGroup("", 0, 18, 271, 29)
$Label2 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 28, 129, 17)
GUICtrlSetOnEvent(-1, "Label2Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "_GuiEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "_GuiEvents")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
Sleep(100)
WEnd

Func _GuiEvents()
        Switch @GUI_CtrlId
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_MINIMIZE
                       
                Case $GUI_EVENT_RESTORE
                       
        EndSwitch
EndFunc

EndFunc
Func Label1Click()

EndFunc
Func Label2Click()一次可能需要產生一百多個控件,最好是能也有 Func 。

幻想如下$Group1 = GUICtrlCreateGroup("", 0, -6, 271, 29)
$Label1 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 4, 129, 17)
GUICtrlSetOnEvent(-1, "Label1Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group2 = GUICtrlCreateGroup("", 0, 18, 271, 29)
$Label2 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 28, 129, 17)
GUICtrlSetOnEvent(-1, "Label2Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group3 = GUICtrlCreateGroup("", 0, 30, 271, 29)
$Label3 = GUICtrlCreateLabel("▉▉▉▉▉▉", 4, 52, 129, 17)
GUICtrlSetOnEvent(-1, "Label3Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group4 = GUICtrlCreateGroup("", 0, 42, 271, 29)
.......
.......
.......

$Group5 .......
.......
.......
.......

$Group155 .......
.......
.......
.......

EndFunc
Func Label1Click()

EndFunc
Func Label2Click()
       
EndFunc
Func Label3Click()
       
EndFunc
Func Label4Click()

EndFunc
Func Label5Click()還是要先寫入一個新的 AU3,再做 UPX ?

想不到有什麼好的方法 Onz...

3mile 发表于 2010-4-1 12:19:49

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <GuiButton.au3>
#include <GuiImageList.au3>


#include <array.au3>
#Region ### START Koda GUI section ### Form=d:\01.18传送\2.1\softins\form1.kxf
$ini_file = @ScriptDir & "\softinst.ini"
$Form1 = GUICreate("Form1", 733, 547, 190, 122)
;$hImage = _GUIImageList_Create(32,32,5,3)
Local $hImage, $listview, $Button, $a;, $ico

$Group1 = GUICtrlCreateGroup("Group1", 0, 96, 145, 330)
Dim $high = 36, $shigh = 60, $all = IniReadSection($ini_file, "type")
;_ArrayDisplay($all)
For $p2 = 0 To 8
        If $p2 = 0 Then
                $Button[$p2] = GUICtrlCreateButton("全部软件", 0, Number($shigh + $high), 145, 36, $BS_ICON)
                $hImage = _GUIImageList_Create(32, 32, 5, 3)
                _GUIImageList_AddIcon($hImage, @ScriptDir & "\SORT\" & $p2 & ".ico", 0, True)
                _GUICtrlButton_SetImageList($Button[$p2], $hImage)
                $shigh += 36
        Else
                $Button[$p2] = GUICtrlCreateButton($all[$p2], 0, Number($shigh + $high), 145, 36, $BS_ICON)
                $hImage = _GUIImageList_Create(32, 32, 5, 3)
                _GUIImageList_AddIcon($hImage, @ScriptDir & "\SORT\" & $p2 & ".ico", 0, True)
                _GUICtrlButton_SetImageList($Button[$p2], $hImage)
                $shigh += 36
        EndIf
Next
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState()
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
        EndSwitch
WEnd
softinst.ini文件内容如下
1=系统辅助
2=影音图像
3=办公应用
4=光盘磁盘
5=上传下载
6=聊天游戏
7=网络浏览
8=实用软件

We! 发表于 2010-4-1 12:30:17

先謝謝樓上!

我試了一下,得到一個 Error_GUIImageList_AddIcon: -1還有想請問一下 _GUIImageList_AddIcon 的作用,

幫助看不太懂 Onz...


_GUIImageList_AddIcon
-------------------------------------------------- ------------------------------

添加一個圖標到一個圖像列表

#包括<GuiImageList.au3>
_GUIImageList_AddIcon($的hWnd,$ sFile [,$ iIndex = 0 [$ fLarge =假])

參數

$的hWnd句柄控制
$ sFile的圖標的路徑包含圖像
$ iIndex指定的從零開始的索引中提取的圖標
$ fLarge提取大圖標

翻譯的鬼東西

:face (3):

afan 发表于 2010-4-1 12:36:26

回复 3# We!


    你们那普遍E文都比较好吧,呵呵

afan 发表于 2010-4-1 12:38:56

产生的控件要能响应点击最好使用数组创建控件

We! 发表于 2010-4-1 12:43:24

不一定。

我上學的時候都是、玩! 打架! 把馬子! 等等... 這樣帶過的!

而已我也只到高中而已。

再說我這個年紀的,也是國中二年紀才開始真正學到...

又還是最愛玩的時候,沒多久又都還回去給老師了!

如果早知道! 早知道! 我就... :face (18):

We! 发表于 2010-4-1 12:50:16

再問一下!

現在 QQ 是不開放臺灣申請了嗎?

二年前的號碼上不去了

:face (36):

afan 发表于 2010-4-1 12:51:50

呵呵,看来和我差不多哦~

回归正题。

要注意ini的写法如何才能使用方便
按你的意思,需要分别按组产生?


name=▉▉▉▉▉▉
ip=127.0.0.1


name=▉▉▉▉▉
ip=127.0.0.x

然后用IniReadSectionNames() 读取所有的组名,循环读取字段名产生Group控件的同时再IniReadSection()循环读取产生组内控件即可

BTW,我不玩QQ,所以不清楚……

We! 发表于 2010-4-1 12:53:44

产生的控件要能响应点击最好使用数组创建控件
afan 发表于 2010-4-1 12:38 http://www.autoitx.com/images/common/back.gif

不懂!

能給個方向嗎?

會不會很高深 :|

We! 发表于 2010-4-1 12:56:47

呵呵,看来和我差不多哦~

回归正题。

要注意ini的写法如何才能使用方便
按你的意思,需要分别按组产 ...
afan 发表于 2010-4-1 12:51 http://www.autoitx.com/images/common/back.gif

不行,我測過了!If FileExists(@DesktopDir&"\1.txt") Then
$var = IniReadSection(@DesktopDir&"\Tests.ini", "GUI1")
If @error Then
    MsgBox(4096, "", "發生錯誤,可能 INI 檔不存在。")
Else
    For $i = 1 To $var
;~         MsgBox(4096, "", "關鍵字:" & $var[$i] & @CRLF & "數值:" & $var[$i])
                MsgBox(4096, "", "數值:" & $var[$i])
    Next
EndIf
Else
    MsgBox(4096,"檔案", "2不存在。")
        Exit
EndIf還是我的方法不對?

PS: 是唷! 我還以為高手都會有一個 QQ 號!

BTW 是什麼 0.0

afan 发表于 2010-4-1 13:08:14

Opt("GUIOnEventMode", 1)

GUICreate('test', 450, 300)
GUISetOnEvent(-3, "zx")
GUICtrlCreateTab(10, 10, 430, 280)
Dim $sl =
For $x = 1 To 5
        GUICtrlCreateTabItem('TabSheet' & $x)
        For $i = 1 To $sl[$x]
                Assign('CHECKBOX' & $x & '_' & $i, GUICtrlCreateCheckbox('', 16 + 150 * ($i - (Int(($i - 1) / 3) * 3 + 1)), 36 + 25 * Int(($i - 1) / 3), 85, 18))
                GUICtrlSetData(Eval('CHECKBOX' & $x & '_' & $i), $x & '_' & $i & '_' & Random(100, 100000, 1))
                GUICtrlSetOnEvent(Eval('CHECKBOX' & $x & '_' & $i), "zx")
        Next
Next
GUICtrlCreateTabItem('')
GUISetState()

While 1
        Sleep(1)
WEnd

Func zx()
        If @GUI_CtrlId = -3 Then Exit
        For $i = 1 To 30
                For $x = 1 To 5
                        Switch @GUI_CtrlId
                                Case Eval('CHECKBOX' & $x & '_' & $i)
                                        If GUICtrlRead(@GUI_CtrlId) = 1 Then MsgBox(0, 'TabSheet ' & $x, GUICtrlRead(@GUI_CtrlId, 1) & ' : CHECKED')
                                        If GUICtrlRead(@GUI_CtrlId) = 4 Then MsgBox(0, 'TabSheet ' & $x, GUICtrlRead(@GUI_CtrlId, 1) & ' : UNCHECKED')
                        EndSwitch
                Next
        Next
EndFunc   ;==>zx以前写的根据需要产生控件并响应点击的例子,没用到数组,了解下大概意思就行。
不知道你的控件到底有多少,可能界面排布会有问题,可以考虑采用Tab标签的形式

p.s, BTW = By The Way :顺便说一句

We! 发表于 2010-4-1 13:20:37

大約會有 15X 個!

我在試一下看看,謝謝。

PS: By The Way 了解!

這裡的網管有很多嗎?
(好像是叫網管,小說是這麼寫的 XD

afan 发表于 2010-4-1 13:23:43

回复 12# We!


    我现在有些蛋疼的杂事,不然就按你的要求写个例子……

p.s,网管的确有不少~

We! 发表于 2010-4-1 13:35:57

回复We!


    我现在有些蛋疼的杂事,不然就按你的要求写个例子……

p.s,网管的确有不少~
afan 发表于 2010-4-1 13:23 http://www.autoitx.com/images/common/back.gif

蛋疼 XD

我喜歡這個說法!

還會不會開一個交流區啊...

有一些問題想請教一下網管...

大陸的最大的網管公司是那一間?

遊戲更新都是用 P2P 傳的嗎?

遊戲 NP 會擋 VNC 等等... 的,大家的解決方法是什麼?

臺灣一直沒聽說過有什麼好的做法!

還有一些 免下載 的想法! 問題真多的我...

:face (7):

afan 发表于 2010-4-1 13:40:31

可以就在这个版块交流吧,这里本来就是提问交流区~
页: [1] 2
查看完整版本: GUI 內部控件由外部文件產生。[已解決]