Skip to content

Commit 6bc6ff3

Browse files
authored
Merge pull request #50 from mlipok/patch-1
Update 2-Html_Gui.au3
2 parents 72d574b + de4456b commit 6bc6ff3

1 file changed

Lines changed: 70 additions & 90 deletions

File tree

examples/2-Html_Gui.au3

Lines changed: 70 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,83 @@
11
#AutoIt3Wrapper_UseX64=y
22
; Html_Gui.au3
3+
#include <Array.au3>
4+
#include <FileConstants.au3>
35
#include <GUIConstantsEx.au3>
46
#include <WindowsConstants.au3>
5-
#include <Array.au3>
67

78
#include "..\NetWebView2Lib.au3"
89

9-
; Global objects
10-
Global $oWebV2M, $oBridge
11-
1210
; Global variables for data management
1311
Global $aMessages[0][3]
1412
Global $sFilePath = @ScriptDir & "\messages.csv"
15-
Global $hGUI
16-
13+
Global $hGUI, $oBridge, $idBlue, $idRed
1714
Main()
15+
Exit
1816

1917
Func Main()
20-
; Local error handler for COM objects
21-
Local $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
22-
#forceref $oMyError
2318

24-
; Create GUI with resizing support
25-
$hGUI = GUICreate("WebView2 Theme Switcher", 500, 480, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
26-
GUISetBkColor(0x1E1E1E)
27-
28-
Local $idBlue = GUICtrlCreateLabel("Blue Theme", 10, 10, 100, 30)
29-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
30-
GUICtrlSetColor(-1, 0x0078D7)
31-
32-
Local $idRed = GUICtrlCreateLabel("Red Theme", 120, 10, 100, 30)
33-
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
34-
GUICtrlSetColor(-1, 0xFF0000)
35-
36-
; Get the WebView2 Manager object and register events
37-
$oWebV2M = _NetWebView2_CreateManager("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0", "__EVENTS_Manager_", "--mute-audio")
38-
39-
; ⚠️ Important: Enclose ($hGUI) in parentheses to force "Pass-by-Value".
40-
; This prevents the COM layer from changing the AutoIt variable type from Ptr to Int64.
41-
_NetWebView2_Initialize($oWebV2M, ($hGUI), "", 0, 50, 500, 400)
42-
43-
; Get the bridge object and register events
44-
$oBridge = _NetWebView2_GetBridge($oWebV2M, "__EVENTS_Bridge_")
45-
46-
; Register the WM_SIZE message to handle window resizing
47-
GUIRegisterMsg($WM_SIZE, WM_SIZE)
48-
49-
GUISetState(@SW_SHOW)
19+
_Show_Form()
5020

5121
; Main Application Loop
5222
While 1
5323
Switch GUIGetMsg()
5424
Case $GUI_EVENT_CLOSE
55-
$oWebV2M.Cleanup()
56-
Exit
25+
_NetWebView2_CleanUp($_g_oWeb, $oBridge)
26+
ExitLoop
5727

5828
Case $idBlue
5929
; Update CSS variables dynamically via JavaScript
60-
$oWebV2M.ExecuteScript("document.documentElement.style.setProperty('--accent-color', '#4db8ff');")
61-
$oWebV2M.ExecuteScript("document.documentElement.style.setProperty('--btn-color', '#0078d7');")
30+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#4db8ff');")
31+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#0078d7');")
6232

6333
Case $idRed
6434
; Update CSS variables dynamically via JavaScript
65-
$oWebV2M.ExecuteScript("document.documentElement.style.setProperty('--accent-color', '#ff4d4d');")
66-
$oWebV2M.ExecuteScript("document.documentElement.style.setProperty('--btn-color', '#d70000');")
35+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#ff4d4d');")
36+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#d70000');")
6737
EndSwitch
6838
WEnd
6939

7040
EndFunc ;==>Main
7141

72-
; Handles data received from the WebView2 Manager
73-
Func __EVENTS_Manager_OnMessageReceived($sMessage)
74-
; Local error handler for COM objects
75-
Local $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
76-
#forceref $oMyError
42+
Func _Show_Form()
43+
; Create GUI with resizing support
44+
$hGUI = GUICreate("WebView2 Theme Switcher", 500, 480, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
45+
GUISetBkColor(0x1E1E1E)
7746

78-
Local Static $bIsInitialized = False
79-
If $sMessage = "INIT_READY" And Not $bIsInitialized Then
80-
$bIsInitialized = True ; We note that we are finished.
81-
Local $sHTML = "<html><head><meta charset='UTF-8'>" & __FormCSS() & "</head><body>" & __FormHTML() & "</body></html>"
82-
$oWebV2M.NavigateToString($sHTML)
83-
$oWebV2M.SetZoom(1.2)
84-
$oWebV2M.DisableBrowserFeatures()
85-
$oWebV2M.LockWebView()
86-
GUISetState(@SW_SHOW, $hGUI)
87-
EndIf
88-
EndFunc ;==>__EVENTS_Manager_OnMessageReceived
47+
$idBlue = GUICtrlCreateLabel("Blue Theme", 10, 10, 100, 30)
48+
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
49+
GUICtrlSetColor(-1, 0x0078D7)
50+
51+
$idRed = GUICtrlCreateLabel("Red Theme", 120, 10, 100, 30)
52+
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
53+
GUICtrlSetColor(-1, 0xFF0000)
54+
55+
; Create WebView2 Manager object and register events
56+
$_g_oWeb = _NetWebView2_CreateManager("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0", "__MyEVENTS_Manager_", "")
57+
58+
; initialize browser - put it on the GUI
59+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
60+
_NetWebView2_Initialize($_g_oWeb, $hGUI, $sProfileDirectory, 0, 50, 500, 400, True, False, False, 1.1)
61+
62+
; Create bridge object and register events
63+
$oBridge = _NetWebView2_GetBridge($_g_oWeb, "__MyEVENTS_Bridge_")
64+
65+
$_g_oWeb.IsZoomControlEnabled = False
66+
$_g_oWeb.IsScrollbarEnabled = False
67+
68+
; Register the WM_SIZE message to handle window resizing
69+
GUIRegisterMsg($WM_SIZE, WM_SIZE)
70+
71+
Local $sHTML = "<html><head><meta charset='UTF-8'><style>:" & __FormCSS() & "</style></head><body>" & __FormHTML() & "</body></html>"
72+
$_g_oWeb.NavigateToString($sHTML)
73+
GUISetState(@SW_SHOW, $hGUI)
74+
75+
EndFunc ;==>_Show_Form
8976

9077
; Handles data received from the JavaScript 'postMessage'
91-
Func __EVENTS_Bridge_OnMessageReceived($sMessage)
78+
Func __MyEVENTS_Bridge_OnMessageReceived($sMessage)
9279
; Local error handler for COM objects
93-
Local $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
80+
Local $oMyError = ObjEvent("AutoIt.Error", __HtmlGUI_ErrFunc)
9481
#forceref $oMyError
9582

9683
ConsoleWrite("$sMessage=" & $sMessage & @CRLF)
@@ -113,7 +100,7 @@ Func __EVENTS_Bridge_OnMessageReceived($sMessage)
113100
_ArrayAdd($aMessages, $sName & "|" & $sEmail & "|" & $sMsg)
114101

115102
; Append data to CSV file safely
116-
Local $hFile = FileOpen($sFilePath, 9) ; 1 (Write) + 8 (Create Path)
103+
Local $hFile = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)
117104
If $hFile <> -1 Then
118105
; Clean the message string for CSV compatibility (remove line breaks)
119106
Local $sCleanMsg = StringReplace($sMsg, @CRLF, " ")
@@ -128,12 +115,12 @@ Func __EVENTS_Bridge_OnMessageReceived($sMessage)
128115
EndIf
129116
EndIf
130117
EndIf
131-
EndFunc ;==>__EVENTS_Bridge_OnMessageReceived
118+
EndFunc ;==>__MyEVENTS_Bridge_OnMessageReceived
132119

133120
; Generates the CSS block with dynamic variables
134121
Func __FormCSS()
135122
Local $sCSS = _
136-
"<style>:root {" & @CRLF & _
123+
"root {" & @CRLF & _
137124
" --bg-color: #1e1e1e;" & @CRLF & _
138125
" --form-bg: #2d2d2d;" & @CRLF & _
139126
" --accent-color: #4db8ff;" & @CRLF & _
@@ -180,12 +167,6 @@ Func __FormCSS()
180167
" width: 100%;" & @CRLF & _
181168
" font-size: 16px;" & @CRLF & _
182169
"}" & @CRLF & _
183-
"body {" & @CRLF & _
184-
" overflow: hidden;" & @CRLF & _
185-
" text-overflow: clip;" & @CRLF & _
186-
" white-space: nowrap;" & @CRLF & _
187-
"}" & @CRLF & _
188-
"</style>" & @CRLF & _
189170
""
190171
Return $sCSS
191172
EndFunc ;==>__FormCSS
@@ -219,43 +200,42 @@ EndFunc ;==>__FormHTML
219200
; Injects a temporary notification box into the web page
220201
Func ShowWebNotification($sMessage, $sBgColor = "#4CAF50", $iDuration = 3000)
221202
; Local error handler for COM objects
222-
Local $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
203+
Local $oMyError = ObjEvent("AutoIt.Error", __HtmlGUI_ErrFunc)
223204
#forceref $oMyError
224205

225206
; We use a unique ID 'autoit-notification' to find and replace existing alerts
226207
Local $sJS = _
227-
"var oldDiv = document.getElementById('autoit-notification');" & _
228-
"if (oldDiv) { oldDiv.remove(); }" & _
229-
"var div = document.createElement('div');" & _
230-
"div.id = 'autoit-notification';" & _ ; Assign the ID
231-
"div.style = 'position:fixed; top:20px; left:50%; transform:translateX(-50%); padding:15px; background:" & $sBgColor & _
232-
"; color:white; border-radius:8px; z-index:9999; font-family:sans-serif; box-shadow: 0 4px 6px rgba(0,0,0,0.2); transition: opacity 0.5s;';" & _
233-
"div.innerText = '" & $sMessage & "';" & _
234-
"document.body.appendChild(div);" & _
235-
"setTimeout(() => {" & _
236-
" var target = document.getElementById('autoit-notification');" & _
237-
" if(target) { target.style.opacity = '0'; setTimeout(() => target.remove(), 500); }" & _
238-
"}, " & $iDuration & ");"
239-
240-
$oWebV2M.ExecuteScript($sJS)
208+
"var oldDiv = document.getElementById('autoit-notification');" & @CRLF & _
209+
"if (oldDiv) { oldDiv.remove(); }" & @CRLF & _
210+
"var div = document.createElement('div');" & @CRLF & _
211+
"div.id = 'autoit-notification'; // Assign the ID" & @CRLF & _
212+
"var div.style = 'position:fixed; top:20px; left:50%; transform:translateX(-50%); padding:15px; background:" & $sBgColor & "; color:white; border-radius:8px; z-index:9999; font-family:sans-serif; box-shadow: 0 4px 6px rgba(0,0,0,0.2); transition: opacity 0.5s; '" & @CRLF & _
213+
"div.innerText = '" & $sMessage & "';" & @CRLF & _
214+
"document.body.appendChild(div);" & @CRLF & _
215+
"setTimeout(() => {" & @CRLF & _
216+
" var target = document.getElementById('autoit-notification');" & @CRLF & _
217+
" If (target) { " & @CRLF & _
218+
" target.style.opacity = '0';" & @CRLF & _
219+
" setTimeout(() => target.remove(), 500); " & @CRLF & _
220+
" }" & @CRLF & _
221+
"}, " & $iDuration & ");" & @CRLF & _
222+
""
223+
224+
$_g_oWeb.ExecuteScript($sJS)
241225
EndFunc ;==>ShowWebNotification
242226

243227
; Synchronizes WebView size with the GUI window
244228
Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam)
245229
#forceref $hWnd, $iMsg, $wParam
246-
; Local error handler for COM objects
247-
Local $oMyError = ObjEvent("AutoIt.Error", _ErrFunc)
248-
#forceref $oMyError
249-
250230
If $hWnd <> $hGUI Then Return $GUI_RUNDEFMSG ; critical, to respond only to the $hGUI
251231
If $wParam = 1 Then Return $GUI_RUNDEFMSG ; 1 = SIZE_MINIMIZED
252232
Local $iW = BitAND($lParam, 0xFFFF), $iH = BitShift($lParam, 16) - 50
253-
If IsObj($oWebV2M) Then $oWebV2M.Resize(($iW < 10 ? 10 : $iW), ($iH < 10 ? 10 : $iH))
233+
If IsObj($_g_oWeb) Then $_g_oWeb.Resize(($iW < 10 ? 10 : $iW), ($iH < 10 ? 10 : $iH))
254234
Return $GUI_RUNDEFMSG
255235
EndFunc ;==>WM_SIZE
256236

257237
; User's COM error function. Will be called if COM error occurs
258-
Func _ErrFunc($oError)
238+
Func __HtmlGUI_ErrFunc(ByRef $oError)
259239
; Do anything here.
260240
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
261241
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
@@ -267,4 +247,4 @@ Func _ErrFunc($oError)
267247
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
268248
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
269249
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
270-
EndFunc ;==>_ErrFunc
250+
EndFunc ;==>__HtmlGUI_ErrFunc

0 commit comments

Comments
 (0)