`
guowee
  • 浏览: 177259 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Hiding the Progress Bar of a .NET 2.0 CF WebBrowse

阅读更多

 

[转载自: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:

progressbar.png

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:

  1. public  WebBrowserPanel()   
  2. {   
  3.     InitializeComponent();          
  4.   
  5.     WebBrowser.Width =  this .Width - 2;   
  6.     WebBrowser.Height =  this .Height;   
  7. }  
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:

handles.png

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:

  1. public   static  IntPtr FindHwndByClass( string  strClass, IntPtr parentHwnd)   
  2. {   
  3.     StringBuilder sbClass =  new  StringBuilder(256);   
  4.      if  (0 != GetClassName(parentHwnd, sbClass, sbClass.Capacity) && sbClass.ToString() == strClass)   
  5.          return  parentHwnd;          
  6.   
  7.     IntPtr hwndChild = GetWindow(parentHwnd, ( int )GetWindowFlags.GW_CHILD);   
  8.      while  (hwndChild != IntPtr.Zero)   
  9.     {   
  10.         IntPtr result = FindHwndByClass(strClass, hwndChild);   
  11.          if  (result != IntPtr.Zero)   
  12.              return  result;          
  13.   
  14.         hwndChild = GetWindow(hwndChild, ( int )GetWindowFlags.GW_HWNDNEXT);   
  15.     }          
  16.   
  17.      return  IntPtr.Zero;   
  18. }  
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:

whitespace.png

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:

  1. public  WebBrowserPanel()   
  2. {   
  3.     InitializeComponent();   
  4.   
  5.     WebBrowser.Width =  this .Width - 2;   
  6.     WebBrowser.Height =  this .Height;   
  7.   
  8.     IntPtr hwndStatus = FindHwndByClass( "MSPIE Status" , WebBrowser.Handle);   
  9.      if  (hwndStatus != IntPtr.Zero)   
  10.     {   
  11.         RECT rectStatus =  new  RECT();   
  12.         GetClientRect(hwndStatus,  out  rectStatus);   
  13.   
  14.         DestroyWindow(hwndStatus);   
  15.   
  16.         WebBrowser.Height += rectStatus.Height;   
  17.     }   
  18. }  
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:

final.png

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:

  1. [DllImport( "coredll.dll" )]   
  2. public   static   extern   int  GetClassName(IntPtr hWnd, StringBuilder lpClassName,  int  nMaxCount);   
  3.   
  4. [DllImport( "coredll.dll" )]   
  5. public   static   extern  IntPtr GetWindow(IntPtr hwnd,  int  cmd);   
  6.   
  7. public   enum  GetWindowFlags :  int   
  8. {   
  9.     GW_HWNDFIRST = 0,   
  10.     GW_HWNDLAST = 1,   
  11.     GW_HWNDNEXT = 2,   
  12.     GW_HWNDPREV = 3,   
  13.     GW_OWNER = 4,   
  14.     GW_CHILD = 5,   
  15.     GW_MAX = 5   
  16. }   
  17.   
  18. [DllImport( "coredll.dll" )]   
  19. public   static   extern   bool  DestroyWindow(IntPtr hwnd);   
  20.   
  21. [StructLayout(LayoutKind.Sequential)]   
  22. public   struct  RECT   
  23. {   
  24.      public   int  X;   
  25.      public   int  Y;   
  26.      public   int  Width;   
  27.      public   int  Height;   
  28. }   
  29.   
  30. [DllImport( "coredll.dll" )]   
  31. 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);

Download the source code

<!---->
<!----><!---->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics