utilisation "IWebBrowser2" avec "WebGadget"

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

utilisation "IWebBrowser2" avec "WebGadget"

Message par celtic88 »

un simple exemple qui montre comment utiliser l'interface "IWebBrowser2" avec "WebGadget"

l'exemple montre comment mettre l'Authentification dans le site "purebasic" puis connecter :wink:

amusez vous bien

le code:

Code : Tout sélectionner

EnableExplicit

Macro DEFINE_GUID(name, ll, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8):DataSection:name:Data.l ll:Data.w w1:Data.w w2:Data.b b1, b2, b3, b4, b5, b6, b7, b8:EndDataSection:EndMacro
DEFINE_GUID(IID_IHTMLImgElement,$3050f240,$98b5,$11cf,$bb,$82,$0,$aa,$0,$bd,$ce,$0b)
DEFINE_GUID(IID_IHTMLElement,$3050F1FF,$98B5, $11CF,$BB, $82, $00, $AA, $00, $BD, $CE, $0B)
DEFINE_GUID(IID_IHTMLDocument2,$332C4425,$26CB, $11D0 ,$B4, $83, $00, $C0, $4F, $D9, $01, $19 )
DEFINE_GUID(IID_IHTMLInputTextElement,$3050F2A6,$98B5,$11CF,$BB,$82,$00,$AA,$00,$BD,$CE,$0B)
DEFINE_GUID(IID_IHTMLInputButtonElement,$3050F2B2,$98B5,$11CF,$BB,$82,$00,$AA,$00,$BD,$CE,$0B)

