-
Oct 27th, 2020, 03:20 PM
#1
Thread Starter
New Member
VB.Net - Scale (Zoom) WPF Elements from WinForms
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:
Code:
Public WPF_Scale As New System.Windows.Media.ScaleTransform(1.0, 1.0)
This object can then be used to set the scaling property of the WPF element:
Code:
TextBox1.LayoutTransform = Form1.WPF_Scale
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:
Code:
WPF_Scale.ScaleX =10.0
WPF_Scale.ScaleY =10.0
Result (Scale 1.0) (Scale 10.0) is:
![Name: Scale_10.png
Views: 608
Size: 3.6 KB](https://www.vbforums.com/attachment.php?s=5285fcd09204e7c1e3525be270b569f1&attachmentid=179146&d=1603828364)
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: 711
Size: 19.7 KB](https://www.vbforums.com/attachment.php?s=5285fcd09204e7c1e3525be270b569f1&attachmentid=179148&d=1603829355)
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|