Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode = 1
%UseWebBrowser = 1 ' // Use the WebBrowser control
#Include Once "CWindow.inc" ' // CWindow class
%IDC_WebBrowser = 1001
%IDC_Go = 1002
%IDC_TextBox = 1003
%IDC_Find = 1004
%IDC_GetText = 1005
%IDC_HighLight = 1006
%IDC_SearchTerm = 1007
Function PBMain
Local hDlg As Dword, bstrURL As WString
Dialog New Pixels, 0, "WebBrowser", , , 800, 1000, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Find, "Find", 10,10,50,20
Control Add Button, hDlg, %IDC_GetText, "Get Text", 70,10,50,20
Control Add Button, hDlg, %IDC_HighLight, "Highlight", 130,10,60,20
Control Add TextBox, hDlg, %IDC_SearchTerm, "you", 200,10,50,20
Control Add Button, hDlg, %IDC_Go, "Go", 260,10,50,20
Control Add TextBox, hDlg, %IDC_TextBox, "http://www.garybeene.com", 320,10,430,20
Local pWindow As IWindow
pWindow = Class "CWindow"
bstrURL = "http://www.garybeene.com"
pWindow.AddWebBrowserControl(hDlg, %IDC_WEBBROWSER, bstrURL, Nothing, 0, 40, 600,350)
Dialog Show Modal hDlg, Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pIWebBrowser2 As IWebBrowser2 'instance of the webbrowser control
Local vURL As Variant 'argument for Navigate2 method
Local hBrowser As Dword 'handle to window containing web browser
Local CGID_WebBrowser As Guid
Local pDisp As IDispatch
Local pCmdTarget As IOleCommandTarget
Local vIn As Variant
Local vOut As Variant
Local pIHTMLDocument As IHTMLDocument2
Local bstrText As WString
Local pIHTMLSelectionObject As IHTMLSelectionObject
Local pDispatch As IDispatch
Local pIHTMLTxtRange As IHTMLTxtRange
Local iRes As Integer
Local tmp$
Select Case CbMsg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Highlight
hBrowser = GetDlgItem(Cb.Hndl,%IDC_WebBrowser)
pIWebBrowser2 = OC_GetDispatch(hBrowser) 'reference to webbrowser control default interface
pIHTMLDocument = pIWebBrowser2.Document
Control Get Text Cb.Hndl, %IDC_SearchTerm To tmp$
pIHTMLSelectionObject = pIHTMLDocument.selection
pDispatch = pIHTMLSelectionObject.createRange
pIHTMLTxtRange = pDispatch
pIHTMLSelectionObject.empty
iRes = pIHTMLTxtRange.findText(tmp$, &H7FFFFFFF???, %FINDTEXT_WHOLEWORD)
Do
If iRes = 0 Then Exit Do
pIHTMLTxtRange.collapse(%VARIANT_FALSE)
iRes = pIHTMLTxtRange.findText(tmp$, &H7FFFFFFF???, %FINDTEXT_WHOLEWORD)
If iRes Then pIHTMLTxtRange.execCommand("BackColor", %VARIANT_TRUE, "Yellow")
Loop
Case %IDC_GetText
hBrowser = GetDlgItem(Cb.Hndl,%IDC_WebBrowser)
pIWebBrowser2 = OC_GetDispatch(hBrowser) 'reference to webbrowser control default interface
pIHTMLDocument = pIWebBrowser2.Document
bstrText = pIHTMLDocument.body.innerText
MsgBox bstrText
Case %IDC_Find
hBrowser = GetDlgItem(Cb.Hndl,%IDC_WebBrowser)
pIWebBrowser2 = OC_GetDispatch(hBrowser) 'reference to webbrowser control default interface
CGID_WebBrowser = Guid$("{ED016940-BD5B-11CF-BA4E-00C04FD70816}")
pDisp = pIWebBrowser2.Document
pCmdTarget = pDisp
pCmdTarget.Exec(CGID_WebBrowser, 1, 0, vIn, vOut)
Case %IDC_Go, %IdOk
hBrowser = GetDlgItem(Cb.Hndl,%IDC_WebBrowser)
pIWebBrowser2 = OC_GetDispatch(hBrowser) 'reference to webbrowser control default interface
vURL = "http://www.garybeene.com/"
pIWebBrowser2.Navigate2 vURL
End Select
Case %WM_Size
If Cb.WParam <> %Size_Minimized Then
Local w,h As Long
Dialog Get Client Cb.Hndl To w,h
Control Set Size Cb.Hndl, %IDC_WebBrowser, w, h-40
Control Set Size Cb.Hndl, %IDC_TextBox, w-320, 20
End If
End Select
End Function
'gbs_01313
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm