Flash and Proxy Support on .NET WebBrowser Controls
At work, I’ve been dealing with Big Brother for the past few weeks (well, before the holiday break)—attempting to both accomplish a bit of work and some play in light of our annoying filter (blocking MSFT blogs, forums, community sites, and almost all useful search engines).
I love Daniel Mackey’s Pandora’s Box application, an encapsulation of the web browser for playing Pandora; however, it doesn’t support proxy servers. I found a few other players, but, it appears, that IE 7 screws up a few things and the proxy settings don’t work correctly. Bleh. So, okay, it’s web browser and I’m a web developer—this can’t be that hard.
Heh. If I only had a dollar for each time I said that…
Creating a Flash-enabled WebBrowser Control
One of the drawbacks to the current .NET WebBrowser control is that Flash, for some reason, can’t operate. It just sits there—staring back at you. I came across a post a few months ago to fix this. It was originally targeted at 2.0, but seems to work just fine in 3.5
The fix? Create a new user control and inherit from the WebBrowser control. The only work is to override the WndProc method:
protected override void WndProc(ref Message m)
If you’re not familiar with how Windows forms messages are processed, you can find more information on MSDN.
Important: If you’re developing on an x64 (64–bit) machine, you must change your compile target platform to x86 (32–bit). The WebBrowser control renders according to your platform—x64 in 64–bit, x86 in 32–bit. Most of us use the 32–bit version of Internet Explorer on our x64 Windows because plugins, such as Flash, only have 32–bit editions. In this case, Flash will not be able to load under the x64 WebBrowser control and will throw errors.
Implementing the Flash-enabled WebBrowser Control
I’m not real original on my control names… my derived control is called FlashWebBrowser. Good deal.
To implement the control, add a Form to your page and size accordingly. You can drag/drop your controls onto the form; however, for this example, I chose to do everything in code behind.
public partial class PandoraForm : Form
private FlashWebBrowser flashBrowser = new FlashWebBrowser();
flashBrowser.Dock = DockStyle.Fill;
flashBrowser.AllowWebBrowserDrop = false;
flashBrowser.IsWebBrowserContextMenuEnabled = false;
flashBrowser.WebBrowserShortcutsEnabled = false;
flashBrowser.ScriptErrorsSuppressed = true;
Load += new EventHandler(Form1_Load);
Information: I’d like to caveat that last statement with a note. It will USUALLY suppress those errors. For some reason, IE 7.0 seems to ignore that statement and still throws the errors. *sigh* More information can be found ala Google. I’m still researching this and hope to have a better understanding soon.
Our final method, Form1_Load as specified above, simply contains our flashBrowser’s Navigate method.
private void Form1_Load(object sender, EventArgs e)
Implementing Proxy Support for the WebBrowser Control
I haven’t cleaned this up into the FlashWebBrowser control yet, but I wanted to post this up for those interested. Proxy support is REALLY useful if you’d like a particular application or service to use a proxy server, but don’t want to deal with the hassle of constantly changing your Internet settings.
You could use the HttpWebRequest object, pass along a WebProxy, and go that route. However, quite honestly, I haven’t worked out how you can apply that to a WebBrowser control—only at the console. I’m sure it can be done and may be more secure than this method, but this forum code works.
Cleaned up, the code looks like:
public struct INTERNET_PROXY_INFO
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
[DllImport(“wininet.dll”, SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet,
int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
private void RefreshIESettings(string strProxy)
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
// Filling in structure
// Allocating memory
IntPtr intptrStruct =
// Converting structure to IntPtr
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
bool iReturn = InternetSetOption(IntPtr.Zero,
wininet.dll is the Windows library that contains most of the internet functions used by Windows and Windows-based applications. Proxy settings are not the only things configurable using this API; for more information, read here. Also, for a full understanding of what this code is doing, MSDN has a nice walkthrough demonstrating the various options and how to get/set them.
With that method added to our project, there’s only one change needed—we need to call our RefreshIESettings method prior to the Navigate method of our WebBrowser control. Our Form1_Load method now looks like:
private void Form1_Load(object sender, EventArgs e)
I want to integrate the proxy settings INTO the WebBrowser control—and still might. When that’s done, I’ll post up the code. For now, however, that’s it. We now have a WebBrowser control and a baby Windows Forms application that loads up a Flash site, like Pandora AND goes through a proxy server (so I can sit and listen to tunes at work OR be productive and access the MSFT community forums). Good deal.