Hello, these are some tips that i have been learning from time to time. Most of them have something in common: they comes to light due to questions posted at EE. Basically, in Visual Basic Area.
Hope you found them useful and enjoy them as me. Thanks for reading ;).
Level: Intermediate If you want to see my previous intent to do this, click here. Web Browser Control Internet Explorer Object (By the way, you could try a little implementation of this code with above two links). Override
default popup menu - Web Browser Control Create a new
Standard exe project.Add a reference to Microsoft HTML Object Library. Add Internet Browser control to your Toolbox. Add the following controls: 1 textbox Text = "Whatever you want" 1 Command button Caption = "&Go!" Default = True 1 Webbrowser control Name = wb1 Your window should looks like the following picture: On General Declaration section of form1, we need to declare variables: Private WithEvents HDoc As HTMLDocumentWith this variable, we will manage Properties/Methods and most important: Events from our target HTML Document. The question here is: why we need a variable of type HTMLDocument? We need it because Webbrowser1.document (which is the same object type) doesn't expose their events and we need them to gain access to an important one: onmousedown .Besides, we will get Autolist Members feature from HDoc that, otherwise, we would miss from
Webbrowser.document in VB IDE.As soon as variable is created, we can access it like any other object that belongs to form1 in the Code Pane. To set HDoc to our current loaded document, we need
to set objects in the DocumentComplete event of webbrowser because until
document is fully loaded, we can't access it. More, if you want to
override menu, but only for a given location, you have to check it there
too. Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant) If (pDisp Is WB1.Object) Then Text1.Text = URL ' Change url www.experts-exchange.com to what url you want to avoid. ' Optionally, instead of hard-coded value, you could store in ' another textbox control. If UCase$(URL) = "WWW.EXPERTS-EXCHANGE.COM" Then Set HDoc = WB1.document End If End If End SubThe If (pDisp Is WB1.Object) Then... statement
is necessary due to the fact that page loaded could contains frames and
this event is fired for each of them.From here, we have plenty access to document inside webbrowser control. When we right-click on the document, the first event that is fired is onmousedown , and
it is the event we need to manage.To do things short, i trapped some tags only. You could manage all of them. This is the code from onmousedown event: Private Sub HDoc_onmousedown() ' Creating a variable to hold elements inside document object, ' and pull off those awful dots that eat resources. Dim Elem As IHTMLElement Set Elem = HDoc.parentWindow.event.srcElement With Elem ' Check if button = 2 (right button) was pressed. ' Otherwise, we don't do anything. If HDoc.parentWindow.event.button = 2 Then Dim msg As String Select Case UCase$(.tagName) Case "A" ' Change url to one of your test page ;) If UCase$(.href) = _ UCase$("http://www.experts-exchange.com/jsp/qList.jsp?ta=visualbasic") Then msg = "you right-clicked on " & vbNewLine & .href MsgBox msg, vbInformation, App.EXEName End If Case "BODY" msg = "This HTML code is mine!" Case "IMG" msg = "Imágen" Case "P" msg = "Para" Case "H1" msg = "Header: size 1" End Select Me.Caption = "Click on " & msg End If End With End SubThe following picture shows the result of do a right-click on a link in a test page (that i trapped with previous code). What we are doing there is check for value of tagName property. If
tag name is one of we already trapped, we write its type on caption of
form1, just to show that it works.The only Popup menu what we are overriding is for anchor tag, all others are trapped but not overriding. You could create your own menu (See Visual Basic help to know how) in form1 to responds to each Case
statement.Following is the code for Form_Load and Command1_Click events. Private Sub Form_Load() WB1.navigate "about:blank" End Sub Private Sub Command1_Click() With Text1 If .Text <> "" Then WB1.navigate .Text End With End SubThat's all. Little code, don't you think so?. Override
default popup menu - Internet Explorer Object Basically, it's
almost the same. Here are those diferences:Create a new exe project. Add a reference to Microsoft HTML Object Library. Add a reference to Microsoft Internet Controls. Add the following controls: 1 textbox Text = "Whatever you want" 1 Command button Enabled = False Caption = "&Go!" Default = True On General Declaration section of form1, we need to declare variables: Private WithEvents HDoc As HTMLDocument Private WithEvents IE as InternetExplorer ... WithEvents IE
... is necessary to gain access to DocumentComplete
event.In that event, we add code similar to previous one: Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant) If (pDisp Is IE) Then Text1.Text = URL ' Change url WWW.MICROSOFT.COM to what url you want to avoid. ' Optionally, Instead of hard-coded value, you could store in ' another textbox control. If UCase$(URL) = "WWW.MICROSOFT.COM" Then Set HDoc = IE.document End If End If End SubFrom here, the code is the same ... Private Sub HDoc_onmousedown() ' Creating a variable to hold elements inside document object, ' and pull off those awful dots that eat resources. Dim Elem As IHTMLElement Set Elem = HDoc.parentWindow.event.srcElement With Elem ' Check if button = 2 (right button) was pressed. ' Otherwise, we don't do anything. If HDoc.parentWindow.event.button = 2 Then Dim msg As String Select Case UCase$(.tagName) Case "A" ' Change url to one of your test page ;) If UCase$(.href) = _ UCase$("http://www.experts-exchange.com/jsp/qList.jsp?ta=visualbasic") Then msg = "you right-clicked on " & vbNewLine & .href MsgBox msg, vbInformation, App.EXEName End If Case "BODY" msg = "This HTML code is mine!" Case "IMG" msg = "Imágen" Case "P" msg = "Para" Case "H1" msg = "Header: size 1" End Select Me.Caption = "Click on " & msg End If End With End Sub...with this little diference: Private Sub Form_Load() Set IE = New InternetExplorer ' Creates new Internet Explorer Instance If Not IE is Nothing then Command1.Enabled = True With IE .navigate "about:blank" .visible = True End With End If End Sub Private Sub Command1_Click() With Text1 If .Text <> "" Then IE.navigate .Text End With End Sub ;) ©2001 - Richie Simonetti |