Niejako uzupełniając zagadnienie poruszone w wpisie Blokada ContextMenu na komponencie WebBrowser chcę Wam przedstawić sposób ukrycia paska postępu ładowania strony. Pojawia się on zawsze w dolnej części komponentu WebBrowser i czasami może być denerwujący :).
Całość zagadnienia sprowadza się do wywołania funkcji DestroyWindow z biblioteki coredll.dll .
Kod wygląda następująco (podobnie jak poprzednio tworze własną kontrolkę dziedzicząc po WebBrowser - wydaje mi sie to najprostszym rozwiązaniem)
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Dudzik.Helpers
{
public class WebBrowserExt : System.Windows.Forms.WebBrowser
{
private IntPtr child, pieHtml;
[DllImport("coredll")]
extern static IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint = "GetWindow")]
private static extern IntPtr GetWindow(IntPtr handleWindow, int cmd);
[DllImport("coredll.dll")]
private static extern bool DestroyWindow(IntPtr hwnd);
protected override void OnHandleCreated(EventArgs e)
{
IntPtr webHandle = GetHWND(this);
child = GetWindow(webHandle, 5);
pieHtml = GetWindow(child, 5); //MSPIE Status
DestroyWindow(pieHtml);
base.OnHandleCreated(e);
}
private IntPtr GetHWND(Control ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
return hWnd;
}
}
}
Oczywiście nic nie stoi na przeszkodzie by połączyć to z ukrywaniem ContextMenu. Uzyskany kod powinien wyglądać następująco
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Dudzik.Helpers
{
public class WebBrowserExt : System.Windows.Forms.WebBrowser
{
private IntPtr child, pieHtml;
private static WndProcDelegate newWndProc;
private static IntPtr oldWndProc = IntPtr.Zero, oldSipProc = IntPtr.Zero;
internal const int GWL_WNDPROC = -4;
internal const int WM_LBUTTONUP = 0x0202;
[DllImport("coredll")]
extern static IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint = "GetWindow")]
private static extern IntPtr GetWindow(IntPtr handleWindow, int cmd);
[DllImport("coredll.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);
[DllImport("coredll.dll")]
private static extern int PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("coredll.dll")]
private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("coredll.dll")]
private static extern bool DestroyWindow(IntPtr hwnd);
delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
protected override void OnHandleCreated(EventArgs e)
{
IntPtr webHandle = GetHWND(this);
child = GetWindow(webHandle, 5);
pieHtml = GetWindow(child, 5); //MSPIE Status
child = GetWindow(pieHtml, 2); //PIEHTML
newWndProc = new WndProcDelegate(WndProc);
oldWndProc = GetWindowLong(child, GWL_WNDPROC);
int success = SetWindowLong(child, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));
DestroyWindow(pieHtml);
base.OnHandleCreated(e);
}
private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == 0x0020)
{
PostMessage(this.Handle, WM_LBUTTONUP, 0, 0);
return IntPtr.Zero;
}
return CallWindowProc(oldWndProc, child, msg, wParam, lParam);
}
private IntPtr GetHWND(Control ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
return hWnd;
}
}
}