v-yiy
Wed Feb 18 20:06:35 CST 2004
Hi,
I'm not sure if we could derive the Control class and implement this
interface without problem, one possible way is get the IOleObject interface
reference (ok, it' needs a reflection but it's a public reflection not
private) then get the ClientSite and the WebBrowser interface. Base on my
research I can get the top level browser Location property like this way,
you may change it to get
<code>
//based on Q172763 HOWTO: Retrieve the Top-Level IWebBrowser2 Interface
from an ActiveX Control
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;
using SHDocVw;
//Use strongname to apply the customized pemission set
[assembly : AssemblyKeyFile(@"c:\key.snk")]
//Q814669 PRB: Strong Named User Controls Do Not render in Internet Explorer
[assembly: System.Security.AllowPartiallyTrustedCallers]
namespace Microsoft.Samples.WinForms.Cs.SimpleControl
{
public class SimpleControl : System.Windows.Forms.Control
{
public SimpleControl() :base()
{
Button btn = new Button();
btn.Parent = this;
btn.Text = "Button1";
btn.Location = new Point(10,10);
btn.Click += new EventHandler(Btn_OnClick);
Controls.Add(btn);
}
//from shlguid.h
Guid SID_STopLevelBrowser = new Guid(0x4C96BE40, 0x915C, 0x11CF, 0x99,
0xD3, 0x00, 0xAA, 0x00, 0x4A, 0xE8, 0x37);
Guid SID_SWebBrowserApp = typeof(SHDocVw.IWebBrowserApp).GUID;
private void Btn_OnClick(object sender, EventArgs e)
{
try
{
Guid guidIServiceProvider = typeof(IServiceProvider).GUID;
Guid guidIWebBrowser2 = typeof(SHDocVw.IWebBrowser2).GUID;
object objIServiceProvider2;
object objIWebBrowser2;
Type typeIOleObject = this.GetType().GetInterface("IOleObject",true);
//call the
method on that interface
object oleClientSite = typeIOleObject.InvokeMember("GetClientSite",
BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlags.Public,
null,this,null);
IServiceProvider serviceProvider = oleClientSite as IServiceProvider;
serviceProvider.QueryService(ref SID_STopLevelBrowser,ref
guidIServiceProvider, out objIServiceProvider2);
serviceProvider = objIServiceProvider2 as IServiceProvider;
serviceProvider.QueryService(ref SID_SWebBrowserApp, ref
guidIWebBrowser2, out objIWebBrowser2);
IWebBrowser2 webBrowser = objIWebBrowser2 as IWebBrowser2;
MessageBox.Show(webBrowser.LocationURL);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}
[
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);
}
}
</code>
Note, you still need give the ReflectionPemission and thel UnmanagedCode
Permission in order to run this sample.
Here is another KB article which might be helpful to you to find the
information you would like to get from the WebBrowser:
<INFO: Accessing the Object Model from Within an ActiveX Control>
http://support.microsoft.com/default.aspx?scid=kb;en-us;172763
Does it resolve the issue?
Let me know if you still have problem on it.
Best regards,
Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.