Procedure WebGadget_getWebBrowser(WebGadget)
  ProcedureReturn GetWindowLong_(GadgetID(WebGadget), #GWL_USERDATA) 
EndProcedure

Procedure WebGadget_PageIsLoaded(WebBrowser.IWebBrowser2)
  Protected READYSTATE_COMPLETE.l ,ireturn = 0
  If WebBrowser
    WebBrowser\get_ReadyState(@READYSTATE_COMPLETE)
    ireturn = Bool(READYSTATE_COMPLETE=4)
  EndIf
  ProcedureReturn ireturn
EndProcedure

Procedure WebGadget_getDocument(WebBrowser.IWebBrowser2)
  Protected idh.IDispatch ,ireturn = 0,Ihtmldocument.IHTMLDocument2
  If WebBrowser
    If WebBrowser\get_Document(@idh)=0
      If idh\QueryInterface(?IID_IHTMLDocument2,@Ihtmldocument)=0
        ireturn = Ihtmldocument
      EndIf
      idh\Release()
    EndIf
  EndIf
  ProcedureReturn ireturn
EndProcedure

Procedure WebGadget_FreeInterfaces(idh.IDispatch)
  If idh
    idh\Release()
  EndIf
EndProcedure

Procedure WebGadget_Document_GetAllElementList(Ihtmldocument.IHTMLDocument2,List lItem.IHTMLElement())
  ClearList(lItem())
  Protected pElement.IHTMLElementCollection, NumItems.l,varName.VARIANT,varIndex.VARIANT,
            idh2.IDispatch ,pItem.IHTMLElement 
  If Ihtmldocument
    If Ihtmldocument\get_all(@pElement)=0
      pElement\get_length(@NumItems)
      Protected y:For y=0 To NumItems-1
        varName\vt = #VT_I4:varName\lVal = y:varIndex\vt = #VT_I4:varIndex\lVal = 1   
        If pElement\item(varName,varIndex,@idh2)=0
          If idh2\QueryInterface(?IID_IHTMLElement, @pItem)=0
            AddElement(lItem())
            PokeI(@lItem(), pItem)
          EndIf
          idh2\Release()
        EndIf
      Next
      pElement\Release()
    EndIf
  EndIf
  ProcedureReturn ListSize(lItem())
EndProcedure

Macro WebGadget_FreeListInterfaces(lList)
  ForEach lList
    WebGadget_FreeInterfaces(lList)
  Next
  ClearList(lList)
EndMacro

Procedure WebGadget_Document_getElement(htmlelement.IHTMLElement,Class_ID)
  Protected pElement
  If htmlelement
    htmlelement\QueryInterface(Class_ID,@pElement)
  EndIf
  ProcedureReturn pElement
EndProcedure

Macro WebGadget_Document_getInputText(htmlelement)
  WebGadget_Document_getElement(htmlelement,?IID_IHTMLInputTextElement)
EndMacro

Macro WebGadget_Document_getImage(htmlelement)
  WebGadget_Document_getElement(htmlelement,?IID_IHTMLImgElement)
EndMacro

Macro WebGadget_Document_getButton(htmlelement)
  WebGadget_Document_getElement(htmlelement,?IID_IHTMLInputButtonElement)
EndMacro

Procedure WebGadget_Document_InputTextById_SetValue(List htmlelement.IHTMLElement(),Id.s,Value.s)
  Protected pid,htmlunputtext.IHTMLInputTextElement
  ForEach htmlelement()
    pid = 0
    htmlelement()\get_id(@pid)
    If pid And PeekS(pid) = Id
      htmlunputtext = WebGadget_Document_getInputText(htmlelement())
      If htmlunputtext
        htmlunputtext\put_value(Value)
        WebGadget_FreeInterfaces(htmlunputtext)
        Break
      EndIf
    EndIf
  Next
EndProcedure

Procedure WebGadget_Document_InputTextByName_SetValue(List htmlelement.IHTMLElement(),Name.s,Value.s)
  Protected pid,ibreak,htmlunputtext.IHTMLInputTextElement
  ForEach htmlelement()
    htmlunputtext = WebGadget_Document_getInputText(htmlelement())
    If htmlunputtext
      pid = 0
      htmlunputtext\get_name(@pid)
      If pid And PeekS(pid) = Name
        htmlunputtext\put_value(Value)
        ibreak = 1
      EndIf 
      WebGadget_FreeInterfaces(htmlunputtext)
      If ibreak = 1
        Break
      EndIf
    EndIf
  Next
  ProcedureReturn ibreak
EndProcedure

Procedure WebGadget_Document_ButtonById_Click(List htmlelement.IHTMLElement(),Id.s)
  Protected pid
  ForEach htmlelement()
    pid = 0
    htmlelement()\get_id(@pid)
    If pid And PeekS(pid) = Id
      htmlelement()\click()
      Break
    EndIf
  Next
EndProcedure

Procedure WebGadget_Document_ButtonByName_Click(List htmlelement.IHTMLElement(),Name.s)
  Protected pid,ibreak,htmlbutton.IHTMLInputButtonElement
  ForEach htmlelement()
    htmlbutton = WebGadget_Document_getButton(htmlelement())
    If htmlbutton
      pid = 0
      htmlbutton\get_name(@pid)
      If pid And PeekS(pid) = Name
        htmlelement()\click()
        ibreak = 1
      EndIf
      WebGadget_FreeInterfaces(htmlbutton)
      If ibreak = 1
        Break
      EndIf
    EndIf
  Next
  ProcedureReturn ibreak
EndProcedure

Procedure WebGadget_Document_FindelEmentById(List htmlelement.IHTMLElement(),tagName.s,Id.s)
  Protected pid,ireturn = 0
  ForEach htmlelement()
    pid = 0
    htmlelement()\get_tagName(@pid)
    If pid  = 0 Or PeekS(pid) <> tagName
      Continue
    EndIf
    pid = 0
    htmlelement()\get_id(@pid)
    If pid And PeekS(pid) = Id
      ireturn = htmlelement()
      Break
    EndIf
  Next
  ProcedureReturn ireturn
EndProcedure

DisableExplicit

OpenWindow(0,0,0,640,480,"wait load..",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
WebGadget(0,0,0,640,480, "") 
WebBrowser.IWebBrowser2 = WebGadget_getWebBrowser(0)
WebBrowser\Navigate("https://www.purebasic.fr/english/ucp.php?mode=login",@"",@"",@"",@"")
WebBrowser\put_Silent(#True)

Repeat
  WindowEvent()
  If WebGadget_PageIsLoaded(WebBrowser)
    Break
  EndIf
Until EventWindow() = #PB_Event_CloseWindow

pureuser.s = "votre name"
purepassword.s = "password"

htmldocument.IHTMLDocument2 = WebGadget_getDocument(WebBrowser)
NewList htmlelement.IHTMLElement()
WebGadget_Document_GetAllElementList(htmldocument,htmlelement())
WebGadget_Document_InputTextByName_SetValue(htmlelement(),"username",pureuser)
WebGadget_Document_InputTextByName_SetValue(htmlelement(),"password",purepassword)
WebGadget_Document_ButtonByName_Click(htmlelement(),"login")
WebGadget_FreeListInterfaces(htmlelement())

Repeat:Until WindowEvent() = #PB_Event_CloseWindow

WebGadget_FreeInterfaces(htmldocument)
WebGadget_FreeInterfaces(WebBrowser)
.....i Love Pb :)
Avatar de l’utilisateur
MLD
Messages : 1103
Inscription : jeu. 05/févr./2009 17:58
Localisation : Bretagne

Re: utilisation "IWebBrowser2" avec "WebGadget"

Message par MLD »

Ecore une bonne Celtiquerie. :lol: :lol:
Merci
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: utilisation "IWebBrowser2" avec "WebGadget"

Message par Kwai chang caine »

Marche nickel 8)
Je suis passionné des pilotages de browser :wink:
Bien que le niveau du code soit pas à la portée du premier venu, on peut dire que pour une fois Microsoft a rendu relativement facile le pilotage de l'interface IE
Dommage qu'il existe pas la même chose AVEC FF et Chrome, quant on voit la galère pour utiliser les webdrivers et leur langage à dormir debout et hyper mal documentés :|
Merci une fois de plus du partage CELTIC 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Ollivier
Messages : 4190
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Re: utilisation "IWebBrowser2" avec "WebGadget"

Message par Ollivier »

Waouw ! De la balle !
Ollivier
Messages : 4190
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Re: utilisation "IWebBrowser2" avec "WebGadget"

Message par Ollivier »

Bonjour Celtic88,

c'est éblouissant ton code source.

Y a-t-il une nomenclature des configurations matérielle et logicielle requises pour réussir à démarrer ce code source?
Répondre