[转载自: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);
相关推荐
设置隐试打开PPT报错 Hiding the application window is not allowed
Seve constraints affect this process: the quantity of data to be hidden, the need for invariance of the data under conditions where a “host” signal is subject to distortions, e.g., lossy ...
精品软件工具--A CLI tool for hiding the application's icon in the
em bedding and coding are synchronous,i.e.a fusion of the inform ation data of secret and public speech. It can em — bed dynam ic data bits of secret speech into public speech, with perfect info...
ConfuserEx 支持 .NET 框架 2.0 - 4.5 和 Mono (还有其他 .NET 框架): Symbol 重命名 WPF/BAML 重命名 Control flow obfuscation Method reference hiding Anti debuggers/profilers Anti memory dumping Anti ...
Data Hiding with Deep Learning A Survey Unifying Digital Watermarking and Steganography.pdf
OPC 客户端The FactorySoft OPC Server (FSServer) DLL provides an easy-to-use interface by hiding the details that are common to most OPC servers. The division between application and FSServer DLL is at...
In the case of Visual Basic 6 and earlier, the main strength of the language was the fact that it was simple to understand and didn’t make many programming tasks easy, largely hiding the details of ...
To shorten the time of building link and exchanging protocol, this paper presents a technology for fast cross-identification. To enhance the security of communication, encryption should be done ...
The Role of C# in the .NET Enterprise Architecture 24 Summary 26 Chapter 2: C# Basics 29 Before We Start 30 Our First C# Program 30 The Code 30 Compiling and Running the Program 31 Contents A Closer ...
The practice of programming is more than just writing code. Programmers must also assess tradeoffs, choose among design alternatives, debug and test, improveperformance, and maintain software written ...
The main idea of this work is to create a driver for hiding selected processes and files.
lsb code..which helps in hiding the secret image within another image...so the image will be secured the intruder.
watermarking is the process of hiding information in the digital image.
Abstract-Traditional speech information hiding methods have several disadvantages, for example, constant embedding ...the performance of this method is better than that of the traditional methods.
- Displaying the icon of a GameObject - Showing / hiding a GameObject - Locking / unlocking a GameObject - Prevent selection of locked GameObject - Displaying tag and layer of a GameObject - ...
And Now, the Rest of the Story What You Get Out of It Inside the Manifest In the Beginning, There Was the Root, And It Was Good Permissions, Instrumentations, and Applications (Oh My!) Your ...
This file Image stegenography is one of the best method of information hiding i.e. text in an image
The extraction of binary information hiding algorithm based on image segmentation
In this paper a information hiding algorithm applied to compressed speech bitstream is proposed. Covering codes are used to hide information in the least important bits in G.729 frames. The embedding ...