Skip to content

Commit 41e8692

Browse files
authored
Merge pull request #11 from mlipok/UDF_functions
UDF adding standard functions - confirmed
2 parents d57ce42 + 1d5028e commit 41e8692

6 files changed

Lines changed: 510 additions & 190 deletions

File tree

NetWebView2Lib.au3

Lines changed: 371 additions & 51 deletions
Large diffs are not rendered by default.

examples/1-BasicDemo.au3

Lines changed: 32 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,43 @@
22
#include <GUIConstantsEx.au3>
33
#include <WindowsConstants.au3>
44

5+
#include "..\NetWebView2Lib.au3"
6+
57
; ==============================================================================
68
; WebView2 Multi-Channel Presentation Script
79
; ==============================================================================
810

9-
; Register the exit function
10-
OnAutoItExitRegister("_CleanExit")
11-
1211
; Global objects
13-
Global $oManager, $oBridge
14-
Global $oEvtManager, $oEvtBridge
15-
16-
; COM Error Handler
17-
Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")
1812

1913
; GUI & Controls
2014
Global $hGUI, $idLabelStatus
2115

2216
Main()
2317

2418
Func Main()
25-
; 1. Create the UI
26-
$hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 900, 650)
27-
$idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, 620, 880, 20)
19+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
20+
#forceref $oMyError
21+
22+
; Create the UI
23+
Local $iHeight = 400
24+
$hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 800, $iHeight)
25+
$idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight -20 , 880, 20)
2826
GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI")
2927
GUISetState(@SW_SHOW)
3028

31-
; 2. Instantiate the .NET Manager
32-
$oManager = ObjCreate("NetWebView2.Manager")
33-
If Not IsObj($oManager) Then
34-
MsgBox(16, "Error", "Could not create WebView2 Manager. Please register the DLL.")
35-
Exit
36-
EndIf
29+
; Initialize WebView2 Manager and register events
30+
Local $oWebV2M = _NetWebView2_CreateManager()
31+
$_g_oWeb = $oWebV2M
32+
If @error Then Return SetError(@error, @extended, $oWebV2M)
3733

38-
; 3. Setup Events (Crucial: Define interfaces explicitly)
39-
; Channel 1: Manager Events (INIT_READY, NAV_COMPLETED, etc.)
40-
$oEvtManager = ObjEvent($oManager, "WebView_", "IWebViewEvents")
34+
; Initialize JavaScript Bridge
35+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_")
36+
If @error Then Return SetError(@error, @extended, $oWebV2M)
4137

42-
; Channel 2: JavaScript Bridge (Messages from JS to AutoIt)
43-
$oBridge = $oManager.GetBridge()
44-
$oEvtBridge = ObjEvent($oBridge, "Bridge_", "IBridgeEvents")
38+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
39+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
4540

46-
; 4. Initialize the Browser
47-
; We pass an empty string for the URL to prevent "ConnectionAborted"
48-
; and wait for our INIT_READY signal.
49-
$oManager.Initialize(($hGUI), "", 10, 10, 880, 600)
41+
_NetWebView2_NavigateToString($_g_oWeb, __GetDemoHTML())
5042

5143
; Main Loop
5244
While 1
@@ -57,77 +49,30 @@ Func Main()
5749
WEnd
5850

5951
GUIDelete($hGUI)
60-
EndFunc ;==>Main
6152

62-
Func _CleanExit()
63-
; Check if the object exists before calling methods to avoid COM errors during crash
64-
If IsObj($oManager) Then
65-
$oManager.Cleanup()
66-
EndIf
67-
68-
; Release the event sinks
69-
$oManager = 0
70-
$oBridge = 0
71-
$oEvtManager = 0
72-
$oEvtBridge = 0
73-
$oMyError = 0
74-
75-
ConsoleWrite(">>> Application exited cleanly." & @CRLF)
76-
EndFunc ;==>_CleanExit
53+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
54+
EndFunc ;==>Main
7755

