Skip to content

Commit de4456b

Browse files
authored
Update 2-Html_Gui.au3
1 parent cc55149 commit de4456b

1 file changed

Lines changed: 37 additions & 43 deletions

File tree

examples/2-Html_Gui.au3

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,78 @@
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

910
; Global variables for data management
1011
Global $aMessages[0][3]
1112
Global $sFilePath = @ScriptDir & "\messages.csv"
12-
Global $hGUI
13-
13+
Global $hGUI, $oBridge, $idBlue, $idRed
1414
Main()
1515
Exit
1616

1717
Func Main()
18+
19+
_Show_Form()
20+
21+
; Main Application Loop
22+
While 1
23+
Switch GUIGetMsg()
24+
Case $GUI_EVENT_CLOSE
25+
_NetWebView2_CleanUp($_g_oWeb, $oBridge)
26+
ExitLoop
27+
28+
Case $idBlue
29+
; Update CSS variables dynamically via JavaScript
30+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#4db8ff');")
31+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#0078d7');")
32+
33+
Case $idRed
34+
; Update CSS variables dynamically via JavaScript
35+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#ff4d4d');")
36+
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#d70000');")
37+
EndSwitch
38+
WEnd
39+
40+
EndFunc ;==>Main
41+
42+
Func _Show_Form()
1843
; Create GUI with resizing support
1944
$hGUI = GUICreate("WebView2 Theme Switcher", 500, 480, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
2045
GUISetBkColor(0x1E1E1E)
2146

22-
Local $idBlue = GUICtrlCreateLabel("Blue Theme", 10, 10, 100, 30)
47+
$idBlue = GUICtrlCreateLabel("Blue Theme", 10, 10, 100, 30)
2348
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
2449
GUICtrlSetColor(-1, 0x0078D7)
2550

26-
Local $idRed = GUICtrlCreateLabel("Red Theme", 120, 10, 100, 30)
51+
$idRed = GUICtrlCreateLabel("Red Theme", 120, 10, 100, 30)
2752
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
2853
GUICtrlSetColor(-1, 0xFF0000)
2954

3055
; Create WebView2 Manager object and register events
31-
$_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_", "--allow-file-access-from-files")
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_", "")
3257

3358
; initialize browser - put it on the GUI
3459
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
3560
_NetWebView2_Initialize($_g_oWeb, $hGUI, $sProfileDirectory, 0, 50, 500, 400, True, False, False, 1.1)
3661

3762
; Create bridge object and register events
38-
Local $oBridge = _NetWebView2_GetBridge($_g_oWeb, "__MyEVENTS_Bridge_")
39-
#forceref $oBridge
63+
$oBridge = _NetWebView2_GetBridge($_g_oWeb, "__MyEVENTS_Bridge_")
4064

4165
$_g_oWeb.IsZoomControlEnabled = False
4266
$_g_oWeb.IsScrollbarEnabled = False
4367

4468
; Register the WM_SIZE message to handle window resizing
4569
GUIRegisterMsg($WM_SIZE, WM_SIZE)
4670

47-
; Main Application Loop
48-
While 1
49-
Switch GUIGetMsg()
50-
Case $GUI_EVENT_CLOSE
51-
_NetWebView2_CleanUp($_g_oWeb, $oBridge)
52-
ExitLoop
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)
5374

54-
Case $idBlue
55-
; Update CSS variables dynamically via JavaScript
56-
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#4db8ff');")
57-
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#0078d7');")
58-
59-
Case $idRed
60-
; Update CSS variables dynamically via JavaScript
61-
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--accent-color', '#ff4d4d');")
62-
_NetWebView2_ExecuteScript($_g_oWeb, "document.documentElement.style.setProperty('--btn-color', '#d70000');")
63-
EndSwitch
64-
WEnd
65-
66-
EndFunc ;==>Main
67-
68-
; Handles data received from the WebView2 Manager
69-
Func __MyEVENTS_Manager_OnMessageReceived($sMessage)
70-
; Local error handler for COM objects
71-
Local $oMyError = ObjEvent("AutoIt.Error", __HtmlGUI_ErrFunc)
72-
#forceref $oMyError
73-
74-
Local Static $bIsInitialized = False
75-
If $sMessage = "INIT_READY" And Not $bIsInitialized Then
76-
$bIsInitialized = True ; We note that we are finished.
77-
Local $sHTML = "<html><head><meta charset='UTF-8'><style>:" & __FormCSS() & "</style></head><body>" & __FormHTML() & "</body></html>"
78-
$_g_oWeb.NavigateToString($sHTML)
79-
GUISetState(@SW_SHOW, $hGUI)
80-
EndIf
81-
EndFunc ;==>__MyEVENTS_Manager_OnMessageReceived
75+
EndFunc ;==>_Show_Form
8276

8377
; Handles data received from the JavaScript 'postMessage'
8478
Func __MyEVENTS_Bridge_OnMessageReceived($sMessage)
@@ -106,7 +100,7 @@ Func __MyEVENTS_Bridge_OnMessageReceived($sMessage)
106100
_ArrayAdd($aMessages, $sName & "|" & $sEmail & "|" & $sMsg)
107101

108102
; Append data to CSV file safely
109-
Local $hFile = FileOpen($sFilePath, 9) ; 1 (Write) + 8 (Create Path)
103+
Local $hFile = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)
110104
If $hFile <> -1 Then
111105
; Clean the message string for CSV compatibility (remove line breaks)
112106
Local $sCleanMsg = StringReplace($sMsg, @CRLF, " ")

0 commit comments

Comments
 (0)