With WinForms I came sometimes to the limits of the graphic capabilities. For example. when some text parts need to be larger, or when an application must run on different computers with different screen resolutions and monitor sizes.
In some cases it could be a solution to use scalable WPF parts in WinForms. The core is:
A scaling object is needed:
This object can then be used to set the scaling property of the WPF element:
Please note that WPF elements have <no name> by default, so it is required to name the WPF textbox first (TextBox1). The destination element can also be a WPF panel, means that entire element compositions can be scaled at once. Do not confuse LayoutTransform and RenderTransform.
Scaling is now easy:
Result (Scale 1.0) (Scale 10.0) is:
![Name: Scale_10.png
Views: 13
Size: 3.6 KB]()
It needs a few little additional steps in [VS2017/2019] - there is a detailed step-by-step description in the Readme.txt of the attachment.
First step in a new project is adding a WPF User Control to the project, then Build Project is important! (a description will also be shown in [VS2019] when inserting a WPF Element Host to Form1 and clicking to edit Hosted Element which is <none>)
There are three projects in the attachment:
Scale1: only the minimum required
Scale2: rounded border, text, background color, textbox is surrounded by Scroll Viewer
Scale 3: autoscaling window, based on a working application in real live. An alternative (with limitations) could also be a WPF View Box around the content without needing the scaling object
Scale WPF from WinForms.zip
![Name: Scale2.PNG
Views: 12
Size: 19.7 KB]()
Note: Designing WPF needs more attention than designing WinForms. Even little mistakes can make it refuse to work. Typical mistakes are: Inserting elements to the wrong position in the XAML tree, moving around elements in the designer changed the Layout settings of the element (v/h alignment, margin), when changing properties, the wrong element was selected before.
In some cases it could be a solution to use scalable WPF parts in WinForms. The core is:
A scaling object is needed:
Code:
Public WPF_Scale As New System.Windows.Media.ScaleTransform(1.0, 1.0)
Code:
TextBox1.LayoutTransform = Form1.WPF_Scale
Scaling is now easy:
Code:
WPF_Scale.ScaleX =10.0
WPF_Scale.ScaleY =10.0
It needs a few little additional steps in [VS2017/2019] - there is a detailed step-by-step description in the Readme.txt of the attachment.
First step in a new project is adding a WPF User Control to the project, then Build Project is important! (a description will also be shown in [VS2019] when inserting a WPF Element Host to Form1 and clicking to edit Hosted Element which is <none>)
There are three projects in the attachment:
Scale1: only the minimum required
Scale2: rounded border, text, background color, textbox is surrounded by Scroll Viewer
Scale 3: autoscaling window, based on a working application in real live. An alternative (with limitations) could also be a WPF View Box around the content without needing the scaling object
Scale WPF from WinForms.zip
Note: Designing WPF needs more attention than designing WinForms. Even little mistakes can make it refuse to work. Typical mistakes are: Inserting elements to the wrong position in the XAML tree, moving around elements in the designer changed the Layout settings of the element (v/h alignment, margin), when changing properties, the wrong element was selected before.