#cs ---------------------------------------------------------------------------- AutoIt Version : 3.3.0.0 Auteur: GaRy delaMer Fonction du Script : Exemple Un petit navigateur web intégrer dans une GUI #ce ---------------------------------------------------------------------------- #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=GaRy deleMer WebBrowser.Exe #AutoIt3Wrapper_Compression=4 #Tidy_Parameters=/sf #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #Region ;**** Options AutoIt **** Opt('MustDeclareVars', 1) Opt('GUICloseOnESC', 1) #EndRegion ;=============================================== #Region ;**** Include AutoIt **** #include #include #include #include #include #include #include #include #include <_My_UDF\WinHTTPConstants.au3> #include <_My_UDF\WinHTTP.au3> #EndRegion ;=============================================== Global $Titre_Appli = "GaRy delaMer WebBrowser://" If Not @Compiled Then _Singleton($Titre_Appli) HotKeySet("!x", "_GUIEvent_Quit") EndIf #Region ;**** Gui, ActiveX _IE, statusbar **** Global $hGUI = GUICreate($Titre_Appli, @DesktopWidth * 4 / 5, @DesktopHeight * 4 / 5, -1, -1, _ $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN) ;; les menus Global $filemenu = GUICtrlCreateMenu("&Page") Global $fileitem = GUICtrlCreateMenuItem("&Ouvre un fichier", $filemenu) Global $htmlitem = GUICtrlCreateMenuItem("&Coller du code html", $filemenu) GUICtrlCreateMenuItem("", $filemenu) Global $exititem = GUICtrlCreateMenuItem("&Quitter", $filemenu) ;; les boutons Global $hURL = GUICtrlCreateInput("", 1, 1, 100, 20) Global $hOK = GUICtrlCreateButton("ok", 100, 1, 50, 20) Global $hBack = GUICtrlCreateButton("<-", 100, 1, 50, 20) Global $hForward = GUICtrlCreateButton("->", 100, 1, 50, 20) ;; Le contrôle ActivX IE _IEErrorHandlerRegister() Global $oIE = _IECreateEmbedded() Global $hIE = GUICtrlCreateObj($oIE, 22, 0) _IENavigate($oIE, "about:blank") ;; la barre de statut Global $hStatus = _GUICtrlStatusBar_Create($hGUI) _GUICtrlStatusBar_SetMinHeight($hStatus, 20) Global $ProgressBar = GUICtrlCreateProgress(1, 1, -1, -1, $PBS_SMOOTH) ; marquee works on Win XP and above GUICtrlSetState($ProgressBar, $GUI_HIDE) Sleep(250) GUIRegisterMsg($WM_SIZE, "_My_WM_SIZE") GUISetState() ;Show GUI Global $aParts[3] = [200, 500, -1] _GUICtrlStatusBar_SetParts($hStatus, $aParts) _GUICtrlStatusBar_SetText($hStatus, "Prêt") _GUICtrlStatusBar_SetIcon($hStatus, 0, _WinAPI_LoadShell32Icon(13)) _GUICtrlStatusBar_EmbedControl($hStatus, 2, GUICtrlGetHandle($ProgressBar)) _GUICtrlStatusBar_Resize($hStatus) #EndRegion ;=============================================== _GUIEVENT_Resize() Global $URL = "http://www.autoitscript.fr/forum/" GUICtrlSetData($hURL, $URL) ; Assign events to UDFs starting with IEEvent_ Global $LoadComplete = False Global $oIEEvent = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2") _IENavigateComplete($URL) ;; logon _IEFormGetCollection($oIE) If @extended > 0 Then Local $oForms = _IEFormGetCollection($oIE) For $oForm In $oForms Local $oUser = _IEFormElementGetObjByName($oForm, "username") If Not @error Then Local $username = InputBox("Logon", "Votre login ?", "GaRydelaMer") Local $oPass = _IEFormElementGetObjByName($oForm, "password") Local $oSubmit = _IEFormElementGetObjByName($oForm, "login") If $username <> "" Then Local $password = InputBox("Logon", "Votre mot de passe ?", "", "*") _IEFormElementSetValue($oUser, $username) _IEFormElementSetValue($oPass, $password) _IEAction($oSubmit, "click") ;~ _IEFormSubmit($oForm) EndIf ExitLoop EndIf Next EndIf Global $testmenu = GUICtrlCreateMenu("&Test") Global $post_files = GUICtrlCreateMenuItem("&Poster un fichier dans un formulaire Web", $testmenu) While 1 Local $msg = GUIGetMsg() Switch $msg Case $post_files _Post_Files() Case $hURL If GUICtrlRead($hURL) <> $URL Then _IENavigate($oIE, GUICtrlRead($hURL)) Case $hBack _IEAction($oIE, "back") Case $hForward _IEAction($oIE, "forward") Case $hOK _IENavigate($oIE, GUICtrlRead($hURL)) Case $fileitem Local $hFile = FileOpenDialog("Ouvre une source HTML", @ScriptDir, _ "Source html (*.html, *.htm, *.txt)|Tous (*.*)") If Not @error Then Local $html = FileRead($hFile) If $html <> "" Then _IEBodyWriteHTML($oIE, $html) EndIf EndIf Case $htmlitem Local $html = ClipGet() If IsString($html) And $html <> "" Then _IEBodyWriteHTML($oIE, $html) Case $GUI_EVENT_CLOSE, $exititem _GUIEvent_Quit() EndSwitch WEnd Func _Post_Files() Local $URL = _WinHttpCrackUrl("http://localhost/test/post_files.php") Local $hOpen = _WinHttpOpen() Local $hConnect = _WinHTTPConnect($hOpen, $URL[2], $URL[3]) Local $hRequest = _WinHttpOpenRequest($hConnect, "POST", $URL[6] & $URL[7], "HTTP/1.1", "http://" & $URL[2]) Local $form_fields[2][2] = [["Date", _Now()],["commentaire", "Plein de gros test & é a ê î è"]] _ScreenCapture_Capture("capture.jpg", 100, 100, 400, 500, False) Local $form_files[1][2] = [["capture", "capture.jpg"]] Local $FormDatas = Encode_MultiPart_FormData($form_fields, $form_files) Local $content_type = 'Content-Type: ' & $FormDatas[0] & @CRLF _WinHttpSendRequest($hRequest, $content_type, $WINHTTP_NO_REQUEST_DATA, StringLen($FormDatas[1])) _WinHTTPWriteData($hRequest, StringToBinary($FormDatas[1]), 1) _WinHttpReceiveResponse($hRequest) Local $html = "" If _WinHttpQueryDataAvailable($hRequest) Then Local $temp While 1 $temp = _WinHttpReadData($hRequest) If $temp = "" Then ExitLoop $html &= $temp WEnd $temp = "" EndIf ConsoleWrite(_WinHttpQueryHeaders($hRequest) & @LF) _IEDocWriteHTML($oIE, $html) _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) EndFunc ;==>_Post_Files #Region ;============= Fonctions ========================== #Region ;; GUIEvent Func _GUIEvent_Quit() Exit EndFunc ;==>_GUIEvent_Quit Func _GUIEvent_Resize() _GUICtrlStatusBar_Resize($hStatus) Local $Size = WinGetClientSize($hGUI, "") GUICtrlSetPos($hURL, 1, 1, $Size[0] - 170, 20) GUICtrlSetPos($hForward, $Size[0] - 55, 1) GUICtrlSetPos($hBack, $Size[0] - 110, 1) GUICtrlSetPos($hOK, $Size[0] - 165, 1) GUICtrlSetPos($hIE, 0, 22, $Size[0], $Size[1] - _GUICtrlStatusBar_GetHeight($hStatus) - 23) EndFunc ;==>_GUIEvent_Resize Func _IENavigateComplete($URL) _IENavigate($oIE, $URL) While Not $LoadComplete WEnd EndFunc ;==>_IENavigateComplete Func _My_WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam) _GUIEvent_Resize() Return $GUI_RUNDEFMSG EndFunc ;==>_My_WM_SIZE #EndRegion #Region ;; IEEvent Func _IEEvent_BeforeNavigate2($pDisp, $vUrl, $vFlags, $vTargetFrameName, $vPostData, $vHeaders, $bCancel) ;~ _DebugPrint("_IEEvent_BeforeNavigate2(""" & $vUrl & """)") GUICtrlSetState($ProgressBar, $GUI_SHOW) $LoadComplete = False EndFunc ;==>_IEEvent_BeforeNavigate2 Func _IEEvent_DocumentComplete($pDisp, $sURL) _Debug("_IEEvent_DocumentComplete()") $LoadComplete = True GUICtrlSetState($ProgressBar, $GUI_HIDE) _GUICtrlStatusBar_SetText($hStatus, "Prêt", 0, $SBT_NOBORDERS) _GUICtrlStatusBar_SetText($hStatus, @TAB & _IEPropertyGet($oIE, "title"), 1, $SBT_NOBORDERS) EndFunc ;==>_IEEvent_DocumentComplete Func _IEEvent_DownloadComplete() ;~ _DebugPrint("_IEEvent_DownloadComplete()") EndFunc ;==>_IEEvent_DownloadComplete Func _IEEvent_NavigateComplete2($pDisp, $sURL) ;~ _DebugPrint("_IEEvent_NavigateComplete2(""" & $sURL & """)") If StringInStr($sURL, "javascript:") Then Return $URL = $sURL GUICtrlSetData($hURL, $sURL) EndFunc ;==>_IEEvent_NavigateComplete2 Func _IEEvent_ProgressChange($Progress, $ProgressMax) ;~ _DebugPrint("_IEEvent_ProgressChange(""" & $Progress & ", " & $ProgressMax & """)") If $ProgressMax = 0 Then _GUICtrlStatusBar_SetText($hStatus, "", 1, $SBT_NOBORDERS) _IEEvent_TitleChange() EndIf If $Progress > 0 Then Local $percent = Int(($Progress * 100) / $ProgressMax) GUICtrlSetState($ProgressBar, $GUI_SHOW) GUICtrlSetData($ProgressBar, $percent) _GUICtrlStatusBar_SetText($hStatus, "Chargement de la page... " & $percent & "%", 1, $SBT_NOBORDERS) EndIf EndFunc ;==>_IEEvent_ProgressChange Func _IEEvent_StatusTextChange($sText) ;~ _DebugPrint("_IEEvent_StatusTextChange(""" & $sText & """)") If $sText = "" Then $sText = "Prêt" _GUICtrlStatusBar_SetText($hStatus, $sText, 0, $SBT_NOBORDERS) EndFunc ;==>_IEEvent_StatusTextChange Func _IEEvent_TitleChange($sText = "") ;~ _DebugPrint("_IEEvent_TitleChange(""" & $sText & """)") If $sText = "" Then $sText = _IEPropertyGet($oIE, "title") WinSetTitle($hGUI, "", $Titre_Appli & $sText) _GUICtrlStatusBar_SetText($hStatus, @TAB & $sText, 2, $SBT_NOBORDERS) EndFunc ;==>_IEEvent_TitleChange #EndRegion #Region ;; Debug Func _Debug($debug, $line = @ScriptLineNumber) $debug &= @LF & $URL & " :: " & _IEPropertyGet($oIE, "title") & @LF _IEFrameGetCollection($oIE) $debug &= "Nb de frame: " & @extended & @LF _IEFormElementGetCollection($oIE) $debug &= "Nb de formulaire: " & @extended & @LF _IEImgGetCollection($oIE) $debug &= "Nb d'image: " & @extended & @LF _IELinkGetCollection($oIE) $debug &= "Nb de lien: " & @extended _DebugPrint($debug, $line) EndFunc ;==>_Debug Func _DebugPrint($s_text, $line = @ScriptLineNumber) ConsoleWrite( _ "!===========================================================" & @LF & _ "+======================================================" & @LF & _ "" & $line & ",0:" & @TAB & $s_text & @LF & _ "+======================================================" & @LF) EndFunc ;==>_DebugPrint #EndRegion #EndRegion ;===============================================