从HWnd得到IWebbrowser2接口 有没有C#版的?

阿良chjlcn 2008-09-17 06:29:18
转自http://www.zxboy.com/article.asp?id=100
谁可以将下面的代码改为C#的

IWebBrowser2* GetIEFromHWnd(HWND hIEWindow)
{ HWND hWnd ;
if(hIEWindow==NULL){
hWnd= FindWindow("IEFrame", NULL);
if(hWnd==NULL)
hWnd= FindWindow("CabinetWClass", NULL);
if( hWnd == NULL){
MessageBox (NULL,"No Running instance of Internet Explorer!","message", MB_OK);
}
// walk Shell DocObject View->Internet Explorer_Server
HWND hWndChild = FindWindowEx(hWnd, 0, "Shell DocObject View", NULL);
if(hWndChild !=0){
hWndChild = FindWindowEx(hWndChild, 0, "Internet Explorer_Server", NULL);
}
hWnd=hWndChild;
}
else{
hWnd=hIEWindow;
}
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = LoadLibrary( _T("OLEACC.DLL") );
IWebBrowser2* pWebBrowser2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){

LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL ){
HRESULT hr;
CComPtr<IHTMLDocument2>spDoc;

hr=pfObjectFromLresult(lRes,IID_IHTMLDocument2,0,(void**)&spDoc);
if ( SUCCEEDED(hr) ){

CComPtr<IHTMLWindow2>spWnd2;
CComPtr<IServiceProvider>spServiceProv;
hr=spDoc->get_parentWindow ((IHTMLWindow2**)&spWnd2);
if(SUCCEEDED(hr)){

hr=spWnd2->QueryInterface (IID_IServiceProvider,(void**)&spServiceProv);
if(SUCCEEDED(hr)){
hr = spServiceProv->QueryService(SID_SWebBrowserApp,
IID_IWebBrowser2,
(void**)&pWebBrowser2);


}
}
}

}
}

::FreeLibrary(hInst);
}
else{//如果没有安装MSAA
MessageBox(NULL,_T("Please Install Microsoft Active Accessibility"),"Error",MB_OK);
}
return pWebBrowser2;
}
...全文
433 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿良chjlcn 2008-09-17
  • 打赏
  • 举报
回复
ZengHD 转一下看看你的方法是怎样的好吗?
ZengHD 2008-09-17
  • 打赏
  • 举报
回复
楼上有答案了,我就不转了

exy337 2008-09-17
  • 打赏
  • 举报
回复
看贴留个名
阿良chjlcn 2008-09-17
  • 打赏
  • 举报
回复
搞出来了,贴出来给大家参考参考
http://www.chenjiliang.com/Article/View.aspx?ArticleID=12214&TypeID=34
yagebu1983 2008-09-17
  • 打赏
  • 举报
回复
没弄过,关注!!
花落_ 2008-09-17
  • 打赏
  • 举报
回复
ding....,怪难
lovehongyun 2008-09-17
  • 打赏
  • 举报
回复
汗...
ZengHD 2008-09-17
  • 打赏
  • 举报
回复
终于转过来了,困难啊

引用:Microsoft HTML Object Library 和 Microsoft Internet Controls


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using SHDocVw;
using mshtml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[ComImport, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
void QueryService(ref Guid guidService,
ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}

[DllImport("kernel32.dll")]
public static extern int LoadLibrary(string lpLibFileName);

[DllImport("User32.dll")]
public static extern int RegisterWindowMessage(string message);

[DllImport("User32.dll")]
public static extern int SendMessageTimeout(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam, int fuFlags, int uTimeout, out UIntPtr lpdwResult);

[DllImport("oleacc.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern object ObjectFromLresult(UIntPtr lResult, [MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam);

[DllImport("oleacc.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern Int32 ObjectFromLresult(Int32 lResult, ref Guid riid, Int32 wParam, out IHTMLDocument2 ppvObject);

[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, ref int lParam);

[DllImport("OLEACC.DLL", EntryPoint = "ObjectFromLresult")]
public static extern int ObjectFromLresult(
int lResult,
ref System.Guid riid,
int wParam,
[System.Runtime.InteropServices.MarshalAs(UnmanagedType.Interface), System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref System.Object ppvObject
//注意这个函数ObjectFromLresult的声明.
);
public const int SMTO_ABORTIFHUNG = 0x2;


private void button1_Click(object sender, EventArgs e)
{

IntPtr ptr1 = FindWindow("IEFrame", null);
if (ptr1 == null)
return;

IntPtr ptr2 = FindWindowEx(ptr1, (IntPtr)0, "Shell DocObject View", null);
if (ptr2 == null)
return;

IntPtr ptr3 = FindWindowEx(ptr2, (IntPtr)0, "Internet Explorer_Server", null);
if (ptr3 == null)
return;

IHTMLDocument2 doc;
IHTMLWindow2 win;
GetIEFromHWnd(ptr3, out win, out doc);

if (doc == null || win == null)
{
MessageBox.Show("出错");
}
else
{
win.navigate("http://hi.csdn.net/ZengHD");
}
}

static void GetIEFromHWnd(IntPtr hwndInterneExplorer_Server, out IHTMLWindow2 window, out IHTMLDocument2 document)
{
window = null;
document = null;

Guid IID_IWebBrowser2 = typeof(SHDocVw.IWebBrowser2).GUID;

Guid IID_IServiceProvider = typeof(IServiceProvider).GUID;
Guid IID_IHTMLDocument2 = typeof(IHTMLDocument2).GUID;

Guid IID_IHTMLWindow2 = typeof(IHTMLWindow2).GUID;

Guid SID_SWebBrowserApp = typeof(SHDocVw.IWebBrowserApp).GUID;

try
{
int hInst = LoadLibrary("OLEACC.DLL");

int nMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");

UIntPtr lRes = UIntPtr.Zero;
int tempInt = 0;
SendMessageTimeout(hwndInterneExplorer_Server, nMsg, new IntPtr(), new IntPtr(), SMTO_ABORTIFHUNG, 1000, out lRes);

if (!(bool)(lRes.ToUInt32() == 0))
{
int hr = ObjectFromLresult((int)lRes.ToUInt32(),
ref IID_IHTMLDocument2, 0, out document);

if ((document != null))
{
IntPtr puk = Marshal.GetIUnknownForObject(document);

IServiceProvider pro = document as IServiceProvider;
//Console.Write((pro == null).ToString());


object objIWebBrowser2;
pro.QueryService(ref SID_SWebBrowserApp, ref IID_IWebBrowser2, out objIWebBrowser2);
IWebBrowser2 web = objIWebBrowser2 as IWebBrowser2;

object objHtmlWindow;
pro.QueryService(ref IID_IHTMLWindow2, ref IID_IHTMLWindow2, out objHtmlWindow);
window = objHtmlWindow as IHTMLWindow2;
}
}
}
catch
{
}
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