添加项目文件。
This commit is contained in:
@@ -0,0 +1,327 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Windows.Forms;
|
||||
using CefSharp;
|
||||
using CefSharp.WinForms;
|
||||
using KGdev.BI3D.Revit.Addin.MainWindow.External.Browser.Properties;
|
||||
|
||||
namespace KGdev.BI3D.Revit.Addin.MainWindow.External.Browser
|
||||
{
|
||||
internal partial class BrowserForm : Form
|
||||
{
|
||||
public BrowserForm(
|
||||
BrowserConfiguration configuration,
|
||||
BrowserFunctions functions,
|
||||
Queue<string> browserToRevitActions,
|
||||
Queue<string> revitToBrowserActions
|
||||
)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.functions = functions;
|
||||
this.browserToRevitActions = browserToRevitActions;
|
||||
this.revitToBrowserActions = revitToBrowserActions;
|
||||
//AssemblyLoaderUtil loaderUtil = new AssemblyLoaderUtil(
|
||||
// Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
|
||||
// );
|
||||
//loaderUtil.HookAssemblyResolve();
|
||||
//try
|
||||
//{
|
||||
|
||||
//}
|
||||
//catch (Exception ex)
|
||||
//{
|
||||
// MessageBox.Show(ex.Message);
|
||||
//}
|
||||
//finally
|
||||
//{
|
||||
// loaderUtil.UnhookAssemblyResolve();
|
||||
//}
|
||||
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
//Assembly.LoadFrom(dir + "//CefSharp.Core.dll");
|
||||
//Assembly.LoadFile(
|
||||
// @"D:\Users\Zhanggg\Desktop\KGdev.BI3D.Revit\KGdev.BI3D.Revit.Addin.MainWindow.External.Browser\bin\Debug\net472\CefSharp.dll"
|
||||
//);
|
||||
//Assembly.LoadFile(
|
||||
// @"D:\Users\Zhanggg\Desktop\KGdev.BI3D.Revit\KGdev.BI3D.Revit.Addin.MainWindow.External.Browser\bin\Debug\net472\CefSharp.Core.dll"
|
||||
//);
|
||||
//Assembly.LoadFile(
|
||||
// @"D:\Users\Zhanggg\Desktop\KGdev.BI3D.Revit\KGdev.BI3D.Revit.Addin.MainWindow.External.Browser\bin\Debug\net472\CefSharp.WinForms.dll"
|
||||
//);
|
||||
base.Load += (object sender, EventArgs args) =>
|
||||
{
|
||||
this.CreateBrowser();
|
||||
|
||||
Size windowSize = Settings.Default.WindowSize;
|
||||
bool flag2 = true;
|
||||
if (flag2)
|
||||
{
|
||||
base.Size = Settings.Default.WindowSize;
|
||||
}
|
||||
base.FormClosing += this.Form_Browser_FormClosing;
|
||||
base.FormClosed += this.Form_Browser_FormClosed;
|
||||
};
|
||||
this.InitializeComponent();
|
||||
this.Text = configuration.Title;
|
||||
bool flag = configuration.ParentProcessId > 0;
|
||||
if (flag)
|
||||
{
|
||||
try
|
||||
{
|
||||
Process processById = Process.GetProcessById(configuration.ParentProcessId);
|
||||
IntPtr mainWindowHandle = processById.MainWindowHandle;
|
||||
BrowserForm.NativeMethod.SetWindowLongPtr64(base.Handle, -8, mainWindowHandle);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
this.SendMessage("handle:" + base.Handle.ToString());
|
||||
this.actionTimer.Tick += this.Action_timer_Tick;
|
||||
this.actionTimer.Start();
|
||||
}
|
||||
|
||||
private void Action_timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
bool flag = this.browserToRevitActions.Count > 0;
|
||||
if (flag)
|
||||
{
|
||||
string text = this.browserToRevitActions.Dequeue();
|
||||
this.SendMessage(text);
|
||||
}
|
||||
bool flag2 = this.revitToBrowserActions.Count > 0;
|
||||
if (flag2)
|
||||
{
|
||||
string text2 = this.revitToBrowserActions.Dequeue();
|
||||
this.browser.ExecuteScriptAsync(
|
||||
string.Concat(
|
||||
new string[]
|
||||
{
|
||||
this.configuration.Net2JsObjectName,
|
||||
".",
|
||||
this.configuration.Net2JsUpdateStatePartialFunctionName,
|
||||
"(",
|
||||
text2,
|
||||
")"
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
bool flag3 = this.configuration.ParentProcessId > 0;
|
||||
if (flag3)
|
||||
{
|
||||
try
|
||||
{
|
||||
Process processById = Process.GetProcessById(
|
||||
this.configuration.ParentProcessId
|
||||
);
|
||||
}
|
||||
catch
|
||||
{
|
||||
base.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMessage(string message)
|
||||
{
|
||||
BrowserForm.MyStruct myStruct;
|
||||
myStruct.Message = message;
|
||||
int num = Marshal.SizeOf<BrowserForm.MyStruct>(myStruct);
|
||||
IntPtr intPtr = Marshal.AllocHGlobal(num);
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr<BrowserForm.MyStruct>(myStruct, intPtr, true);
|
||||
BrowserForm.COPYDATASTRUCT copydatastruct = new BrowserForm.COPYDATASTRUCT
|
||||
{
|
||||
cbData = num,
|
||||
lpData = intPtr
|
||||
};
|
||||
BrowserForm
|
||||
.NativeMethod
|
||||
.SendMessage(
|
||||
this.configuration.MessageHandle,
|
||||
74,
|
||||
base.Handle,
|
||||
ref copydatastruct
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(intPtr);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void WndProc(ref Message m)
|
||||
{
|
||||
bool flag = m.Msg == 74;
|
||||
if (flag)
|
||||
{
|
||||
BrowserForm.COPYDATASTRUCT copydatastruct = (BrowserForm.COPYDATASTRUCT)
|
||||
m.GetLParam(typeof(BrowserForm.COPYDATASTRUCT));
|
||||
bool flag2 = copydatastruct.cbData == Marshal.SizeOf(typeof(BrowserForm.MyStruct));
|
||||
if (flag2)
|
||||
{
|
||||
BrowserForm.MyStruct myStruct = (BrowserForm.MyStruct)
|
||||
Marshal.PtrToStructure(copydatastruct.lpData, typeof(BrowserForm.MyStruct));
|
||||
this.revitToBrowserActions.Enqueue(myStruct.Message);
|
||||
base.Visible = true;
|
||||
base.Activate();
|
||||
}
|
||||
}
|
||||
base.WndProc(ref m);
|
||||
}
|
||||
|
||||
private bool CreateBrowser()
|
||||
{
|
||||
CefSettings cefSettings = new CefSettings();
|
||||
cefSettings.PackLoadingDisabled = false;
|
||||
cefSettings.LogSeverity = LogSeverity.Disable;
|
||||
cefSettings.BrowserSubprocessPath =
|
||||
this.assemblyDirectory + "\\CefSharp.BrowserSubprocess.exe";
|
||||
cefSettings.CachePath = this.configuration.CachePath;
|
||||
cefSettings.IgnoreCertificateErrors = false;
|
||||
cefSettings.PersistSessionCookies = true;
|
||||
//Cef.EnableHighDPISupport();
|
||||
//CefSharpSettings.LegacyJavascriptBindingEnabled = true;
|
||||
CefSharpSettings.WcfTimeout = TimeSpan.Zero;
|
||||
bool flag = !Cef.IsInitialized;
|
||||
if (flag)
|
||||
{
|
||||
bool flag2 = !Cef.Initialize(cefSettings);
|
||||
if (flag2)
|
||||
{
|
||||
MessageBox.Show("Failed to initialize the document browser component");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
string text = (
|
||||
this.configuration.Url.StartsWith("http")
|
||||
? this.configuration.Url
|
||||
: Path.Combine(this.assemblyDirectory, this.configuration.Url)
|
||||
);
|
||||
this.browser = new ChromiumWebBrowser(text, null);
|
||||
//this.browser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
|
||||
//this.browser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
|
||||
this.browser
|
||||
.JavascriptObjectRepository
|
||||
.Register(this.configuration.Js2NetObjectName, this.functions, true, null);
|
||||
this.browser.BrowserSettings.Javascript = CefState.Enabled;
|
||||
this.browser.BrowserSettings.JavascriptCloseWindows = CefState.Enabled;
|
||||
this.browser.BrowserSettings.WebGl = CefState.Enabled;
|
||||
//this.browser.BrowserSettings.ApplicationCache = CefState.Disabled;
|
||||
this.browser.BrowserSettings.LocalStorage = CefState.Disabled;
|
||||
this.browser.BrowserSettings.Databases = CefState.Disabled;
|
||||
this.browser.LifeSpanHandler = new LifeSpanHandler();
|
||||
this.browser.MenuHandler = new ContextMenuHandler();
|
||||
this.browser.LoadingStateChanged += delegate(
|
||||
object sender,
|
||||
LoadingStateChangedEventArgs args
|
||||
)
|
||||
{
|
||||
bool flag3 = !args.IsLoading;
|
||||
if (flag3)
|
||||
{
|
||||
base.Invoke(
|
||||
new MethodInvoker(
|
||||
delegate
|
||||
{
|
||||
this.loader.Visible = false;
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
this.panelBrowser.Controls.Add(this.browser);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Form_Browser_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
bool flag = base.WindowState == FormWindowState.Normal;
|
||||
if (flag)
|
||||
{
|
||||
Settings.Default.WindowSize = base.Size;
|
||||
}
|
||||
else
|
||||
{
|
||||
Settings.Default.WindowSize = base.RestoreBounds.Size;
|
||||
}
|
||||
Settings.Default.Save();
|
||||
}
|
||||
|
||||
private void Form_Browser_FormClosed(object sender, FormClosedEventArgs e)
|
||||
{
|
||||
bool isInitialized = Cef.IsInitialized;
|
||||
if (isInitialized)
|
||||
{
|
||||
Cef.Shutdown();
|
||||
}
|
||||
this.SendMessage("close:");
|
||||
}
|
||||
|
||||
private const int WM_COPYDATA = 74;
|
||||
|
||||
private const int WM_CLOSE = 16;
|
||||
|
||||
private const int MESSAGE_MAX_SIZE = 16777216;
|
||||
|
||||
private string assemblyDirectory = Path.GetDirectoryName(
|
||||
new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath
|
||||
);
|
||||
|
||||
private Queue<string> browserToRevitActions;
|
||||
|
||||
private Queue<string> revitToBrowserActions;
|
||||
|
||||
private ChromiumWebBrowser browser = null;
|
||||
|
||||
private Timer actionTimer = new Timer { Interval = 300 };
|
||||
|
||||
private readonly BrowserConfiguration configuration;
|
||||
|
||||
private readonly BrowserFunctions functions;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
internal struct MyStruct
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16777216)]
|
||||
public string Message;
|
||||
}
|
||||
|
||||
internal struct COPYDATASTRUCT
|
||||
{
|
||||
public IntPtr dwData;
|
||||
|
||||
public int cbData;
|
||||
|
||||
public IntPtr lpData;
|
||||
}
|
||||
|
||||
[SuppressUnmanagedCodeSecurity]
|
||||
internal class NativeMethod
|
||||
{
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr SendMessage(
|
||||
IntPtr hWnd,
|
||||
int Msg,
|
||||
IntPtr wParam,
|
||||
ref BrowserForm.COPYDATASTRUCT lParam
|
||||
);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
||||
|
||||
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
|
||||
public static extern IntPtr SetWindowLongPtr64(
|
||||
IntPtr hWnd,
|
||||
int nIndex,
|
||||
IntPtr dwNewLong
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user