Pergunta

I am hosting a winforms DataGridView inside a WindowsFormsHost on a WPF Window. Functionality is fine, but the text in the grid cells looks a little more fuzzy. Normally WinForms text has very little antialiasing.

How can I get the DataGridView text to look as it normally would on a Windows Form, or at least be sharper? I have tried playing with the TextOptions.TextFormattingModeand SnapsToDevicePixelssettings of the WindowsFormsHost, but don't see any difference.

As an example of what I mean here are two screenshots:

Datagridview inside a WPF WindowsFormsHost:

DataGridView in WindowsFormHost

DataGridView in Windows Forms:

DataGridView in WinForms

Foi útil?

Solução

The appearance of hosted WinForms/Win32 content should not be affected by WPF. There is an "airspace" limitation that dictates that a single window pixel can only be owned by a single graphics API, and it can only be drawn by that API. Hence, WinForms content is drawn by WinForms/GDI+, even when hosted by WPF. This explains why hosted content will be drawn on top of any WPF content in the same window, even if the WPF content is positioned in front of it.

Changing WPF rendering properties like TextFormattingMode cannot affect the appearance of interop content. You should be looking at the layout/rendering properties of the hosted WinForms content.

Update

Presumably the fuzzy effect is do to WPF defaulting to grayscale AA for the DataGridView. In the Paint and/or CellPainting events of the DataGridView, setting the graphics text hint to cleartype:

 e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit

eliminated most of the fuzziness.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top