Skip to content

Commit 5cab3c2

Browse files
committed
WinForms Example - Make DPI Aware
- WinForms Example is now DPI aware using https://docs.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms?view=netframeworkdesktop-4.8 There's a mistake in the MS Docs which was already reported at dotnet/docs-desktop#1485 - Toolstrip layout updated slightly, could use some more minor tweaks to better calculate size Resolves #1543
1 parent fa6e731 commit 5cab3c2

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

CefSharp.WinForms.Example/AboutBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public AboutBox()
7777
InitializeComponent();
7878
ExecutingAssembly = Assembly.GetExecutingAssembly();
7979

80-
Text = "About CefTest";
80+
Text = "About CefSharp.WinForms.Example";
8181
labelProductName.Text = AssemblyProduct;
8282
labelVersion.Text = String.Format("Version {0} ", Cef.CefSharpVersion);
8383
labelCopyright.Text = AssemblyCopyright;

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private void HandleToolStripLayout()
444444
width -= item.Width - item.Margin.Horizontal;
445445
}
446446
}
447-
urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
447+
urlTextBox.Width = Math.Max(100, width - urlTextBox.Margin.Horizontal - goButton.Width);
448448
}
449449

450450
private void GoButtonClick(object sender, EventArgs e)

CefSharp.WinForms.Example/Minimal/SimpleBrowserForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void HandleToolStripLayout()
138138
width -= item.Width - item.Margin.Horizontal;
139139
}
140140
}
141-
urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
141+
urlTextBox.Width = Math.Max(100, width - urlTextBox.Margin.Horizontal - goButton.Width);
142142
}
143143

144144
private void ExitMenuItemClick(object sender, EventArgs e)

CefSharp.WinForms.Example/Program.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ public class Program
1717
[STAThread]
1818
public static int Main(string[] args)
1919
{
20+
// DEMO: Change to true to self host the BrowserSubprocess.
21+
// instead of using CefSharp.BrowserSubprocess.exe, your applications exe will be used.
22+
// In this case CefSharp.WinForms.Example.exe
2023
const bool selfHostSubProcess = false;
2124

22-
Cef.EnableHighDPISupport();
23-
24-
//NOTE: Using a simple sub processes uses your existing application executable to spawn instances of the sub process.
25-
//Features like JSB, EvaluateScriptAsync, custom schemes require the CefSharp.BrowserSubprocess to function
2625
if (selfHostSubProcess)
2726
{
27+
var processType = CefSharp.Internals.CommandLineArgsParser.GetArgumentValue(args, CefSharp.Internals.CefSharpArguments.SubProcessTypeArgument);
28+
29+
if (processType == "gpu-process")
30+
{
31+
// Enable DPI Awareness for GPU process.
32+
// Our main application is already DPI aware using WinForms specific features
33+
// **IMPORTANT** There's a mistake in the following doc https://github.com/dotnet/docs-desktop/issues/1485
34+
// https://docs.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms
35+
Cef.EnableHighDPISupport();
36+
}
37+
2838
var exitCode = CefSharp.BrowserSubprocess.SelfHost.Main(args);
2939

3040
if (exitCode >= 0)
@@ -45,6 +55,7 @@ public static int Main(string[] args)
4555

4656
Cef.Initialize(settings);
4757

58+
Application.EnableVisualStyles();
4859
var browser = new SimpleBrowserForm();
4960
Application.Run(browser);
5061
}
@@ -58,11 +69,14 @@ public static int Main(string[] args)
5869
}
5970
#endif
6071

61-
//When multiThreadedMessageLoop = true then externalMessagePump must be set to false
62-
// To enable externalMessagePump set multiThreadedMessageLoop = false and externalMessagePump = true
72+
// DEMO: To integrate CEF into your applications existing message loop
73+
// set multiThreadedMessageLoop = false;
6374
const bool multiThreadedMessageLoop = true;
75+
// When multiThreadedMessageLoop = true then externalMessagePump must be set to false
76+
// To enable externalMessagePump set multiThreadedMessageLoop = false and externalMessagePump = true
6477
const bool externalMessagePump = false;
6578

79+
//TEST: There are a number of different Forms for testing purposes.
6680
var browser = new BrowserForm(multiThreadedMessageLoop);
6781
//var browser = new SimpleBrowserForm(multiThreadedMessageLoop);
6882
//var browser = new TabulationDemoForm();
@@ -95,6 +109,7 @@ public static int Main(string[] args)
95109

96110
CefExample.Init(settings, browserProcessHandler: browserProcessHandler);
97111

112+
Application.EnableVisualStyles();
98113
//Application.Run(new MultiFormAppContext());
99114
Application.Run(browser);
100115
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<?xml version="1.0"?>
22
<configuration>
3-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
5+
</startup>
6+
7+
<System.Windows.Forms.ApplicationConfigurationSection>
8+
<add key="DpiAwareness" value="PerMonitorV2" />
9+
</System.Windows.Forms.ApplicationConfigurationSection>
10+
</configuration>

CefSharp.WinForms.Example/app.manifest

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@
3434
</security>
3535
</trustInfo>
3636

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-
4337
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
4438
<application>
4539
<!-- A list of all Windows versions that this application is designed to work with.

0 commit comments

Comments
 (0)