Skip to content

Commit 988343e

Browse files
committed
Minimal working example
1 parent 603f63a commit 988343e

12 files changed

Lines changed: 1350 additions & 0 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CefSharp.OutOfProcess.BrowserProcess
2+
{
3+
/// <summary>
4+
/// Initialization settings. Many of these and other settings can also configured using command-line switches.
5+
/// </summary>
6+
public class CefSettings : CefSettingsBase
7+
{
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<PlatformTarget>x64</PlatformTarget>
7+
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
8+
<SelfContained>false</SelfContained>
9+
<ApplicationManifest>app.manifest</ApplicationManifest>
10+
<CefSharpBuildAction>NoAction</CefSharpBuildAction>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="CefSharp.Common.NETCore" Version="102.0.90" />
15+
</ItemGroup>
16+
17+
</Project>

CefSharp.OutOfProcess.BrowserProcess/ChromiumWebBrowser.cs

Lines changed: 902 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using CefSharp.Internals;
5+
6+
namespace CefSharp.OutOfProcess.BrowserProcess
7+
{
8+
public class Program
9+
{
10+
public static int Main(string[] args)
11+
{
12+
Cef.EnableHighDPISupport();
13+
14+
//Debugger.Launch();
15+
16+
var parentProcessId = int.Parse(CommandLineArgsParser.GetArgumentValue(args, "--parentProcessId"));
17+
var hostHwnd = int.Parse(CommandLineArgsParser.GetArgumentValue(args, "--hostHwnd"));
18+
19+
var parentProcess = Process.GetProcessById(parentProcessId);
20+
21+
var settings = new CefSettings()
22+
{
23+
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
24+
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\OutOfProcessCache")
25+
};
26+
27+
Cef.EnableWaitForBrowsersToClose();
28+
29+
Cef.Initialize(settings);
30+
31+
var browser = new ChromiumWebBrowser("https://github.com");
32+
33+
var windowInfo = new WindowInfo();
34+
windowInfo.SetAsChild(new IntPtr(hostHwnd));
35+
36+
browser.CreateBrowser(windowInfo);
37+
38+
parentProcess.WaitForExit();
39+
40+
Cef.WaitForBrowsersToClose();
41+
42+
Cef.Shutdown();
43+
44+
return 0;
45+
}
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<asmv1:assembly
4+
manifestVersion="1.0"
5+
xmlns="urn:schemas-microsoft-com:asm.v1"
6+
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
7+
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
8+
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
9+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
10+
11+
<assemblyIdentity version="1.0.0.0" name="CefSharp.OutOfProcess.BrowserProcess.app" />
12+
13+
<dependency>
14+
<dependentAssembly>
15+
<assemblyIdentity type="Win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
16+
</dependentAssembly>
17+
</dependency>
18+
19+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
20+
<security>
21+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
22+
<!-- UAC Manifest Options
23+
If you want to change the Windows User Account Control level replace the
24+
requestedExecutionLevel node with one of the following.
25+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
26+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
27+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
28+
29+
Specifying requestedExecutionLevel node will disable file and registry virtualization.
30+
If you want to utilize File and Registry Virtualization for backward
31+
compatibility then delete the requestedExecutionLevel node.-->
32+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
33+
</requestedPrivileges>
34+
</security>
35+
</trustInfo>
36+
37+
<asmv3:application>
38+
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
39+
<dpiAware>true/PM</dpiAware>
40+
</asmv3:windowsSettings>
41+
</asmv3:application>
42+
43+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
44+
<application>
45+
<!-- A list of all Windows versions that this application is designed to work with.
46+
Windows will automatically select the most compatible environment.-->
47+
48+
<!-- Windows 7 -->
49+
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
50+
<!-- Windows 8 -->
51+
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
52+
<!-- Windows 8.1 -->
53+
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
54+
<!-- Windows 10 -->
55+
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
56+
</application>
57+
</compatibility>
58+
</asmv1:assembly>

CefSharp.OutOfProcess.Example.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.32510.428
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CefSharp.OutOfProcess.Example", "CefSharp.OutOfProcess.Example\CefSharp.OutOfProcess.Example.csproj", "{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefSharp.OutOfProcess.BrowserProcess", "CefSharp.OutOfProcess.BrowserProcess\CefSharp.OutOfProcess.BrowserProcess.csproj", "{8CB2936D-E12C-4CD2-9044-F264D6F0547B}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{8CB2936D-E12C-4CD2-9044-F264D6F0547B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{8CB2936D-E12C-4CD2-9044-F264D6F0547B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{8CB2936D-E12C-4CD2-9044-F264D6F0547B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{8CB2936D-E12C-4CD2-9044-F264D6F0547B}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {5E011DD5-7B05-4678-AFFD-5FDEACB52718}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<UseWindowsForms>true</UseWindowsForms>
7+
<PlatformTarget>x64</PlatformTarget>
8+
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
9+
<SelfContained>false</SelfContained>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\CefSharp.OutOfProcess.BrowserProcess\CefSharp.OutOfProcess.BrowserProcess.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Windows.Forms;
3+
4+
namespace CefSharp.OutOfProcess.Example
5+
{
6+
public class ChromiumHostControl : Control
7+
{
8+
public ChromiumHostControl()
9+
{
10+
Dock = DockStyle.Fill;
11+
}
12+
13+
protected override void OnSizeChanged(EventArgs e)
14+
{
15+
base.OnSizeChanged(e);
16+
}
17+
}
18+
}

CefSharp.OutOfProcess.Example/HostForm.Designer.cs

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Windows.Forms;
4+
5+
namespace CefSharp.OutOfProcess.Example
6+
{
7+
public partial class HostForm : Form
8+
{
9+
public HostForm()
10+
{
11+
InitializeComponent();
12+
13+
Load += HostFormOnLoad;
14+
Resize += HostFormOResize;
15+
}
16+
17+
private void HostFormOResize(object sender, EventArgs e)
18+
{
19+
var hostControl = splitContainer.Panel2.Controls[0];
20+
21+
hostControl.Size = splitContainer.Panel2.ClientSize;
22+
}
23+
24+
private void HostFormOnLoad(object sender, EventArgs e)
25+
{
26+
var host = new ChromiumHostControl();
27+
host.CreateControl();
28+
var hostHwnd = host.Handle;
29+
30+
splitContainer.Panel2.Controls.Add(host);
31+
32+
var currentProcess = Process.GetCurrentProcess();
33+
34+
var args = $"--parentProcessId={currentProcess.Id} --hostHwnd={hostHwnd.ToInt32()}";
35+
36+
var browserProcess = Process.Start("CefSharp.OutOfProcess.BrowserProcess.exe", args);
37+
}
38+
39+
private void ExitToolStripMenuItemClick(object sender, EventArgs e)
40+
{
41+
Close();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)