frmIEBrowser.cs.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using Microsoft.Win32;
  3. using System.Windows.Forms;
  4. using Excel;
  5. namespace Core.Mes.Client.Common
  6. {
  7. public partial class frmIEBrowser : Form
  8. {
  9. struct regkey
  10. {
  11. public RegistryKey rootKey;
  12. public string sKey;
  13. public string sItem;
  14. public int val;
  15. public bool isExist;
  16. }
  17. regkey[] aryKeys = new regkey[10];
  18. public frmIEBrowser()
  19. {
  20. InitializeComponent();
  21. }
  22. void RegSet()
  23. {
  24. try
  25. {
  26. //RegistryPermission f = new RegistryPermission(RegistryPermissionAccess.Read | RegistryPermissionAccess.Write,
  27. // "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes");
  28. //f.AddPathList(RegistryPermissionAccess.Read | RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT");
  29. RegistryKey rKey = Registry.LocalMachine;
  30. aryKeys[0].rootKey = rKey;
  31. aryKeys[0].sKey = "SOFTWARE\\Classes\\Excel.Sheet.12";
  32. aryKeys[0].isExist = false;
  33. aryKeys[0].sItem = "BrowserFlags";
  34. RegistryKey subKey = rKey.OpenSubKey(aryKeys[0].sKey, true);
  35. if (subKey != null)
  36. {
  37. if (subKey.GetValue(aryKeys[0].sItem, null) != null)
  38. {
  39. aryKeys[0].val = (int)subKey.GetValue(aryKeys[0].sItem);
  40. aryKeys[0].isExist = true;
  41. subKey.SetValue(aryKeys[0].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  42. subKey.Close();
  43. aryKeys[1].rootKey = rKey;
  44. aryKeys[1].sKey = "SOFTWARE\\Classes\\Excel.Sheet.8";
  45. aryKeys[1].isExist = false;
  46. aryKeys[1].sItem = "BrowserFlags";
  47. subKey = rKey.OpenSubKey(aryKeys[1].sKey, true);
  48. if (subKey != null)
  49. {
  50. aryKeys[1].val = (int)subKey.GetValue(aryKeys[1].sItem);
  51. aryKeys[1].isExist = true;
  52. subKey.SetValue(aryKeys[1].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  53. subKey.Close();
  54. }
  55. aryKeys[2].rootKey = rKey;
  56. aryKeys[2].sKey = "SOFTWARE\\Classes\\Excel.SheetMacroEnabled.12";
  57. aryKeys[2].isExist = false;
  58. aryKeys[2].sItem = "BrowserFlags";
  59. subKey = rKey.OpenSubKey(aryKeys[2].sKey, true);
  60. if (subKey != null)
  61. {
  62. aryKeys[2].val = (int)subKey.GetValue(aryKeys[2].sItem);
  63. aryKeys[2].isExist = true;
  64. subKey.SetValue(aryKeys[2].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  65. subKey.Close();
  66. }
  67. aryKeys[3].rootKey = rKey;
  68. aryKeys[3].sKey = "SOFTWARE\\Classes\\Excel.SheetBinaryMacroEnabled.12";
  69. aryKeys[3].isExist = false;
  70. aryKeys[3].sItem = "BrowserFlags";
  71. subKey = rKey.OpenSubKey(aryKeys[3].sKey, true);
  72. if (subKey != null)
  73. {
  74. aryKeys[3].val = (int)subKey.GetValue(aryKeys[3].sItem);
  75. aryKeys[3].isExist = true;
  76. subKey.SetValue(aryKeys[3].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  77. subKey.Close();
  78. }
  79. }
  80. }
  81. rKey.Close();
  82. rKey = Registry.ClassesRoot;
  83. aryKeys[4].rootKey = rKey;
  84. aryKeys[4].sKey = "Excel.Sheet.8";
  85. aryKeys[4].isExist = false;
  86. aryKeys[4].sItem = "EditFlags";
  87. subKey = rKey.OpenSubKey(aryKeys[4].sKey, true);
  88. if (subKey != null)
  89. {
  90. if (!(subKey.GetValue(aryKeys[4].sItem) is Array))
  91. aryKeys[4].val = (int)subKey.GetValue(aryKeys[4].sItem);
  92. else
  93. aryKeys[4].val = BitConverter.ToInt32((byte[])subKey.GetValue(aryKeys[4].sItem), 0);
  94. aryKeys[4].isExist = true;
  95. subKey.SetValue(aryKeys[4].sItem, 0x0010000, RegistryValueKind.DWord);
  96. subKey.Close();
  97. }
  98. rKey.Close();
  99. }
  100. catch (Exception ee)
  101. {
  102. MessageBox.Show(ee.Message);
  103. }
  104. }
  105. void RegBack()
  106. {
  107. try
  108. {
  109. RegistryKey rKey, subKey;
  110. //if (aryKeys[0].isExist)
  111. //{
  112. // rKey = Registry.LocalMachine;
  113. // subKey = rKey.OpenSubKey(aryKeys[0].sKey, true);
  114. //}
  115. for (int i = 0; i < aryKeys.Length; i++)
  116. {
  117. if (aryKeys[i].isExist)
  118. {
  119. rKey = aryKeys[i].rootKey;
  120. subKey = rKey.OpenSubKey(aryKeys[i].sKey, true);
  121. subKey.SetValue(aryKeys[i].sItem, aryKeys[i].val, RegistryValueKind.DWord);
  122. subKey.Close();
  123. rKey.Close();
  124. }
  125. }
  126. }
  127. catch { }
  128. }
  129. public string sUrl = "";
  130. void Go()
  131. {
  132. Object obj = new object();
  133. axWebBrowser1.Navigate(sUrl, ref obj, ref obj, ref obj, ref obj);
  134. }
  135. private void frmIEBrowser_Load(object sender, EventArgs e)
  136. {
  137. RegSet();
  138. Go();
  139. }
  140. private void frmIEBrowser_FormClosing(object sender, FormClosingEventArgs e)
  141. {
  142. RegBack();
  143. try
  144. {
  145. object sRef = new object();
  146. axWebBrowser1.Navigate("about:blank", ref sRef, ref sRef, ref sRef, ref sRef);
  147. axWebBrowser1.Dispose();
  148. }
  149. catch { }
  150. }
  151. public Excel.Application xlApp;
  152. public Excel.Workbook xlWb;
  153. private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
  154. {
  155. if (axWebBrowser1.Document is Excel.Workbook)
  156. {
  157. xlWb = (axWebBrowser1.Document as Excel.Workbook);
  158. xlApp = xlWb.Application;
  159. xlApp.DisplayFormulaBar = false;
  160. xlApp.DisplayAlerts = false;
  161. }
  162. }
  163. }
  164. }