发个例子- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
-
- namespace VBScript
- {
- [ComVisible(true)]
- [Guid("FD3B7777-90F5-AC77-C8D3-1B5259B42927")]
- [InterfaceType(ComInterfaceType.InterfaceIsDual)]
- public interface IWinForm
- {
- Control GetControl(string controlname);
- void AddControl(object c, object p);
- void AddControl(object c, object[] p);
- Control[] GetControlList(object c);
- void AttachEvent(object c, string eventname, object f);
- }
-
- [ComVisible(true)]
- [Guid("2A0C7DB1-4CAF-561D-130A-2C52DB0DF12D")]
- [ClassInterface(ClassInterfaceType.None)]
- public class WinForm : IWinForm
- {
- public Control GetControl(string controlname)
- {
- return (Control)typeof(System.Windows.Forms.Form).Assembly.CreateInstance("System.Windows.Forms." + controlname, true);
- }
- public void AddControl(object c, object p)
- {
- ((Control)c).AddControl((Control)p);
- }
- public void AddControl(object c, object[] p)
- {
- ((Control)c).AddControl(p);
- }
- public Control[] GetControlList(object c)
- {
- return ((Control)c).GetControlList();
- }
- public void AttachEvent(object c, string eventname, object f)
- {
- ((Control)c).AttachEvent(eventname, f);
- }
- }
-
- public static class ExtensionMethodForWinFormInCom
- {
- public static Control[] GetControlList(this Control control)
- {
- IList<Control> list = (IList<Control>)control.Controls;
- return list.ToArray();
- }
- public static void AddControl(this Control p, Control c)
- {
- p.Controls.Add(c);
- }
- public static void AddControl(this Control p, object[] c)
- {
- List<Control> cl = new List<Control>();
- Type ctlt = typeof(Control);
- foreach (object o in c)
- {
- if (o.GetType() == ctlt)
- {
- cl.Add(o as Control);
- }
- }
- p.Controls.AddRange(cl.ToArray());
- }
- public static void AttachEvent(this Control c, string eventname, object co)
- {
- EventInfo ev = c.GetType().GetEvent(eventname);
- switch (ev.EventHandlerType.Name)
- {
- case "KeyEventHandler":
- ev.AddEventHandler(c, new KeyEventHandler((object sender, KeyEventArgs e) =>
- {
- ComFuncCaller.Invoke(co, sender, e);
- }));
- break;
- case "KeyPressEventHandler":
- ev.AddEventHandler(c, new KeyPressEventHandler((object sender, KeyPressEventArgs e) =>
- {
- ComFuncCaller.Invoke(co, sender, e);
- }));
- break;
- case "MouseEventHandler":
- ev.AddEventHandler(c, new MouseEventHandler((object sender, MouseEventArgs e) =>
- {
- ComFuncCaller.Invoke(co, sender, e);
- }));
- break;
- case "EventHandler":
- ev.AddEventHandler(c, new EventHandler((object sender, EventArgs e) =>
- {
- ComFuncCaller.Invoke(co, sender, e);
- }));
- break;
- case "PaintEventHandler":
- ev.AddEventHandler(c, new PaintEventHandler((object sender, PaintEventArgs e) =>
- {
- ComFuncCaller.Invoke(co, sender, e);
- }));
- break;
- default:
- break;
- }
- }
- public static class ComFuncCaller
- {
- public static void Invoke(dynamic o,object sender,dynamic e)
- {
- (new z(o)).zz(sender,e);
- }
- private struct z
- {
- public dynamic zz;
- public z(dynamic zzz)
- {
- zz = zzz;
- }
- }
- }
- }
- }
复制代码
|