[转载自:http://www.chriskarcher.net/2008/01/26/hiding-the-progress-bar-of-a-net-20-cf-webbrowser]
One of the nice additions to the .NET 2.0 Compact Framework is the WebBrowser control. This control has always been present in the full framework , but to implement a web browser on the PocketPC you would have had to either write your own managed wrapper or use an existing one such as OpenNETCF’s HTMLViewer. But now that it’s included in the CF, we should give it a try. Many developers, myself included, use an embedded browser control to display rich, custom formatted content in their .NET apps. You can do this with the WebBrowser class by generating the HTML and setting the DocumentText property on the control. However, the new managed WebBrowser has a major drawback: Everytime you set the DocumentText property it shows a progress bar while loading the content into the browser. As far as I can tell, this behavior is only on Windows Mobile 5 devices, not on PocketPC 2003. Read on to figure out how to disable this behavior.
So, here’s what it looks like:
At first glance, it doesn’t look intrusive. But when you start updating the HTML regularly and the progress bar pops up every time, it starts to look out of place. It would be nice if MS just exposed an option to disable it, but they don’t. We’re going to have to do some work ourselves to get rid of it.
First, some background. In my application, I created a UserControl named WebBrowserPanel. WebBrowserPanel has a single WebBrowser child control which is anchored Top, Right, Bottom, and Left. It has the following constuctor to make it use all the available space:
- public WebBrowserPanel()
- {
- InitializeComponent();
- WebBrowser.Width = this .Width - 2;
- WebBrowser.Height = this .Height;
- }
public WebBrowserPanel() { InitializeComponent(); WebBrowser.Width = this.Width - 2; WebBrowser.Height = this.Height; }
Putting the WebBrowser inside a user control lets us give it a border and, as you’ll see later, will be crucial for this fix to work.
Using the Remote Spy tool that ships with VS 2005, we can examine the window structure of the application on the Windows Mobile 5 emulator:
The “MSPIE Status” window, a child of the “IExplore” window, looks interesting. Inspecting its properties shows us that it’s 23 pixels tall, and likely the progress bar we’re looking to hide. If we can get our hands on its window handle we can try to move, resize, or manipulate it in some other way to hide it. To get its window handle, I wrote the following function:
- public static IntPtr FindHwndByClass( string strClass, IntPtr parentHwnd)
- {
- StringBuilder sbClass = new StringBuilder(256);
- if (0 != GetClassName(parentHwnd, sbClass, sbClass.Capacity) && sbClass.ToString() == strClass)
- return parentHwnd;
- IntPtr hwndChild = GetWindow(parentHwnd, ( int )GetWindowFlags.GW_CHILD);
- while (hwndChild != IntPtr.Zero)
- {
- IntPtr result = FindHwndByClass(strClass, hwndChild);
- if (result != IntPtr.Zero)
- return result;
- hwndChild = GetWindow(hwndChild, ( int )GetWindowFlags.GW_HWNDNEXT);
- }
- return IntPtr.Zero;
- }
public static IntPtr FindHwndByClass(string strClass, IntPtr parentHwnd) { StringBuilder sbClass = new StringBuilder(256); if (0 != GetClassName(parentHwnd, sbClass, sbClass.Capacity) && sbClass.ToString() == strClass) return parentHwnd; IntPtr hwndChild = GetWindow(parentHwnd, (int)GetWindowFlags.GW_CHILD); while (hwndChild != IntPtr.Zero) { IntPtr result = FindHwndByClass(strClass, hwndChild); if (result != IntPtr.Zero) return result; hwndChild = GetWindow(hwndChild, (int)GetWindowFlags.GW_HWNDNEXT); } return IntPtr.Zero; }
We can pass in “MSPIE Status” as the class name and look for it as a child of the WebBrowser.
Now that we have the handle, we can try to hide it. My initial attempts to hide the progress bar included using SetWindowPos to move the bar off the screen, resize it to one pixel high, and a number of other hacks that didn’t work. As a last resort, I tried the ultimate: DestroyWindow . After initializing the WebBrowser in WebBrowserPanel’s constructor, I would find the status bar window and destroy it. This seemed to work, kind of. As you can see below, there are about 20 pixels of whitespace at the bottom of the browser, noticeable when you have enough content to need scrollbars:
Not good, but easy enough to work around. After we get the progress bar handle we can get its window height. After we destroy the window, we can resize the WebBrowser to be this much taller than its parent panel, thus clipping off this whitespace. WebBrowserPanel’s constructor now looks like:
- public WebBrowserPanel()
- {
- InitializeComponent();
- WebBrowser.Width = this .Width - 2;
- WebBrowser.Height = this .Height;
- IntPtr hwndStatus = FindHwndByClass( "MSPIE Status" , WebBrowser.Handle);
- if (hwndStatus != IntPtr.Zero)
- {
- RECT rectStatus = new RECT();
- GetClientRect(hwndStatus, out rectStatus);
- DestroyWindow(hwndStatus);
- WebBrowser.Height += rectStatus.Height;
- }
- }
public WebBrowserPanel() { InitializeComponent(); WebBrowser.Width = this.Width - 2; WebBrowser.Height = this.Height; IntPtr hwndStatus = FindHwndByClass("MSPIE Status", WebBrowser.Handle); if (hwndStatus != IntPtr.Zero) { RECT rectStatus = new RECT(); GetClientRect(hwndStatus, out rectStatus); DestroyWindow(hwndStatus); WebBrowser.Height += rectStatus.Height; } }
And we end up with:
Success! You can now set the DocumentText any number of times without the progress bar showing up.
The code presented above makes use of the following PInvokes and enums:
- [DllImport( "coredll.dll" )]
- public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
- [DllImport( "coredll.dll" )]
- public static extern IntPtr GetWindow(IntPtr hwnd, int cmd);
- public enum GetWindowFlags : int
- {
- GW_HWNDFIRST = 0,
- GW_HWNDLAST = 1,
- GW_HWNDNEXT = 2,
- GW_HWNDPREV = 3,
- GW_OWNER = 4,
- GW_CHILD = 5,
- GW_MAX = 5
- }
- [DllImport( "coredll.dll" )]
- public static extern bool DestroyWindow(IntPtr hwnd);
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int X;
- public int Y;
- public int Width;
- public int Height;
- }
- [DllImport( "coredll.dll" )]
- static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("coredll.dll")] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("coredll.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, int cmd); public enum GetWindowFlags : int { GW_HWNDFIRST = 0, GW_HWNDLAST = 1, GW_HWNDNEXT = 2, GW_HWNDPREV = 3, GW_OWNER = 4, GW_CHILD = 5, GW_MAX = 5 } [DllImport("coredll.dll")] public static extern bool DestroyWindow(IntPtr hwnd); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int X; public int Y; public int Width; public int Height; } [DllImport("coredll.dll")] static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);