MainWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using Autodesk.RevitAddIns;
  20. using LRH.Controls;
  21. using RevitVersion;
  22. namespace LRH
  23. {
  24. /// <summary>
  25. /// MainWindow.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class MainWindow : WindowBase
  28. {
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. this.Loaded += MainWindow_Loaded;
  33. }/// <summary>
  34. /// 删除指定控件的指定事件
  35. /// </summary>
  36. /// <param name="control"></param>
  37. /// <param name="eventname"></param>
  38. public void ClearEvent(System.Windows.Forms.Control control, string eventname)
  39. {
  40. if (control == null) return;
  41. if (string.IsNullOrEmpty(eventname)) return;
  42. BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
  43. BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;
  44. Type controlType = typeof(System.Windows.Forms.Control);
  45. PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);
  46. EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);
  47. FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("Event" + eventname, mFieldFlags);
  48. Delegate d = eventHandlerList[fieldInfo.GetValue(control)];
  49. if (d == null) return;
  50. EventInfo eventInfo = controlType.GetEvent(eventname);
  51. foreach (Delegate dx in d.GetInvocationList())
  52. eventInfo.RemoveEventHandler(control, dx);
  53. }
  54. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  55. {
  56. Button_Click(sender,e);
  57. }
  58. private void Button_Click(object sender, RoutedEventArgs e)
  59. {
  60. var application = GetAddInApplication();
  61. AddInManager.StartRevit(StartType.Start,application);
  62. this.Close();
  63. }
  64. public RevitAddInApplication GetAddInApplication()
  65. {
  66. string fileName = System.IO.Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "LRH.RevitApplication.dll");
  67. RevitAddInApplication application = new RevitAddInApplication("LRH", fileName, Guid.NewGuid(), "LRH.RevitApplication.XHApplication", "FWindSoft");
  68. return application;
  69. }
  70. private void Button_Click_1(object sender, RoutedEventArgs e)
  71. {
  72. var application = GetAddInApplication();
  73. AddInManager.StartRevit(StartType.StartAuto, application);
  74. this.Close();
  75. }
  76. private void Button_Click_2(object sender, RoutedEventArgs e)
  77. {
  78. var application = GetAddInApplication();
  79. AddInManager.StartRevit(StartType.Attach, application);
  80. }
  81. private void Button_Click_3(object sender, RoutedEventArgs e)
  82. {
  83. var application = GetAddInApplication();
  84. AddInManager.StartRevit(StartType.Cancel, application);
  85. }
  86. }
  87. }