110,579
社区成员
发帖
与我相关
我的任务
分享
using PARAM = System.IntPtr;
using MESSAGE = System.Int32;
using RETURN = System.Int32;
////////////////////////////////
[DllImport("user32.dll")]
protected static extern IntPtr GetWindowLong(int hwindow, int unindex);
[DllImport("user32.dll")]
static extern RETURN CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, MESSAGE Msg, PARAM wParam, PARAM lParam);
[DllImport("user32.dll")]
protected static extern int SetWindowLong(int hwindow, int unindex, WndProcDelegate lnewvalue);
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com.hk/");
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);//必需在文档加载完后才能查找窗口成功
}
bool add = false;
IntPtr oldWindow = IntPtr.Zero;
const int GWL_WNDPROC = -4;
public delegate RETURN WndProcDelegate(IntPtr hWnd, MESSAGE msg, PARAM wParam, PARAM lParam);
public WndProcDelegate MyCallWindowProc;
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(add)
{ return; }
add = true;
FindWindow fw = new FindWindow(this.Handle, "Internet Explorer_Server");
IntPtr hIE = fw.FoundHandle;
if (hIE.ToInt32() != 0)
{
oldWindow = GetWindowLong(hIE.ToInt32(), GWL_WNDPROC);
MyCallWindowProc = new WndProcDelegate(WndProc);
SetWindowLong(hIE.ToInt32(), GWL_WNDPROC, MyCallWindowProc);
}
}
public RETURN WndProc(IntPtr hWnd, MESSAGE Msg, PARAM WParam, PARAM LParam)
{
const int WM_ERASEBKGND = 0x14;
if (Msg == WM_ERASEBKGND)
{
return 1;
}
return CallWindowProc(oldWindow, hWnd, Msg, WParam, LParam);
}
class FindWindow
{
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
//IMPORTANT : LPARAM must be a pointer (InterPtr) in VS2005, otherwise an exception will be thrown
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
//the callback function for the EnumChildWindows
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
//if found return the handle , otherwise return IntPtr.Zero
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
private string m_classname; // class name to look for
private IntPtr m_hWnd; // HWND if found
public IntPtr FoundHandle
{
get { return m_hWnd; }
}
// ctor does the work--just instantiate and go
public FindWindow(IntPtr hwndParent, string classname)
{
m_hWnd = IntPtr.Zero;
m_classname = classname;
FindChildClassHwnd(hwndParent, IntPtr.Zero);
}
private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty);
if (hwnd != IntPtr.Zero)
{
this.m_hWnd = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd
return true;// keep looking
}
}