【H版】子線程如何對主線程做GuiControl?可執行不可編譯?

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

au6
Posts: 7
Joined: 15 Aug 2015, 23:00

【H版】子線程如何對主線程做GuiControl?可執行不可編譯?

16 Aug 2015, 01:06

抓漫畫程式.JPG
我做了一個抓漫畫程式,想改成多線程於是產生了兩個問題

【已解決】子線程如何對主線程做GuiControl?
第42行的 GuiControl,,下載進度%列%,% 100*A_Index/圖片數量 完全無作用,沒辦法控制進度條。

可執行不可編譯?
我從官網下載的H版,安裝步驟如下:
1.先有L版,再手動安裝H版(MD版的Win32w資料夾)。
2.注意H版下載的檔案排列不整齊,請以下列順序一個個搬運資料:
 2.1) lib資料夾 丟進去L版
 2.2) 普通版和Multi-threaded Dll MD版兩者擇一,然後選Win32w資料夾裡面內容如下:
   2.2.1) AutoHotkeySC.bin 丟進去Complie資料夾覆蓋之
   2.2.2) AutoHotkey.exe 丟進去L版覆蓋之
   2.2.3) AutoHotkey.dll 丟進去腳本目錄
   2.2.4) AutoHotkeyMini.dll 丟進去腳本目錄
   2.2.5) msvcr100.dll(MD版才有) 丟進去腳本目錄
 2.3) 至於compile資料夾我覆蓋至L版會出問題,會連最基本的編譯都沒辦法,所以就沒覆蓋了。
只要用到AutoHotKey.dll,該腳本就會變得可執行不可編譯。

原腳本代碼

Code: Select all

Gui,Show,x517 y81 w613 h380
Gui,Font,Normal,Tahoma
Gui,Add,GroupBox,x18 y18 w450 h300,步驟一
Gui,Add,Text,x46 y48 w140 h18,請輸入圖片的主網址
Gui,Add,Text,x222 y48 w55 h18,圖片數量
Gui,Add,Text,x297 y48 w80 h18,存檔路徑
Gui,Add,GroupBox,x470 y18 w128 h300,步驟二
Gui,Add,Text,x484 y48 w50 h18,下載進度
Gui,Add,Button,x485 y276 w70 h23 default,開始下載                 ;按下按鈕,就會跑到"Button開始下載"的標籤位置
Loop,6                                                             ;產生6行介面
 {	
  Y座標:=40+A_Index*33  
  Gui,Add,Text,x31 y%Y座標% w10 h12,%A_Index%.
  Gui,Add,Edit,x46 y%Y座標% w150 h20 v圖片主網址%A_Index%, 
  Gui,Add,Edit,x222 y%Y座標% w50 h20 v圖片數量%A_Index%, 
  Gui,Add,Edit,x297 y%Y座標% w130 h20 v存檔路徑%A_Index%,
  Gui,Add,Button,x430 y%Y座標% w25 h20 g下載路徑,%A_Index%
  Gui,Add,Progress,x483 y%Y座標% w83 h18 -Smooth v下載進度%A_Index%   
 }
Return

下載路徑:
FileSelectFolder,存檔路徑%A_GuiControl%,%A_Desktop%,1             ;A_GuiControl這個內建變數,同一個標籤產生不同指令
GuiControl,,存檔路徑%A_GuiControl%,% 存檔路徑%A_GuiControl%       ;這邊要用GuiControl重新賦予值,雖然我也不知道為什麼
Return

Button開始下載:
Gui,Submit,NoHide                                                 ;儲存使用者輸入的資訊。要用這行命令之後才可以把Edit內的變數拿來用。1
Loop,6                                                            ;外迴圈,控制第幾列
 {
  線程%A_Index%:=AhkDllThread()  
  列:=A_Index
  線程%A_Index%.Ahkassign.圖片主網址:=圖片主網址%列%
  線程%A_Index%.Ahkassign.圖片數量:=圖片數量%列%
  線程%A_Index%.Ahkassign.存檔路徑:=存檔路徑%列%
  線程%A_Index%.Ahkassign.列:=列                                 ;這邊不能寫線程%A_Index%.Ahkassign.列:=A_Index
  命令=
    ( % Comments
     #NoTrayIcon
     Loop,% 圖片數量                                             ;內迴圈,控制該列下載幾張圖片      
      {
       GuiControl,,下載進度%列%,% 100*A_Index/圖片數量
       If A_Index <10                                             ;由於檔名是001.jpg、999.jpg這樣的連號形式,如果要順利運作必須將迴圈分3部分
        {
         URLDownloadToFile,% 圖片主網址 "00" A_Index ".jpg",% 存檔路徑 "\00" A_Index ".jpg"
         continue
        }
       else if A_Index <100
        {
         URLDownloadToFile,% 圖片主網址 "0" A_Index ".jpg",% 存檔路徑 "\0" A_Index ".jpg"
         continue
        }
       else if A_Index <1000
        {
	     URLDownloadToFile,% 圖片主網址 A_Index ".jpg",% 存檔路徑 "\" A_Index ".jpg"
	     continue
        } 
      }   
    )
  線程%A_Index%.AhkTextDll(命令) 
 }   
MsgBox,,,全部下載完畢,本視窗5秒後關閉,5
Return

GuiClose:                                                          ;按視窗右上角X時關閉程式
ExitApp
Return
修正後的腳本(感謝arcticir的幫助)

Code: Select all

