--------------------------------MyGrid.cs----------------------------------
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace CursorLib
{
public partial class MyGrid : Grid
{
public MyGrid()
{
this.Loaded += OnLoaded;
if (this.InitCodes())
this.RunDynamicCodes();
}
// Begin Set MyButton.Cursor according to the value of ButtonCursor
public static DependencyProperty ButtonCursorProperty = null;
// Begin get/set real cursors
public static void OnButtonCursorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyGrid fe = (MyGrid)d;
fe.SetElementCursor(typeof(Button), MyCursors.GetCursor(cursorTypeName));
}
// Used in all kind of FrameworkElements
private void SetElementCursor(Type elementType, Cursor cursorValue)
{
for (int i = 0; i < this.Children.Count; i++)
{
if (this.Children[i].GetType() == elementType)
((FrameworkElement)this.Children[i]).Cursor = cursorValue;
}
}
// End get/set real cursors
private void OnLoaded(object obj, RoutedEventArgs args)
{
MethodInfo method = dynamicCodesObj.GetType().GetMethod("ResetProperty");
method.Invoke(dynamicCodesObj,null);
MessageBox.Show("ButtonCursorProperty:" + GetValue(ButtonCursorProperty).ToString());
}
// Begin Dynamic Codes
private bool InitCodes()
{
StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DynamicCodeFile.txt");
if (sr != null)
{
this.dynamicCodes = sr.ReadToEnd();
return true;
}
else
{
MessageBox.Show("Can not input DynamicCodeFile.txt\n");
return false;
}
}
private void RunDynamicCodes()
{
CompilerResults cr = this.CompileCodes();
Assembly assembly = cr.CompiledAssembly;
if (dynamicCodesObj==null)
dynamicCodesObj = assembly.CreateInstance("CursorLib.DynamicCodes");
MethodInfo method = null;
method = dynamicCodesObj.GetType().GetMethod("GetProperty");
if (ButtonCursorProperty==null)
// Codes can only run till here in Blend***********************Project failed here***************************
ButtonCursorProperty = (DependencyProperty)method.Invoke(dynamicCodesObj, null);
method = dynamicCodesObj.GetType().GetMethod("GetCursorType");
object[] args=new object[1];
args[0]="Hand";
object objCursorType = method.Invoke(dynamicCodesObj,args);
if (objCursorType!=null)
SetValue(ButtonCursorProperty, objCursorType);
method = dynamicCodesObj.GetType().GetMethod("GetCursorTypeName");
cursorTypeName = (String)method.Invoke(dynamicCodesObj, null);
MessageBox.Show("cursorTypeName:" + cursorTypeName);
}
private CompilerResults CompileCodes()
{
// 1.CSharpCodePrivoder
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.ICodeComplier
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
// 3.CompilerParameters
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("PresentationCore.dll");
objCompilerParameters.ReferencedAssemblies.Add("PresentationFramework.dll");
objCompilerParameters.ReferencedAssemblies.Add("WindowsBase.dll");
objCompilerParameters.ReferencedAssemblies.Add("CursorLib.dll");
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
// 4.CompilerResults
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, this.dynamicCodes);
if (cr.Errors.HasErrors)
{
MessageBox.Show("Error in Compiling:");
foreach (CompilerError err in cr.Errors)
{
MessageBox.Show(err.ErrorText);
}
throw new Exception("Error in Compiling!!!");
return null;
}
else
{
return cr;
}
}
// End Dynamic Codes
private String dynamicCodes = null;
private static String cursorTypeName = null;
private static object dynamicCodesObj=null;
}
}
-------------------------------MyCursors.cs------------------------------------
using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
namespace CursorLib
{
// Cursors offering to the designers
public class MyCursor
{
private String cursorName = null;
private Cursor cursorValue = null;
public MyCursor(String cursorName, Cursor cursorValue)
{
this.cursorName = cursorName;
this.cursorValue = cursorValue;
}
public String CursorName
{
get { return this.cursorName; }
}
public Cursor CursorValue
{
get { return this.cursorValue; }
}
public bool Exist(String cursorName)
{
return cursorName.Equals(this.CursorName);
}
}
public class MyCursors
{
private static int EnumLenth = 20;
private static readonly MyCursor[] myCursors = new MyCursor[EnumLenth];
public static int IndexOfMyCursor(String cursorName)
{
for (int i = 0; i < EnumLenth; i++)
{
if (myCursors[i] != null)
if (myCursors[i].Exist(cursorName))
return i;
}
return -1;
}
public static bool AddMyCursor(String cursorName, Cursor cursorValue)
{
if (IndexOfMyCursor(cursorName) == -1)
{
int i = 0;
while (myCursors[i] != null && i < EnumLenth)
i++;
if (i >= EnumLenth)
return false;
myCursors[i] = new MyCursor(cursorName, cursorValue);
return true;
}
else
return false;
}
public static String GetCursorName(int index)
{
if (index < EnumLenth)
return myCursors[index].CursorName;
else return null;
}
public static Cursor GetCursor(int index)
{
if (index < EnumLenth)
return myCursors[index].CursorValue;
else return null;
}
public static Cursor GetCursor(String cursorName)
{
int index = -1;
if ((index = IndexOfMyCursor(cursorName)) != -1)
return myCursors[index].CursorValue;
else return null;
}
}
}
----------------------------------DynamicCodeFile.txt--------------------------------------
using System;
using System.Windows;
namespace CursorLib
{
public enum MyCursorType:int
{
Default,
Hand,
Max
}
public class DynamicCodes : DependencyObject
{
public String GetCursorTypeName()
{
return GetValue(this.ButtonCursorProperty).ToString();
}
public DependencyProperty GetProperty()
{
return ButtonCursorProperty=DependencyProperty.Register("ButtonCursor", typeof(MyCursorType) , typeof(MyGrid),
new FrameworkPropertyMetadata(MyCursorType.Default, new PropertyChangedCallback(MyGrid.OnButtonCursorChanged)));
}
public void ResetProperty()
{
MyCursorType temp=(MyCursorType)GetValue(this.ButtonCursorProperty);
SetValue(this.ButtonCursorProperty,MyCursorType.Default);
SetValue(this.ButtonCursorProperty,temp);
}
public object GetCursorType(String name)
{
object obj=null;
for(int i=0;i<(int)MyCursorType.Max;i++)
{
if(name.Equals(Enum.GetName(typeof(MyCursorType), i)))
{
obj = Enum.ToObject(typeof(MyCursorType),i);
break;
}
}
return obj;
}
private DependencyProperty ButtonCursorProperty=null;
}
}