要用 au3 來建立一個 GUI 程式雖然不像 VB 等程式方便,但其實也不難
檢視原始碼 AutoIt v3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #cs 版本:AutoIt v3 3.2.12.0 環境:Windows XP SP2 #ce ;載入 GUIConstants 及 WindowsConstants 的 UDF #include <GUIConstants.au3> #include <WindowsConstants.au3> ;設定變數使用前要先宣告 Opt("MustDeclareVars", 1) ;宣告變數 Local $MyWin, $Msg ;產生一個空白表單,寬高為300, 300,畫面置中 $MyWin = GUICreate("我的視窗", 300, 300, -1, -1) ;顯示視窗 GUISetState(@SW_SHOW ) While 1 ;取得GUI事件 $Msg = GUIGetMsg() Switch $Msg ;如果是關閉視窗事件時 Case $GUI_EVENT_CLOSE ;關閉程式 Exit EndSwitch WEnd |
這樣就能產生一個空白的表單了。執行時的畫面:
那如果是要建立一個沒有標題列的視窗的話呢?這只要指定其樣式為 WS_POPUPWINDOW 即可
檢視原始碼 AutoIt v3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #cs 版本:AutoIt v3 3.2.12.0 環境:Windows XP SP2 #ce ;載入 GUIConstants 及 WindowsConstants 的 UDF #include <GUIConstants.au3> #include <WindowsConstants.au3> ;設定變數使用前要先宣告 Opt("MustDeclareVars", 1) ;宣告變數 Local $MyWin, $Msg ;產生一個空白表單,寬高為300, 300,畫面置中,樣式為 pop-up window $MyWin = GUICreate("我的視窗", 300, 300, -1, -1, $WS_POPUPWINDOW) ;顯示視窗 GUISetState(@SW_SHOW ) While 1 ;取得GUI事件 $Msg = GUIGetMsg() Switch $Msg ;如果是關閉視窗事件時 Case $GUI_EVENT_CLOSE ;關閉程式 Exit EndSwitch WEnd |
但是因為沒有標題列,所以就無法移動該視窗
若是要能移動的話,則要加上對 WM_NCHITTEST 事件的處理
檢視原始碼 AutoIt v3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #cs 版本:AutoIt v3 3.2.12.0 環境:Windows XP SP2 #ce ;載入 GUIConstants 及 WindowsConstants 的 UDF #include <GUIConstants.au3> #include <WindowsConstants.au3> ;設定變數使用前要先宣告 Opt("MustDeclareVars", 1) ;宣告變數 Local $MyWin, $Msg ;產生一個空白表單,寬高為300, 300,畫面置中,樣式為 pop-up window $MyWin = GUICreate("我的視窗", 300, 300, -1, -1, $WS_POPUPWINDOW) ;顯示視窗 GUISetState(@SW_SHOW ) ;註冊 WM_NCHITTEST 事件的處理函式為 WM_NCHITTEST() GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST") While 1 ;取得GUI事件 $Msg = GUIGetMsg() Switch $Msg ;如果是關閉視窗事件時 Case $GUI_EVENT_CLOSE ;關閉程式 Exit EndSwitch WEnd ;處理 WM_NCHITTEST 事件用 Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam) if ($hWnd = $MyWin) and ($iMsg = $WM_NCHITTEST) then Return $HTCAPTION EndFunc |
這樣就能透過拖移視窗畫面來移動整個視窗了。
另外,常看到有網友問到要怎樣建立一個執行就置中的視窗,其實只要在 GUICreate 時把 left 跟 top 值設為 -1(預設) 即可。
檢視原始碼 AutoIt v3