186 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			186 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.CodeDom.Compiler;
 | |
| using Microsoft.CSharp;
 | |
| using System.Reflection;
 | |
| using DCSoft.Writer.Controls;
 | |
| using DCSoft.Writer;
 | |
| using DCSoft.Writer.Dom;
 | |
| using System.Windows.Forms;
 | |
| 
 | |
| namespace DocumentManagement
 | |
| {
 | |
|     public class EventCodeCompiler
 | |
|     {
 | |
|         public CompilerResults cr;
 | |
| 
 | |
|         public Type type;
 | |
| 
 | |
|         private ElementEventTemplate eet;
 | |
| 
 | |
|         //string strMouseClickEvent = string.Empty;
 | |
| 
 | |
|         //string strContentChangedEvent = string.Empty;
 | |
| 
 | |
|         public EventCodeCompiler(ref WriterControl myEditControl, string strMouseClickEvent, string strContentChangedEvent)
 | |
|         {
 | |
|             eet = new ElementEventTemplate();
 | |
|             eet.MouseClick += new ElementMouseEventHandler(eetChk_MouseClick);
 | |
|             eet.ContentChanged += new ContentChangedEventHandler(eet_ContentChanged);
 | |
|             myEditControl.GlobalEventTemplate_Element = eet;
 | |
| 
 | |
|             var providerOptions = new Dictionary<string, string>();
 | |
|             providerOptions.Add("CompilerVersion", "v4.0");
 | |
|             CodeDomProvider cdp = new CSharpCodeProvider(providerOptions);
 | |
|             CompilerParameters cp = new CompilerParameters();
 | |
|             cp.GenerateExecutable = false;
 | |
|             cp.GenerateInMemory = true;
 | |
|             cp.ReferencedAssemblies.Add("mscorlib.dll");
 | |
|             cp.ReferencedAssemblies.Add("System.dll");
 | |
|             cp.ReferencedAssemblies.Add("System.Core.dll");
 | |
|             cp.ReferencedAssemblies.Add("DCSoft.Writer.dll");
 | |
|             cp.ReferencedAssemblies.Add("System.Data.Linq.dll");
 | |
|             cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
 | |
|             cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
 | |
|             string source = @"
 | |
| namespace DocumentManagement
 | |
| {
 | |
|                 using System;
 | |
|                 using System.Collections.Generic;
 | |
|                 using System.Linq;
 | |
|                 using DCSoft.Writer;
 | |
|                 using DCSoft.Writer.Dom;
 | |
|                 using System.Windows.Forms;
 | |
|                 using System.Reflection;
 | |
|     public class BindEvents
 | |
|     {
 | |
|         private static XTextDocument doc;
 | |
|         private static XTextContainerElement container;
 | |
|         public BindEvents(DCSoft.Writer.Controls.WriterControl myEditControl)
 | |
|         {
 | |
|             doc = myEditControl.Document;
 | |
|             container = myEditControl.Document;
 | |
|             
 | |
|         }
 | |
|         public static void MouseClickEvent(XTextElement element){
 | |
| 
 | |
| //            var pName = element.GetType().GetProperty(""Name"").GetValue(element,null).ToString();
 | |
| //            if(pName == null) return;
 | |
| //            if ((pName == ""高血压"" || pName == ""冠心病"" || pName == ""瓣膜病"" || pName == ""心律失常"") && GetCheckedValue(pName))
 | |
| //            {
 | |
| //                SetCheckedValue(""心血管系统正常"",false);
 | |
| //            }
 | |
|         " + strMouseClickEvent + @"
 | |
|         }
 | |
| 
 | |
|         public static void ContentChangedEvent(XTextElement element)
 | |
|         {
 | |
|            " + strContentChangedEvent + @" 
 | |
|         }
 | |
| 
 | |
|         private static void SetValueById(string Id, string type, object value)
 | |
|         {
 | |
|             var element = doc.GetElementsById(Id).FirstElement;
 | |
|             if (element != null)
 | |
|             {
 | |
|                 var pChecked = element.GetType().GetProperty(type);
 | |
|                 pChecked.SetValue(element, value, null);
 | |
|                 element.EditorRefreshView();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private static void SetValueByName(string name, string type,object value)
 | |
|         {
 | |
|             var element = doc.GetElementsByName(name).FirstElement;
 | |
|             if (element != null)
 | |
|             {
 | |
|                 var pChecked = element.GetType().GetProperty(type);
 | |
|                 pChecked.SetValue(element, value, null);
 | |
|                 element.EditorRefreshView();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private static bool GetCheckedValueByName(string name)
 | |
|         {
 | |
|             bool? result = null;
 | |
|             var element = doc.GetElementsByName(name).FirstElement;
 | |
|             if (element != null)
 | |
|             {
 | |
|                 var pChecked = element.GetType().GetProperty(""Checked"");
 | |
|                 result = Convert.ToBoolean(pChecked.GetValue(element, null));
 | |
|             }
 | |
|             return result.Value;
 | |
|         }
 | |
|     }
 | |
| }
 | |
| ";
 | |
|             cr = cdp.CompileAssemblyFromSource(cp, source);
 | |
|             if (cr.Errors.HasErrors)
 | |
|             {
 | |
|                 MessageBox.Show("加载元素事件失败!");
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 Assembly a = cr.CompiledAssembly;
 | |
|                 type = a.GetTypes().Where(x => x.IsClass).FirstOrDefault();
 | |
|                 Type[] types = new Type[1];
 | |
|                 types[0] = typeof(WriterControl);
 | |
|                 ConstructorInfo ci = type.GetConstructor(types);
 | |
|                 if (ci != null)
 | |
|                 {
 | |
|                     object obj = ci.Invoke(new object[] { myEditControl });
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public CompilerErrorCollection Errors { get { return cr.Errors; } }
 | |
| 
 | |
|         void eet_ContentChanged(object eventSender, ContentChangedEventArgs args)
 | |
|         {
 | |
|             if (args.Element is XTextInputFieldElement)
 | |
|             {
 | |
|                 //var element = args.Element as XTextInputFieldElement;
 | |
|                 try
 | |
|                 {
 | |
|                     Object rslt = type.InvokeMember(
 | |
|                                "ContentChangedEvent",
 | |
|                              BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
 | |
|                                   null,
 | |
|                                   null, // for static class
 | |
|                                   new object[] { args.Element });
 | |
|                 }
 | |
|                 catch (Exception exp)
 | |
|                 {
 | |
|                     MessageBox.Show(exp.StackTrace + ":" + exp.Message);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         void eetChk_MouseClick(object eventSender, ElementMouseEventArgs args)
 | |
|         {
 | |
|             if (args.Element is XTextCheckBoxElement || args.Element is XTextRadioBoxElement)
 | |
|             {
 | |
|                 try
 | |
|                 {
 | |
|                     Object rslt = type.InvokeMember(
 | |
|                                "MouseClickEvent",
 | |
|                              BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
 | |
|                                   null,
 | |
|                                   null, // for static class
 | |
|                                   new object[] { args.Element });
 | |
|                 }
 | |
|                 catch (Exception exp)
 | |
|                 {
 | |
|                     MessageBox.Show(exp.StackTrace + ":" + exp.Message);
 | |
|                 }
 | |
|                 //var doc = myEditControl.Document;
 | |
|                 //var element = args.Element;
 | |
| 
 | |
|             }
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 |