7856
; ==============================================================================
7957
; ; Function to update a text element inside the WebView UI
8058
; ==============================================================================
81-
Func UpdateWebUI($sElementId, $sNewText)
59+
Func UpdateWebUI(ByRef $oWeb, $sElementId, $sNewText)
60+
If Not IsObj($oWeb) Then Return ''
61+
8262
; Escape backslashes, single quotes and handle new lines for JavaScript safety
8363
Local $sCleanText = StringReplace($sNewText, "\", "\\")
8464
$sCleanText = StringReplace($sCleanText, "'", "\'")
8565
$sCleanText = StringReplace($sCleanText, @CRLF, "\n")
8666
$sCleanText = StringReplace($sCleanText, @LF, "\n")
8767

88-
Local $sJS = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';"
89-
90-
If IsObj($oManager) Then
91-
$oManager.ExecuteScript($sJS)
92-
EndIf
68+
Local $sJavaScript = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';"
69+
_NetWebView2_ExecuteScript($oWeb, $sJavaScript)
9370
EndFunc ;==>UpdateWebUI
9471

9572
; ==============================================================================
96-
; EVENT HANDLER: WebView Manager (C# Internal Events)
73+
; MY EVENT HANDLER: Bridge (JavaScript Messages)
9774
; ==============================================================================
98-
Func WebView_OnMessageReceived($sMessage)
99-
ConsoleWrite(">>> [CORE EVENT]: " & $sMessage & @CRLF)
100-
101-
; Separating messages that have parameters (e.g. TITLE_CHANGED|...)
102-
Local $aParts = StringSplit($sMessage, "|")
103-
Local $sCommand = StringStripWS($aParts[1], 3)
104-
105-
Switch $sCommand
106-
Case "INIT_READY"
107-
GUICtrlSetData($idLabelStatus, "Status: Engine Ready. Loading HTML UI...")
108-
$oManager.NavigateToString(_GetDemoHTML())
109-
110-
Case "NAV_STARTING"
111-
GUICtrlSetData($idLabelStatus, "Status: Navigation started...")
112-
113-
Case "NAV_COMPLETED"
114-
GUICtrlSetData($idLabelStatus, "Status: Application Ready.")
115-
116-
Case "TITLE_CHANGED"
117-
; If you want to change the title of your GUI based on the page
118-
If $aParts[0] > 1 Then WinSetTitle($hGUI, "", "WebView2 - " & $aParts[2])
119-
120-
Case "ERROR", "NAV_ERROR"
121-
Local $sErr = ($aParts[0] > 1) ? $aParts[2] : "Unknown"
122-
GUICtrlSetData($idLabelStatus, "Status: Error " & $sErr)
123-
MsgBox(16, "WebView2 Error", $sMessage)
124-
EndSwitch
125-
EndFunc ;==>WebView_OnMessageReceived
126-
127-
; ==============================================================================
128-
; EVENT HANDLER: Bridge (JavaScript Messages)
129-
; ==============================================================================
130-
Func Bridge_OnMessageReceived($sMessage)
75+
Func _BridgeMyEventsHandler_OnMessageReceived($sMessage)
13176
Local Static $iMsgCnt = 0
13277
ConsoleWrite(">>> [JS MESSAGE]: " & $sMessage & @CRLF)
13378

@@ -136,14 +81,14 @@ Func Bridge_OnMessageReceived($sMessage)
13681
Else
13782
MsgBox(64, "JS Notification", "Message from Browser: " & $sMessage)
13883
$iMsgCnt += 1
139-
UpdateWebUI("mainTitle", $iMsgCnt & " Hallo from AutoIt!")
84+
UpdateWebUI($_g_oWeb, "mainTitle", $iMsgCnt & " Hello from AutoIt!")
14085
EndIf
141-
EndFunc ;==>Bridge_OnMessageReceived
86+
EndFunc ;==>_BridgeMyEventsHandler_OnMessageReceived
14287

14388
; ==============================================================================
14489
; HELPER: Demo HTML Content
14590
; ==============================================================================
146-
Func _GetDemoHTML()
91+
Func __GetDemoHTML()
14792
Local $sH = '<html><head><style>' & _
14893
'body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _
14994
'.card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _
@@ -158,21 +103,4 @@ Func _GetDemoHTML()
158103
'</div>' & _
159104
'</body></html>'
160105
Return $sH
161-
EndFunc ;==>_GetDemoHTML
162-
163-
; ==============================================================================
164-
; COM ERROR HANDLER
165-
; ==============================================================================
166-
Func _ErrFunc($oError)
167-
; Do anything here.
168-
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
169-
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
170-
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
171-
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
172-
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
173-
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
174-
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
175-
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
176-
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
177-
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
178-
EndFunc ;==>_ErrFunc
106+
EndFunc ;==>__GetDemoHTML

examples/4-FileViewerDemo.au3

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#include <GUIConstantsEx.au3>
3+
#include <WindowsConstants.au3>
4+
5+
#include "..\NetWebView2Lib.au3"
6+
7+
; ==============================================================================
8+
; WebView2 Multi-Channel Presentation Script^
9+
; ==============================================================================
10+
11+
; Global objects
12+
13+
; GUI & Controls
14+
Global $hGUI, $idLabelStatus
15+
16+
Main()
17+
18+
Func Main()
19+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
20+
#forceref $oMyError
21+
22+
; Create the UI
23+
Local $iHeight = 400
24+
$hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 800, $iHeight, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
25+
$idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight -20 , 880, 20)
26+
GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI")
27+
28+
; Initialize WebView2 Manager and register events
29+
Local $oWebV2M = _NetWebView2_CreateManager()
30+
$_g_oWeb = $oWebV2M
31+
If @error Then Return SetError(@error, @extended, $oWebV2M)
32+
33+
; Initialize JavaScript Bridge
34+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_")
35+
If @error Then Return SetError(@error, @extended, $oWebV2M)
36+
37+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
38+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
39+
40+
GUISetState(@SW_SHOW)
41+
ConsoleWrite("! ===" & @ScriptLineNumber & @CRLF)
42+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 0)
43+
Local $s_PDF_FileFullPath
44+
45+
GUISetState(@SW_HIDE)
46+
$s_PDF_FileFullPath = "file:///" & @ScriptDir & '/FileViewerDemo_1.pdf'
47+
_NetWebView2_Navigate($oWebV2M, $s_PDF_FileFullPath)
48+
ConsoleWrite("! ===" & @ScriptLineNumber & @CRLF)
49+
Sleep(200)
50+
GUISetState(@SW_SHOW)
51+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, $s_PDF_FileFullPath)
52+
53+
GUISetState(@SW_HIDE)
54+
$s_PDF_FileFullPath = "file:///" & @ScriptDir & '/FileViewerDemo_2.pdf'
55+
_NetWebView2_Navigate($oWebV2M, $s_PDF_FileFullPath)
56+
ConsoleWrite("! ===" & @ScriptLineNumber & @CRLF)
57+
Sleep(200)
58+
GUISetState(@SW_SHOW)
59+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, $s_PDF_FileFullPath)
60+
61+
ConsoleWrite($oWebV2M.GetBrowserProcessId() & @CRLF)
62+
ConsoleWrite($oWebV2M.GetInnerText() & @CRLF)
63+
_NetWebView2_Navigate($oWebV2M, 'https://google.com')
64+
MsgBox($MB_TOPMOST, @ScriptLineNumber, _NetWebView2_GetSource($oWebV2M))
65+
MsgBox($MB_TOPMOST, @ScriptLineNumber, $oWebV2M.GetDocumentTitle())
66+
;~ $oWebV2M.Print()
67+
ConsoleWrite($oWebV2M.WebViewSetFocus() & @CRLF)
68+
$oWebV2M.LockWebView()
69+
$oWebV2M.DisableBrowserFeatures()
70+
71+
ConsoleWrite("- " & $oWebV2M.GetHtmlSource() & @CRLF)
72+
ConsoleWrite("- " & $oWebV2M.GetSelectedText() & @CRLF)
73+
$oWebV2M.SetZoom(0.6);
74+
75+
76+
MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 1)
77+
78+
; Main Loop
79+
While 1
80+
Switch GUIGetMsg()
81+
Case $GUI_EVENT_CLOSE
82+
ExitLoop
83+
EndSwitch
84+
WEnd
85+
86+
GUIDelete($hGUI)
87+
88+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
89+
EndFunc ;==>Main

examples/FileViewerDemo_1.pdf

13.2 KB
Binary file not shown.

examples/FileViewerDemo_2.pdf

13.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)