Not been round these parts for a while. Been working on a project and have had to answer a few problems which I was struggling to find the answers for on the internet and seeing as I'm a nice fellow who likes to share I thought I'd pop back here and post it all for other people to use.
I don't know if someone would be nice enough to sort them out so they are easier to find, I'm not really too sure where to put any of it. So I'm gonna dump it here and we'll see what happens.
1. Scrolling the webbrowser control.
I'm working with pretty much a purely HTML site so I'm not too sure yet what might have to change for other sites. But anyway...
I'd tried a few API suggestions off the internet and nothing was working. I couldn't even get GetScrollInfo to work, let alone SetScrollInfo.
I was able to get the window to scroll by calling the ScrollTo method of the browser.Document.Window but I was struggling to get the current position of the scrollbar.
After checking all the HTMLelements from browser.Document.All I noticed that the HTMLelement with the tag HTML had the scrolltop property that I had set.
Therefore, to scroll the webbrowser control, let's say 20 pixels...
Dim y as integer = browser.Document.GetElementsByTagName("HTML").Item(0).ScrollTop
y += 20
2. Stop debug errors in webbrowser
The next problem I was having was with script errors showing up in the control, even though all the settings within Internet options suggested these errors wouldn't be shown.
What we need to do to catch these errors is to trap errors thrown by the Window of the Document of the webbrowser control.
Obviously the window has to be set, so the best place to put the handler is in the document complete event.
Private Sub DocumentComplete(byval sender as Object, byval e as WebbrowserDocumentCompletedEventArgs)
If browser.Document.Window IsNot Nothing then
AddHandler browser.Document.Window.Error, Address of PageError
' And the sub to catch the event
Private Sub PageError(byval sender as object, byal e as HtmlElementErrorEventArgs)
' You can do whatever you want, display the message yourself, but to stop
' the browser from showing the error just set handled to true
e.handled = True
You also need to remove the handler, and I'm doing this whenever the user is navigating to a new page (i.e. in the navigating event if I'm allowing the link). Not sure if that's the best place, but it works.
3. Fullscreen Windows Media Player control
I was having problems with fullscreen using the windows media control. Seen a lot of other people have the same problem. It's the critical error when you try to set fullscreen after it's played one video (it returns to not fullscreen after playing one video).
The simple answer to this is to only set fullscreen when it's playing.
The best thing to do is to stop autoplay, otherwise you sometimes miss the start of the clip.
MediaPlayerControl.Settings.AutoStart = False
You set the url of the clip you want to play and tell the media player to play it
MediaPlayerControl.Url = Whatever
Then, you catch the playing event in the playstate changed event of the control and set fullscreen
Private Sub PlayStateChanged(byval sender as Object, byval e as _WMPOCXEvents_PlaystateChangeEvent)
Select Case MediaPlayerControl.playstate
MediaPlayerControl.Fullscreen = True
I also find it useful to hide the media player when it has stopped to try and stop the user seeing it resize back to normal. The fullscreen sets it visible when it's hidden so that takes care of that too.
I'm sorry for any errors in the code, I've typed them out rather than copied them.
Also, apologies again for just dumping this here, but hopefully it can find a good home.