当前位置:网站首页>WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications
WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications
2022-07-28 11:35:00 【biyusr】
summary
all the time , We design windows Applications , It's all about sizing the controls , Whatever the size of the form , Will not change , This kind of design is no problem for the general application , But for some special applications , For example, there are background pictures , The whole screen needs to be paved , Because there are many different resolutions , So there will be confusion in the layout . Today we'll look at it WPF How to make our controls zoom in and out with the resolution . Let's write an example to see the effect ~
Problems in general layout
Here we write a simple page , newly build WPF project , stay MainWindow Add buttons inside , Here's the picture :

This page is very simple , There are only three buttons , What we think is that there are two buttons in the upper left corner and the lower right corner , There is a big button in the middle , Now there is no problem with the display , Then we can simulate the resolution change by changing the size of the button , Let's see what it will look like , Pictured :

The positions of the buttons have changed , Of course, here you will say , We can modify the layout so that the button is still displayed in the specified position , But when the user's screen resolution is too small ? Some controls will not be displayed . Here's the picture :

The solution to the problem
Answer the above question , Let's see how to layout , To solve the above problems , Next, we modify the layout code as follows , Use canvas Layout :
<Window x:Class="WpfApplication5.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><Viewbox Stretch=" fill"><Canvas Width=" 525" Height=" 350"><Button Content=" Key 1" HorizontalAlignment="Left" Margin="28,23,0,0" VerticalAlignment="Top" Width="75"/><Button Content=" Key 2" HorizontalAlignment="Left" Margin="416,275,0,0" VerticalAlignment="Top" Width="75"/></Canvas></Viewbox></Grid></Window>
The main part is that we are Canvas The outermost layer is added ViewBox, because ViewBox Of stretch Property default is Uniform, So we need to set it to Fill, This will ensure that the form is filled ,canvas The width and height of must be set , Otherwise it won't show . The display effect of shrinking :

The display effect when zooming in , Although the space is a little deformed , But the layout is not messy . Generally, the ratio of the display is standard , The deformation will not be so serious .
Viewbox Control description
ViewBox The function of a component is to stretch or extend the component in it , Make it have better layout and visual effect . This article will introduce the basic features and application examples of this component .
Component common properties :
Child: Get or set a ViewBox A single child of an element .
Stretch: Get or set the stretching mode to determine how the content in the component fills the existing space of the component .
StretchDirection: Get or set the stretching direction of the component to determine the form in which the content in the component will be extended .
Viewbox Changeable Stretch Attribute to select how the child elements are placed Viewbox Free space for ,Stretch The enumeration of is as follows :
Fill: Resize the content to fill the target size , The aspect ratio is not preserved ;
None: The content keeps its original size ;
Uniform: Adjust the size of the content while retaining the original aspect ratio of the content , To fit the target size .Viewbox Medium Stretch Property value defaults to Uniform;
UniformToFill: Adjust the size of the content while retaining the original aspect ratio of the content , To fill the target size . If the aspect ratio of the target rectangle is different from that of the source rectangle , Then the source content is trimmed to fit the target size ;
Canvas Control description
1.Canvas For container control , Used to locate
You can put Canvas Compare it to a coordinate system , All elements determine their position in the coordinate system by setting coordinates . The origin of this coordinate system is not in the center , But in its upper left corner . See the picture below
![]()
There are four ways to set the coordinates of elements :
Canvas.Top Set element spacing Canvas Distance from the top
Canvas.Bottom Set element spacing Canvas Distance from the bottom
Canvas.Left Set element spacing Canvas The distance from the left border
Canvas.Right Set element spacing Canvas The distance of the right boundary
<Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2"><Canvas Background="LightBlue" Width="400" Height="400"><Button Canvas.Top="50">Canvas.Top="50"</Button><Button Canvas.Bottom="50">Canvas.Bottom="50"</Button><Button Canvas.Left="50">Canvas.Left="50"</Button><Button Canvas.Right="50">Canvas.Right="50"</Button></Canvas></Border>
2. When set at the same time left and right,top and bottom, With left and top Subject to
<Canvas><Button Canvas.Left="20" Canvas.Top="20" Width="50" Height="20">button</Button></Canvas>
3. Overlap depth setting
<Canvas><Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue"/><Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="150" Canvas.Left="150" Fill="yellow"/><Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="200" Canvas.Left="200" Fill="green"/><!-- Reverse the order to illustrate z-index property --><Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="300" Canvas.Left="200" Fill="green"/><Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="350" Canvas.Left="150" Fill="yellow"/><Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="400" Canvas.Left="100" Fill="blue"/></Canvas>
边栏推荐
- What is WordPress
- 万字详解 Google Play 上架应用标准包格式 AAB
- Blackboard cleaning effect shows H5 source code + very romantic / BGM attached
- 使用c语言实现双向链表
- B2 sub theme / blog b2child sub theme / open source code
- Detailed explanations of%*d,%.*s, etc. of [C language]: "recommended collection"
- [half understood] zero value copy
- Iterative method for determinant (linear algebraic formula)
- I/O实操之对象流(序列化与反序列化)
- Remote shutdown of computer with mobile phone
猜你喜欢

【MySQL从入门到精通】【高级篇】(十)MyISAM的索引方案&&索引的优缺点

Unity鼠标带动物体运动的三种方法

18张图,直观理解神经网络、流形和拓扑

leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】

Refresh your understanding of redis cluster
![Leetcode:981. time based key value storage [trap of iteration for: on]](/img/87/759594104d61bf787693544dd7152d.png)
Leetcode:981. time based key value storage [trap of iteration for: on]

2022-2023 年十大应用程序发展趋势

Two dimensional prefix and

Boutique scheme | Haitai Fangyuan full stack data security management scheme sets a "security lock" for data

PKG packaging node project
随机推荐
什么是WordPress
微软安全团队发现一家利用Windows零日漏洞销售间谍软件的奥地利公司
Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]
What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
leetcode:1300. 转变数组后最接近目标值的数组和【二分】
Thinkphp5行为钩子Hook返回结果(数据)示例
Under the platform driven platform, the "dev- > dev.of_node" of the formal parameter dev in the probe function Understanding of
机器学习强基计划0-5:为什么学习的本质是泛化能力?
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Why does MySQL sometimes choose the wrong index?
保障邮箱安全,验证码四个优势
DHCP实验演示(Huawei交换机设备配置)
How to deal with invalid objects monitored by Oracle every day in the production environment?
Object to object mapping -automapper
web安全入门-Radius协议应用
18张图,直观理解神经网络、流形和拓扑
C language uses double pointer to realize simple factory mode (polymorphism)
STM32驱动ST7701S芯片(vⅰV0手机换屏价)
go status. Go status code definition
B2 sub theme / blog b2child sub theme / open source code