Gui,Show,x517 y81 w613 h380
Gui,Font,Normal,Tahoma
Gui,Add,GroupBox,x18 y18 w450 h300,步驟一
Gui,Add,Text,x46 y48 w140 h18,請輸入圖片的主網址
Gui,Add,Text,x222 y48 w55 h18,圖片數量
Gui,Add,Text,x297 y48 w80 h18,存檔路徑
Gui,Add,GroupBox,x470 y18 w128 h300,步驟二
Gui,Add,Text,x484 y48 w50 h18,下載進度
Gui,Add,Button,x485 y276 w70 h23 default,開始下載                 ;按下按鈕,就會跑到"Button開始下載"的標籤位置
Loop,6                                                             ;產生6行介面
 {	
  Y座標:=40+A_Index*33  
  Gui,Add,Text,x31 y%Y座標% w10 h12,%A_Index%.
  Gui,Add,Edit,x46 y%Y座標% w150 h20 v圖片主網址%A_Index%, 
  Gui,Add,Edit,x222 y%Y座標% w50 h20 v圖片數量%A_Index%, 
  Gui,Add,Edit,x297 y%Y座標% w130 h20 v存檔路徑%A_Index%,
  Gui,Add,Button,x430 y%Y座標% w25 h20 g下載路徑,%A_Index%
  Gui,Add,Progress,x483 y%Y座標% w83 h18 -Smooth v下載進度%A_Index%   
 }
Return

下載路徑:
FileSelectFolder,存檔路徑%A_GuiControl%,%A_Desktop%,1             ;A_GuiControl這個內建變數,同一個標籤產生不同指令
GuiControl,,存檔路徑%A_GuiControl%,% 存檔路徑%A_GuiControl%       ;這邊要用GuiControl重新賦予值,雖然我也不知道為什麼
Return

Button開始下載:
Gui,Submit,NoHide                                                 ;儲存使用者輸入的資訊。要用這行命令之後才可以把Edit內的變數拿來用。1
Loop,6                                                            ;外迴圈,控制第幾列
 {
  線程%A_Index%:=AhkDllThread()  
  列:=A_Index
  線程%A_Index%.Ahkassign.圖片主網址:=圖片主網址%列%
  線程%A_Index%.Ahkassign.圖片數量:=圖片數量%列%
  線程%A_Index%.Ahkassign.存檔路徑:=存檔路徑%列%
  線程%A_Index%.Ahkassign.列:=列                                 ;這邊不能寫線程%A_Index%.Ahkassign.列:=A_Index
  命令=
    ( % Comments
     #NoTrayIcon
     主線程:=AhkExported()                                        ;命名主線程
     Loop,% 圖片數量                                              ;內迴圈,控制該列下載幾張圖片      
      {       
       已下載圖片數量:=A_Index
       主線程.ahkFunction("下載進度",列,已下載圖片數量,圖片數量) ;這邊要用呼叫主線程函數的方法才可以Guicontrol,另外參數名不可以是A_Index
       If A_Index <10                                              ;由於檔名是001.jpg、999.jpg這樣的連號形式,如果要順利運作必須將迴圈分3部分
        {
         URLDownloadToFile,% 圖片主網址 "00" A_Index ".jpg",% 存檔路徑 "\00" A_Index ".jpg"
         continue
        }
       else if A_Index <100
        {
         URLDownloadToFile,% 圖片主網址 "0" A_Index ".jpg",% 存檔路徑 "\0" A_Index ".jpg"
         continue
        }
       else if A_Index <1000
        {
	     URLDownloadToFile,% 圖片主網址 A_Index ".jpg",% 存檔路徑 "\" A_Index ".jpg"
	     continue
        } 
      }   
    )
  線程%A_Index%.AhkTextDll(命令) 
 }   
Return

下載進度(列,已下載圖片數量,圖片數量)                             ;控制進度條的函數
 {
  GuiControl,,下載進度%列%,% 100*已下載圖片數量/圖片數量
 }

GuiClose:                                                          ;按視窗右上角X時關閉程式
ExitApp
Return
各位好,這2個問題我在http://ahk8.com/qa/1919/ 發過了,但沒有解答故來此請教。 :wave:
Last edited by au6 on 16 Aug 2015, 08:32, edited 2 times in total.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: 【H版】子線程如何對主線程做GuiControl?可執行不可編譯?

16 Aug 2015, 03:57

这问题放着我来

在主脚本写个操作GuiControl的函数,然后在子线程调用这个函数就行了。

Code: Select all

f:=AhkExported() ;获取主进程句柄
f.ahkFunction("函数名",参数)
我都是自用,没编译过,应该很简单的吧,摸索下吧
au6
Posts: 7
Joined: 15 Aug 2015, 23:00

Re: 【H版】子線程如何對主線程做GuiControl?可執行不可編譯?

16 Aug 2015, 04:24

arcticir wrote:这问题放着我来

在主脚本写个操作GuiControl的函数,然后在子线程调用这个函数就行了。

Code: Select all

f:=AhkExported() ;获取主进程句柄
f.ahkFunction("函数名",参数)
我都是自用,没编译过,应该很简单的吧,摸索下吧
謝謝,已找到AhkExported()相關資料,晚點會試試看。
---------------------------------------------------------------
成功了,修正好的腳本請看1樓內文。
原來要這樣繞才能從子線程對主線程做GuiControl,再次謝謝! :thumbup:

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 55 guests