WWebViewWin32.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using UnityEngine;
  5. using Win32;
  6. namespace ICODES.STUDIO.WWebView
  7. {
  8. public class WWebViewWin32
  9. {
  10. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  11. public static extern int Initialize(IntPtr mainInstance, IntPtr prevInstance, string commandLine, int commandShow, IntPtr windowTemplate, int windowWidth, int windowHeight, int version);
  12. [DllImport("Win32-WebView")]
  13. public static extern bool SetResizeMode(uint mode);
  14. [DllImport("Win32-WebView")]
  15. public static extern void Release();
  16. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  17. public static extern bool Create(string name, WWebViewWin32.ActionDocumentComplete documentComplete, WWebViewWin32.ActionBeforeNavigate beforeNavigate, WWebViewWin32.ActionWindowClosing windowClosing, WWebViewWin32.ActionTitleChange titleChange, WWebViewWin32.ActionNewWindow newWindow, WWebViewWin32.ActionNavigateComplete navigateComplete, int left, int top, int right, int bottom, int width, int height, bool popup);
  18. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  19. public static extern void Navigate(string name, string url);
  20. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  21. public static extern void NavigateToString(string name, string text);
  22. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  23. public static extern IntPtr AddJavaScript(string name, string script);
  24. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  25. public static extern IntPtr EvaluateJavaScript(string name, string script);
  26. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  27. public static extern void Destroy(string name);
  28. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  29. public static extern void EnableContextMenu(string name, bool enable);
  30. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  31. public static extern void CleanCache();
  32. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  33. public static extern void CleanCookie(string name);
  34. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  35. public static extern IntPtr GetCookie(string url, string key);
  36. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  37. public static extern void SetCookie(string url, string cookie);
  38. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  39. public static extern void Refresh(string name);
  40. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  41. public static extern void GoBack(string name);
  42. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  43. public static extern void GoForward(string name);
  44. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  45. public static extern void Stop(string name);
  46. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  47. public static extern void Show(string name, bool show);
  48. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  49. public static extern void ChangeLayout(string name, int left, int top, int right, int bottom, int width, int height);
  50. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  51. public static extern IntPtr CurrentUrl(string name);
  52. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  53. public static extern void AddUrlScheme(string name, string scheme);
  54. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  55. public static extern void RemoveUrlScheme(string name, string scheme);
  56. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  57. public static extern void SetZoom(string name, int factor);
  58. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  59. public static extern void SetUserAgent(string name, string userAgent);
  60. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  61. public static extern IntPtr GetUserAgent(string name);
  62. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  63. public static extern void SetTexture(string name, IntPtr texture, int width, int height);
  64. [DllImport("Win32-WebView")]
  65. public static extern IntPtr GetRenderEventFunc();
  66. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  67. public static extern void Transparent(string name, bool transparent);
  68. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  69. public static extern float GetAlpha(string name);
  70. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  71. public static extern void SetAlpha(string name, float alpha);
  72. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  73. public static extern int GetActualWidth(string name);
  74. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  75. public static extern int GetActualHeight(string name);
  76. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  77. public static extern void ShowScroll(string name, bool show);
  78. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  79. public static extern void ShowScrollX(string name, bool show);
  80. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  81. public static extern void ShowScrollY(string name, bool show);
  82. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  83. public static extern void SetTitleText(string name, string text);
  84. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  85. public static extern bool CanGoBack(string name);
  86. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  87. public static extern bool CanGoForward(string name);
  88. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  89. public static extern void SetSilent(string name, bool mode);
  90. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  91. public static extern void InputEvent(string name, int state, int key, int x, int y);
  92. [DllImport("Win32-WebView", CharSet = CharSet.Auto)]
  93. public static extern void SetHeaderField(string name, string key, string value);
  94. [DllImport("Win32-WebView")]
  95. public static extern void DispatchAccelerator();
  96. public static void DispatchMessage()
  97. {
  98. WWebViewWin32.DispatchAccelerator();
  99. }
  100. public static bool ModifyStyle(IntPtr window, long remove, long add, uint flags)
  101. {
  102. IntPtr windowLong = User32.GetWindowLong(window, -16);
  103. IntPtr intPtr = new IntPtr(((long)windowLong.ToInt32() & ~remove) | add);
  104. if (windowLong == intPtr)
  105. {
  106. return false;
  107. }
  108. User32.SetWindowLong(window, -16, intPtr);
  109. if (flags != 0U)
  110. {
  111. User32.SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, 23U | flags);
  112. }
  113. return true;
  114. }
  115. public static IntPtr FindUnityPlayerWindow()
  116. {
  117. IntPtr intPtr = WWebViewWin32.FindUnityWindow("UnityWndClass", null);
  118. if (intPtr != IntPtr.Zero)
  119. {
  120. return intPtr;
  121. }
  122. return User32.FindWindow("UnityWndClass", Application.productName);
  123. }
  124. public static IntPtr FindUnityEditorWindow()
  125. {
  126. return WWebViewWin32.FindUnityWindow("UnityContainerWndClass", "Unity");
  127. }
  128. public static IntPtr FindUnityWindow(string targetClassName, string prefixWindowText = null)
  129. {
  130. uint currentProcessId = Kernel32.GetCurrentProcessId();
  131. IntPtr intPtr = User32.GetTopWindow(IntPtr.Zero);
  132. while (intPtr != IntPtr.Zero)
  133. {
  134. uint num;
  135. User32.GetWindowThreadProcessId(intPtr, out num);
  136. if (num == currentProcessId && User32.IsWindowVisible(intPtr) != 0)
  137. {
  138. StringBuilder stringBuilder = new StringBuilder(1024);
  139. User32.GetClassName(intPtr, stringBuilder, stringBuilder.Capacity);
  140. if (stringBuilder.ToString() == targetClassName)
  141. {
  142. StringBuilder stringBuilder2 = new StringBuilder(1024);
  143. User32.GetWindowText(intPtr, stringBuilder2, stringBuilder2.Capacity);
  144. if (string.IsNullOrEmpty(prefixWindowText) || stringBuilder2.ToString().StartsWith(prefixWindowText))
  145. {
  146. return intPtr;
  147. }
  148. }
  149. }
  150. intPtr = User32.GetWindow(intPtr, 2U);
  151. }
  152. return IntPtr.Zero;
  153. }
  154. public static void SubclassWindow()
  155. {
  156. IntPtr hWnd = WWebViewWin32.FindUnityPlayerWindow();
  157. WWebViewWin32.ActionWindowProc d = new WWebViewWin32.ActionWindowProc(WWebViewWin32.WindowProc);
  158. IntPtr functionPointerForDelegate = Marshal.GetFunctionPointerForDelegate(d);
  159. WWebViewWin32.defaultWindowProc = User32.SetWindowLong(hWnd, -4, functionPointerForDelegate);
  160. }
  161. public static void UnsubclassWindow()
  162. {
  163. if (WWebViewWin32.defaultWindowProc != IntPtr.Zero)
  164. {
  165. User32.SetWindowLong(WWebViewWin32.FindUnityPlayerWindow(), -4, WWebViewWin32.defaultWindowProc);
  166. WWebViewWin32.defaultWindowProc = IntPtr.Zero;
  167. }
  168. }
  169. private static IntPtr DefWindowProc(IntPtr window, uint message, IntPtr wparam, IntPtr lparam)
  170. {
  171. if (WWebViewWin32.defaultWindowProc != IntPtr.Zero)
  172. {
  173. return User32.CallWindowProc(WWebViewWin32.defaultWindowProc, window, message, wparam, lparam);
  174. }
  175. return User32.DefWindowProcW(window, message, wparam, lparam);
  176. }
  177. private static IntPtr WindowProc(IntPtr window, uint message, IntPtr wparam, IntPtr lparam)
  178. {
  179. bool flag = false;
  180. IntPtr result = IntPtr.Zero;
  181. if (message == 16U)
  182. {
  183. flag = WWebViewWin32.OnClose(window, message, wparam, lparam, ref result);
  184. }
  185. if (!flag)
  186. {
  187. result = WWebViewWin32.DefWindowProc(window, message, wparam, lparam);
  188. }
  189. return result;
  190. }
  191. private static bool OnClose(IntPtr window, uint message, IntPtr wparam, IntPtr lparam, ref IntPtr result)
  192. {
  193. WWebViewWin32.UnsubclassWindow();
  194. Application.Quit();
  195. return true;
  196. }
  197. private static IntPtr defaultWindowProc = IntPtr.Zero;
  198. private static MSG lastMsg;
  199. public delegate void ActionDocumentComplete(IntPtr name, IntPtr url);
  200. public delegate void ActionBeforeNavigate(IntPtr name, IntPtr url, IntPtr message, ref bool cancel);
  201. public delegate void ActionWindowClosing(IntPtr name, bool childWindow, ref bool cancel);
  202. public delegate void ActionTitleChange(IntPtr name, IntPtr title);
  203. public delegate void ActionNewWindow(IntPtr name, ref bool cancel);
  204. public delegate void ActionNavigateComplete(IntPtr name, IntPtr url);
  205. public delegate IntPtr ActionWindowProc(IntPtr window, uint message, IntPtr wparam, IntPtr lparam);
  206. }
  207. }