Change target of links in WebBrowser control - vb.net

Sometimes when I click on a link, it opens in a new window. Can I prevent this from happening and just load the page in my current window?

This is working for me (so far):
Private Sub browser_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles browser.NewWindow
e.Cancel = True
browser.Navigate(browser.StatusText)
End Sub
Really simple, if the status text is the url (which it always is for the documents I am browsing)

If you are targeting Windows XP SP2 or later, you can catch the NewWindow3 event to get the target url, and after setting the cancel parameter, navigate to the url instead.
For earlier versions of IE you can catch the obsolete NewWindow event with the same handling as documented in Q185538 How To Cause Navigation to Occur in Same WebBrowser Window

Related

How to make an invisible internet explorer?

I have been playing around with VB today and was wondering on the concept of being able to load a webpage in this case radio 1 online player and but having no open browsers visible to the user even though there will be one hidden one in this case this one. Currently I have it set up to simply navigate to the site but was wondering if it was possible to do what was stated above.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim QueryAddress As New StringBuilder()
QueryAddress.Append("http://www.bbc.co.uk/radio/player/bbc_radio_one")
Process.Start(QueryAddress.ToString())
Visible = False
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Unable to station")
End Try
End Sub
If you want to play a radio station you might be better using media player and connecting to the station's radio stream.
Drop windows media player onto your form.
Set its Visible property to False.
add a button with this code inside the button's click event
AxWindowsMediaPlayer1.URL = "http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_med/llnw/bbc_radio_one.m3u8"
And of course your try..catch code as well.
Tada!
I got the url from this website by clicking on the link in the second colum and then right clicking on the data rate that I wanted and copying the linked addres. I then pasted it into the vb.net code that you see

Permanently disable Windows Aero using vb.net

What I want to do is disable windows aero permanently for the user if he / she clicks a button.
Currently I have this code:
<System.RunTime.InteropServices.DLLImport("dwmapi.dll", PreserveSig:=False)>
Public Shared Sub DwmEnableComposition(bEnable As Boolean)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DwmEnableComposition(False)
End Sub
The current problem with this code is... It works, but whenever you exit your application Aero switches back on.
Is there a way to prevent this?
It sounds like in windows 8 aero transparency is disabled by default.
http://www.howtogeek.com/howto/windows-vista/disable-aero-on-windows-vista/
Also, from the link: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969510%28v=vs.85%29.aspx
Disabling DWM composition disables it for the entire desktop. DWM composition will be automatically enabled when all processes that have disabled composition have called DwmEnableComposition to enable it or have been terminated. The WM_DWMCOMPOSITIONCHANGED notification is sent whenever DWM composition is enabled or disabled.
So it sounds to me like you need to leave your program running. You could do something like minimize it to the task tray (as opposed to exiting it ). Who knows, maybe you can even just launch a separate executable that stays running and doesn't show up in the taskbar OR task tray. But you will have to leave the process running.
If you want Aero to be disabled "PERMANENTLY" for the user regardless of whether your app is running or not, then you'll need to identify where this setting lives in the registry and modify that setting.
If you meant that you only want to disable it for your application (meaning that the next time you launch the app, it will be either enabled or disabled based on how it was set when it was last closed), then you will need to store this in a user setting.
Create a new bool setting (user-scope) in the project settings and call it DWM. Then, in the button click event toggle it's value. Apply that value to the setting at runtime.
EDIT: The previous version would have resulted in an always false setting.
I've edited the If statements to an If-Else block.
<System.RunTime.InteropServices.DLLImport("dwmapi.dll", PreserveSig:=False)>
Public Shared Sub DwmEnableComposition(bEnable As Boolean)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If My.Settings.DWM = False THEN
My.Settings.DWM = True
Else
My.Settings.DWM = False
End If
My.Settings.Save()
DwmEnableComposition(My.Settings.DWM)
End Sub
In the onload or startup event of the form/app, run the code to set true or false
DwmEnableComposition(My.Settings.DWM)

Enable Tab Key in my Web Browser Control

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.
Cheers.
Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.
For that change the WebBrowser contructor from
new System.Windows.Forms.WebBrowser();
to
new Microsoft.Office.Tools.Excel.Controls.WebBrowser();
You would need to add references to your project:
Project/Add Referernce/Extensions select
Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities
Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.
It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.
Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)
Me.Focus()
TestWebBrowser.Navigate(url)
WaitForPageLoad()
TestWebBrowser.TabIndex = 0
TestWebBrowser.TabStop = True
End Sub
Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted
' Now set the focus on the Web Browser Control
TestWebBrowser.Focus()
End Sub

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto

How to determine URL about to be opened by the NewWindow event?

I'm trying to build a simple desktop application to keep users on a certain website. It's very simple, just a webbrowser control with right click disabled. However certain applications on our website require popup windows and those windows always open up a full browser window.
Private Sub WebBrowser1_NewWindow(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'What will the URL of this new window be?
Console.WriteLine("New Window maybe to " & WebBrowser1.Url.AbsoluteUri)
End Sub
Catching the newwindow event seems straight forward, but the event does not specify the URL that is being passed to the new window. I have seen some mention of a "NewWindow2" event, but vb.net does not appear to have anything like that.
How can I get the URL that is about to be opened in a new window?
The link is generated from a normal web application and not through a webbrowser.navigate call so it could be just about anything.
Thank you
You can catch the NewWindow2 event, create a new browser instance, host it in a new form or tab and pass it to ppDisp, then handle BeforeNavigate2 to get the new url. You can cancel the navigation if necessary. See Handle the BeforeNavigate2 event in the new window to get the url. for details.