程序博客网 > 杭州行知小学校训
来源:互联网 发布:杭州行知小学校训 编辑:程序博客网 时间:2024/04/29 12:52
/// <summary> /// Summary description for IEAutomation. /// </summary> public class IEAutomation : IDisposable { /// <summary> /// Instance of the InternetExplorer.Application raw COM Object /// </summary> public object Application { get { if (!this.IsAlive()) this.Load(); return this.IE; } } private object IE = null; private int IEHwnd = 0; public int Top { get { return (int)wwUtils.GetProperty(this.IE, "Top"); } set { wwUtils.SetProperty(this.Application, "Top", value); } } public int Left { get { return (int)wwUtils.GetProperty(this.Application, "Left"); } set { wwUtils.SetProperty(this.Application, "Left", value); } } public int Height { get { return (int)wwUtils.GetProperty(this.Application, "Height"); } set { wwUtils.SetProperty(this.Application, "Height", value); } } public int Width { get { return (int)wwUtils.GetProperty(this.Application, "Width"); } set { wwUtils.SetProperty(this.Application, "Width", value); } } public bool AddressBar { get { return (bool)wwUtils.GetProperty(this.Application, "AddressBar"); } set { wwUtils.SetProperty(this.Application, "AddressBar", value); } } public bool Toolbar { get { return (bool)wwUtils.GetProperty(this.Application, "Toolbar"); } set { wwUtils.SetProperty(this.Application, "Toolbar", value); } } public bool StatusBar { get { return (bool)wwUtils.GetProperty(this.Application, "StatusBar"); } set { wwUtils.SetProperty(this.Application, "StatusBar", value); } } public bool Visible { get { return (bool)wwUtils.GetProperty(this.Application, "Visible"); } set { wwUtils.SetProperty(this.Application, "Visible", value); } } public IEAutomation() { } /// <summary> /// Internally loads the IE COM object /// </summary> private void Load() { Type loT = Type.GetTypeFromProgID("InternetExplorer.Application"); this.IE = Activator.CreateInstance(loT); this.IEHwnd = (int)wwUtils.GetProperty(Application, "hwnd"); } /// <summary> /// Check whether the IE is still alive /// </summary> /// <returns></returns> public bool IsAlive() { if (this.IE == null) return false; try { wwUtils.GetProperty(this.IE, "Top"); } catch { return false; } return true; } /// <summary> /// Navigates to a specific URL /// </summary> /// <param name="Url"></param> public void Navigate(string Url) { // *** Make sure user hasn't closed IE - if so reload if (!this.IsAlive()) { // Reload this.Load(); this.Show(); } wwUtils.CallMethod(this.Application, "Navigate", Url); } /// <summary> /// Make internet Explorer Visible /// </summary> public void Show() { wwUtils.SetProperty(Application, "visible", true); this.BringToFront(); } [DllImport("user32.dll")] public static extern int SetForegroundWindow(int hwnd); /// <summary> /// Brings IE to the top of the Window Stack /// </summary> public void BringToFront() { SetForegroundWindow(this.IEHwnd); } #region IDisposable Members public void Dispose() { if (this.IE != null) { try { this.Visible = false; } catch { ; } Marshal.ReleaseComObject(this.Application); this.IE = null; } } #endregion }
public class wwUtils { /// <summary> /// Retrieve a dynamic 'non-typelib' property /// </summary> /// <param name="loObject"></param> /// <param name="lcProperty"></param> /// <returns></returns> public static object GetProperty(object loObject, string lcProperty) { return loObject.GetType().InvokeMember(lcProperty, BindingFlags.GetProperty, null, loObject, null); } /// <summary> /// Set Property dynamically /// </summary> /// <param name="loObject">The object to set this property on.</param> /// <param name="lcProperty">The name of the property to set</param> /// <param name="loValue">The value to set it to</param> /// <returns>nothing</returns> public static void SetProperty(object loObject, string lcProperty, params object[] loValue) { loObject.GetType().InvokeMember(lcProperty, BindingFlags.SetProperty, null, loObject, loValue); } /// <summary> /// Wrapper method to call a 'dynamic' (non-typelib) method /// on a COM object /// </summary> /// <param name="loParams"></param> /// 1st - Method name, 2nd - 1st parameter, 3rd - 2nd parm etc. /// <returns></returns> public static object CallMethod(object loObject, string lcMethod, params object[] loParams) { return loObject.GetType().InvokeMember(lcMethod, BindingFlags.InvokeMethod, null, loObject, loParams); } }
object add = vWindowType.InvokeMember("add", BindingFlags.InvokeMethod, null, vWindow, new object[] { 1, 2 }); // 执行方法 object x = vWindowType.InvokeMember("x", BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取'
var x = 2;function add(a,b) { return a+b; }