Skip to content

Compare: General Usage

New page
Showing with 70 additions and 16 deletions.
  1. +68 −16 General-Usage.md
  2. +1 −0 Questions-&-Support.md
  3. +1 −0 _Footer.md
84 changes: 68 additions & 16 deletions General-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,35 +681,87 @@ If you need better printing support then you should discuss that on `ceforum`. T

## High DPI Displays/Support

To enable _High DPI_ support you'll need to add the relevant entries to your `app.manifest` to let Windows know your app is _High DPI Aware_. The relevant MSDN article is https://msdn.microsoft.com/en-us/library/windows/desktop/dn469266(v=vs.85).aspx it's also recommended that you read https://docs.microsoft.com/en-us/windows/desktop/sbscs/application-manifests for details on how to set the entries in the `app.manifest`
Desktop applications using `WinForms/WPF` need to be made [DPI Aware](https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows) to run correctly on a _High DPI Display_ (A display with a `DPI Scale` set greater than `100%`).

It's a very long MSDN article, but it's necessary reading if your app needs to be run on high DPI displays.
**Note** If you mouse cursor is incorrectly positioned in the browser or the browser displays black boxes/border with rendering/resizing then your app needs to be made `DPI Aware`. Other parts of your application may also appear blurry or incorrectly-sized.

**Note** If you experience black boxes with rendering/resizing then your app likely needs changes to support `High DPI`
There are two main methods to specify the default DPI awareness of a process:

Prevent browser from scaling on High DPI use `settings.CefCommandLineArgs.Add("force-device-scale-factor", "1");` command line flag.
1. [Through an application manifest setting](https://docs.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-with-the-application-manifest) (This is generally the preferred option)
2. [Programmatically through an API call](https://docs.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-programmatically)

### WinForms
### WinForms High DPI

Add the relevant `app.manifest` entries and call `Cef.EnableHighDPISupport()`. It's best make that the first call in your application entry point.
#### App.manifest
Set the default awareness using the [application manifest](https://docs.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-with-the-application-manifest). The following example is PerMonitor DPI Aware on Win 10 1703 and above and PerMonitor DPI aware on older version. Make sure you read https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows#dpi-awareness-mode which discusses the different `DPI Awareness` options

The commit where support was added to the `CefSharp.MinimalExample.WinForms` project is https://github.com/cefsharp/CefSharp.MinimalExample/commit/1197f37035549ad98c7682399c12e40be677426a. Check it out for a working example.
```
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
<dpiAware>true/PM</dpiAware>
</windowsSettings>
</application>
</assembly>
```

### WPF
#### Programatically
Setting High DPI in code you can use the [Cef.EnableHighDPISupport();](http://cefsharp.github.io/api/79.1.x/html/M_CefSharp_Cef_EnableHighDPISupport.htm) helper method. This calls the `Chromium` [base::win::EnableHighDPISupport();](https://cs.chromium.org/search/?q=EnableHighDPISupport()&sq=package:chromium&type=cs) function. You then have exactly the same settings as `Chromium` uses.

[Cef.EnableHighDPISupport();](http://cefsharp.github.io/api/79.1.x/html/M_CefSharp_Cef_EnableHighDPISupport.htm) **must** be called very early on in your application execution, preferably in your application entry point (Program.Main).

The [CefSharp.MinimalExample.WinForms](https://github.com/cefsharp/CefSharp.MinimalExample/tree/master/CefSharp.MinimalExample.WinForms) project contains a working example.

### WPF High DPI

#### App.manifest

Add the relevant `app.manifest` entries. It should roughly look like the following (You need to add some xmlns entries to the top level root element, see https://github.com/cefsharp/CefSharp/blob/cefsharp/79/CefSharp.Wpf.Example/app.manifest for a working example)

```
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
<dpiAware>true/PM</dpiAware>
</windowsSettings>
</application>
</assembly>
```

#### Programatically

Add the relevant `app.manifest` entries. It should roughly look like the following (You need to add some xmlns entries to the top level root element, see https://github.com/cefsharp/CefSharp/blob/cefsharp/63/CefSharp.Wpf.Example/app.manifest for a working example)
`WPF` applications by default have an automatically generated `Program.Main` entry point which makes it harder to programatically set the `DPI`. See https://stackoverflow.com/a/26890426/4583726 for how to create a `Program.Main` then you can call [Cef.EnableHighDPISupport();](http://cefsharp.github.io/api/79.1.x/html/M_CefSharp_Cef_EnableHighDPISupport.htm). This **must** be called very early on in your application execution, preferably the first call in your custom Program.Main.

### OffScreen High DPI

Add the relevant `app.manifest` entries or call `Cef.EnableHighDPISupport()` (see above for an example). Read the `WinForms` section above, choose which option suites your needs.

### High DPI Additional Info

`Chromium` by default performs all rendering in separate sub-process. Specifically the `GPU Compositor` needs to have a `DPI Awareness` that matches your main application. Currently the default used by the `CefSharp.BrowserSubprocess.exe` is `Per Monitor DPI Aware`. As a workaround use the `disable-gpu-compositing` command line arg and the `DPI Awareness` of your main application process will be used instead of the `DPI Awareness` specified by the `GPU Process` (which is used for `GPU Compositing`). Disabling `GPU Compositing` may have an impact on performance, when https://github.com/cefsharp/CefSharp/issues/2927 is complete it will be possible to programatically set the `DPI Awareness` used by the `CefSharp.BrowserSubprocess.exe`

```c#
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true/PM</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
var settings = new CefSettings();
settings.CefCommandLineArgs.Add("disable-gpu-compositing");
Cef.Initialize(settings);
```

### OffScreen
Alternatively you can try the [force-device-scale-factor](https://peter.sh/experiments/chromium-command-line-switches/#force-device-scale-factor) command line flag.

Add the relevant `app.manifest` entries (see above for an example)
```c#
var settings = new CefSettings();
settings.CefCommandLineArgs.Add("force-device-scale-factor", "1");
Cef.Initialize(settings);
```

- https://msdn.microsoft.com/en-us/library/windows/desktop/dn469266(v=vs.85).aspx
It's a very long MSDN article, but it's necessary reading if your app needs to be run on high DPI displays.
- https://blogs.windows.com/windowsdeveloper/2017/05/19/improving-high-dpi-experience-gdi-based-desktop-apps/
- https://docs.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-with-the-application-manifest

## MultiThreadedMessageLoop

Expand Down