Skip to content

Commit aff5c6c

Browse files
authored
4-FileViewerDemo_Loop.au3
1 parent a296c4b commit aff5c6c

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

examples/4-FileViewerDemo_Loop.au3

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#include <File.au3>
3+
#include <GUIConstantsEx.au3>
4+
#include <SendMessage.au3>
5+
#include <StaticConstants.au3>
6+
#include <WinAPIGdi.au3>
7+
#include <WinAPIGdiDC.au3>
8+
#include <WinAPIHObj.au3>
9+
#include <WinAPISysWin.au3>
10+
#include <WindowsConstants.au3>
11+
12+
13+
#SciTE4AutoIt3_Dynamic_Include_Path=;..\NetWebView2Lib.au3
14+
#SciTE4AutoIt3_Dynamic_Include=y ;dynamic.include=y/n
15+
#SciTE4AutoIt3_Dynamic_Include_whiletyping=y ;dynamic.include.whiletyping=y/n
16+
#SciTE4AutoIt3_Dynamic_Include_recursive_check=y ;dynamic.include.recursive.check=n/y
17+
#SciTE4AutoIt3_Dynamic_Include_use_local_cache=y ;dynamic.include.use.local.cache=n/y
18+
#SciTE4AutoIt3_Dynamic_Include_version=prod ;dynamic.include.version=prod/beta
19+
#SciTE4AutoIt3_Dynamic_Include_verboselevel=0 ;dynamic.include.verbose.level=0/1/2 ; 0=disabled; 1=Timing per Function; 2=1+Total timing
20+
#SciTE4AutoIt3_Dynamic_Include_Always_Update_Local_File=n ;dynamic.include.always.update.local.file=n/y
21+
#SciTE4AutoIt3_AutoItTools_debug=n ;debug.autoIttools=n/y
22+
#SciTE4AutoIt3_AutoItDynamicIncludes_debug=n ;debug.AutoItDynamicIncludes=n/y
23+
#SciTE4AutoIt3_AutoItAutocomplete_debug=n ;debug.autoitautocomplet=n/y
24+
#SciTE4AutoIt3_AutoItGotoDefinition_debug=n ;debug.autoitgotodefinition=n/y
25+
#SciTE4AutoIt3_AutoItIndentFix_debug=n ;debug.autoitindentfix=n/y
26+
27+
#include "..\NetWebView2Lib.au3"
28+
29+
Main()
30+
31+
Func Main()
32+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
33+
#forceref $oMyError
34+
35+
; Create the UI
36+
Local $iHeight = 800
37+
Local $hGUI = GUICreate("WebView2 .NET Manager - Demo: " & @ScriptName, 1100, $iHeight, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
38+
Local $idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight - 20, 880, 20)
39+
#forceref $idLabelStatus
40+
GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI")
41+
42+
; Initialize WebView2 Manager and register events
43+
Local $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", "", "--mute-audio")
44+
$_g_oWeb = $oWebV2M
45+
If @error Then Return SetError(@error, @extended, $oWebV2M)
46+
47+
; Initialize JavaScript Bridge
48+
Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_")
49+
If @error Then Return SetError(@error, @extended, $oWebV2M)
50+
51+
Local $sProfileDirectory = @TempDir & "\..\UserDataFolder"
52+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
53+
Local $i_ProcessID = @extended
54+
#forceref $i_ProcessID
55+
56+
GUISetState(@SW_SHOW, $hGUI)
57+
ConsoleWrite("! ===" & @ScriptLineNumber & @CRLF)
58+
;~ MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 0)
59+
Local $s_PDF_FileFullPath
60+
61+
Local $s_PDF_Directory = FileSelectFolder('Choose folder with PDF','')
62+
63+
Local $a_Files = _FileListToArrayRec($s_PDF_Directory, '*.pdf', $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
64+
Local Static $hWebView2_Window = _WinAPI_GetWindow($hGUI, $GW_CHILD)
65+
For $IDX_File = 1 To $a_Files[0]
66+
$s_PDF_FileFullPath = $a_Files[$IDX_File]
67+
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, $hWebView2_Window, '#view=FitH', 1000)
68+
ConsoleWrite("! === @SLN=" & @ScriptLineNumber & ' ' & $s_PDF_FileFullPath & @CRLF)
69+
Next
70+
71+
; Main Loop
72+
While 1
73+
Switch GUIGetMsg()
74+
Case $GUI_EVENT_CLOSE
75+
ExitLoop
76+
EndSwitch
77+
WEnd
78+
79+
GUIDelete($hGUI)
80+
81+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
82+
EndFunc ;==>Main
83+
84+
Func _EnumWindow($hWnd)
85+
Local $aData = _WinAPI_EnumChildWindows($hWnd)
86+
ConsoleWrite("! $aData[1][0] = " & $aData[1][0] & @CRLF)
87+
;~ _ArrayDisplay($aData, '_WinAPI_EnumChildWindows')
88+
89+
If Not @error And UBound($aData) Then Return $aData[1][0]
90+
91+
Return SetError(1, @extended, False)
92+
EndFunc ;==>Example
93+
94+
Func __NetWebView2_freezer($hWebView2_Window, $idPic = 0)
95+
#Region ; if $idPic is given then it means you already have it and want to delete it - unfreeze - show WebView2 content
96+
If $idPic Then
97+
_SendMessage($hWebView2_Window, $WM_SETREDRAW, True, 0) ; Enables
98+
_WinAPI_RedrawWindow($hWebView2_Window, 0, 0, BitOR($RDW_FRAME, $RDW_INVALIDATE, $RDW_ALLCHILDREN)) ; Repaints
99+
GUICtrlDelete($idPic)
100+
Return
101+
EndIf
102+
#EndRegion ; if $idPic is given then it means you already have it and want to delete it - unfreeze - show WebView2 content
103+
104+
#Region ; freeze $hWebView2_Window
105+
106+
#Region ; add PIC to parent window
107+
Local $hMainGUI_Window = _WinAPI_GetWindow($hWebView2_Window, $GW_HWNDPREV)
108+
Local $aPos = WinGetPos($hWebView2_Window)
109+
Local $hPrev = GUISwitch($hMainGUI_Window)
110+
$idPic = GUICtrlCreatePic('', 0, 0, $aPos[2], $aPos[3])
111+
Local $hPic = GUICtrlGetHandle($idPic)
112+
GUISwitch($hPrev)
113+
#EndRegion ; add PIC to parent window
114+
115+
; Create bitmap
116+
Local $hDC = _WinAPI_GetDC($hPic)
117+
Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
118+
Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $aPos[2], $aPos[3])
119+
Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
120+
Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
121+
Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $aPos[2], $aPos[3])
122+
Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBmp)
123+
_WinAPI_PrintWindow($hWebView2_Window, $hSrcDC, 2)
124+
_WinAPI_BitBlt($hDestDC, 0, 0, $aPos[2], $aPos[3], $hSrcDC, 0, 0, $MERGECOPY)
125+
126+
_WinAPI_ReleaseDC($hPic, $hDC)
127+
_WinAPI_SelectObject($hDestDC, $hDestSv)
128+
_WinAPI_SelectObject($hSrcDC, $hSrcSv)
129+
_WinAPI_DeleteDC($hDestDC)
130+
_WinAPI_DeleteDC($hSrcDC)
131+
_WinAPI_DeleteObject($hBmp)
132+
133+
; Set bitmap to control
134+
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
135+
Local $hObj = _SendMessage($hPic, $STM_GETIMAGE)
136+
If $hObj <> $hBitmap Then
137+
_WinAPI_DeleteObject($hBitmap)
138+
EndIf
139+
140+
_SendMessage($hWebView2_Window, $WM_SETREDRAW, False, 0) ; Disables ; https://www.autoitscript.com/forum/topic/199172-disable-gui-updating-repainting/
141+
Return $idPic
142+
#EndRegion ; freeze $hWebView2_Window
143+
EndFunc ;==>__NetWebView2_freezer
144+
145+
Func _NetWebView2_NavigateToPDF(ByRef $oWebV2M, $s_URL_or_FileFullPath, $hWebView2_Window = 0, $s_Parameters = '', $iSleep_ms = 1000)
146+
Local $idPic
147+
If FileExists($s_URL_or_FileFullPath) Then
148+
$s_URL_or_FileFullPath = StringReplace($s_URL_or_FileFullPath, ' ', '%20')
149+
$s_URL_or_FileFullPath = "file:///" & $s_URL_or_FileFullPath
150+
If $iSleep_ms Then
151+
$iSleep_ms -= 400
152+
If $iSleep_ms < 600 Then $iSleep_ms = 600
153+
EndIf
154+
EndIf
155+
156+
If $s_Parameters Then
157+
$s_URL_or_FileFullPath &= $s_Parameters
158+
#TIP: FitToPage: https://stackoverflow.com/questions/78820187/how-to-change-webview2-fit-to-page-button-on-pdf-toolbar-default-to-fit-to-width#comment138971950_78821231
159+
#TIP: Open desired PAGE: https://stackoverflow.com/questions/68500164/cycle-pdf-pages-in-wpf-webview2#comment135402565_68566860
160+
EndIf
161+
162+
If $hWebView2_Window Then $idPic = __NetWebView2_freezer($hWebView2_Window)
163+
164+
_NetWebView2_Navigate($oWebV2M, $s_URL_or_FileFullPath)
165+
Sleep($iSleep_ms)
166+
If $hWebView2_Window Then
167+
__NetWebView2_freezer($hWebView2_Window, $idPic)
168+
EndIf
169+
EndFunc ;==>_NetWebView2_NavigateToPDF

0 commit comments

Comments
 (0)