调整内容大小以适应屏幕分辨率

时间:2022-01-02 00:26:20

hello i have a window(wpf) with labels and text boxes, i want him to fit to the screen resolution as much as possible, how do i do it

你好我有一个带标签和文本框的窗口(wpf),我希望他尽可能地适应屏幕分辨率,我该怎么做

4 个解决方案

#1


25  

Viewbox is quite useful if you need the content of your window to scale proportionally when you resize the window (for example maximize it). In this minimalistic page

如果您需要在调整窗口大小时按比例缩放窗口内容(例如最大化窗口),Viewbox非常有用。在这个简约的页面中

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Viewbox>
        <StackPanel>
            <TextBlock FontSize="14">Example</TextBlock>
            <Border Background="Aqua" Width="100" Height="100"></Border>                    
        </StackPanel>
    </Viewbox>
</Window>

you have a TextBlock and a colored Border stacked vertically; if you start this xaml the window will have a size of 300x300, the font of the TextBlock will be 14 in size and the colored border will be 100x100. If you rescale the window you will see both the TextBlock and the Border scale accordingly (so they'll be no more of the size you've specified in the xaml), keeping relative proportions. Viewbox is really useful, in this respect, if you need a window whose internal components layout look always the same independently from the final resolution it will be displayed (what does matter is aspect-ratio, thought). This obviously work with any contents you'll put inside the Viewbox (we had an application with videos and 3D views for example). Note that in Visual Studio 2008 you'll not be able to see the content of the Viewbox in the Designer.

你有一个TextBlock和一个垂直堆叠的彩色边框;如果你启动这个xaml,窗口的大小将为300x300,TextBlock的字体大小为14,彩色边框的大小为100x100。如果您重新缩放窗口,您将相应地看到TextBlock和Border缩放(因此它们将不再是您在xaml中指定的大小),保持相对比例。在这方面,Viewbox非常有用,如果你需要一个窗口,其内部组件布局看起来总是独立于它将显示的最终分辨率(重要的是宽高比,思考)。这显然适用于您将放入Viewbox中的任何内容(例如,我们有一个带有视频和3D视图的应用程序)。请注意,在Visual Studio 2008中,您将无法在Designer中看到Viewbox的内容。

Hope this help.

希望这有帮助。

#2


2  

If you want to scale really everything including font sizes, you could probably apply a scale transform to your content, and bind it's X and Y values to the window's width and height. You would then also need a value converter to convert those to the appropriate scale.

如果你想真正扩展所有内容,包括字体大小,你可以对你的内容应用缩放变换,并将它的X和Y值绑定到窗口的宽度和高度。然后,您还需要一个值转换器来将它们转换为适当的比例。

#3


1  

If you want to scale everything to the size of the window just put everything inside a Viewbox control.

如果要将所有内容扩展到窗口大小,只需将所有内容放在Viewbox控件中即可。

#4


0  

Do you mean you want the window to fill the entire screen? The simplest way to do that (without causing further headaches) is to maximise the window.

你的意思是你想让窗户填满整个屏幕吗?最简单的方法(不会引起进一步的麻烦)是最大化窗口。

w.WindowState = WindowState.Maximized;

EDIT:

编辑:

A scalable window layout requires you to avoid using the XAML editor in Visual Studio! Actually you can do it in the editor, but it is very hard.

可伸缩的窗口布局要求您避免在Visual Studio中使用XAML编辑器!实际上你可以在编辑器中完成它,但这很难。

Much easier to write the XAML by hand:

手工编写XAML要容易得多:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Grid.Row="0">First Name</Label>
    <TextBox Grid.Column="1" Grid.Row="0" Name="firstName">Fred</TextBox>
    <Label Grid.Column="0" Grid.Row="1">First Name</Label>
    <TextBox Grid.Column="1" Grid.Row="1" Name="lastName">Smith</TextBox>
</Grid>

This will size to fit the window, though may look strange, as the rows and columns will by default get half the space each. You can override this so they have a height determined by their contents instead:

这将适合窗口大小,但可能看起来很奇怪,因为行和列默认情况下每个空间占一半。您可以覆盖它,以便它们的高度由其内容决定:

<RowDefinition Height="Auto"/>

It can also help to put margins on some controls, to space them out:

它还可以帮助将边距放在某些控件上,以便将它们分开:

<TextBox Grid.Column="1" Grid.Row="1" Margin="6" Name="lastName">Smith</TextBox>

#1


25  

Viewbox is quite useful if you need the content of your window to scale proportionally when you resize the window (for example maximize it). In this minimalistic page

如果您需要在调整窗口大小时按比例缩放窗口内容(例如最大化窗口),Viewbox非常有用。在这个简约的页面中

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Viewbox>
        <StackPanel>
            <TextBlock FontSize="14">Example</TextBlock>
            <Border Background="Aqua" Width="100" Height="100"></Border>                    
        </StackPanel>
    </Viewbox>
</Window>

you have a TextBlock and a colored Border stacked vertically; if you start this xaml the window will have a size of 300x300, the font of the TextBlock will be 14 in size and the colored border will be 100x100. If you rescale the window you will see both the TextBlock and the Border scale accordingly (so they'll be no more of the size you've specified in the xaml), keeping relative proportions. Viewbox is really useful, in this respect, if you need a window whose internal components layout look always the same independently from the final resolution it will be displayed (what does matter is aspect-ratio, thought). This obviously work with any contents you'll put inside the Viewbox (we had an application with videos and 3D views for example). Note that in Visual Studio 2008 you'll not be able to see the content of the Viewbox in the Designer.

你有一个TextBlock和一个垂直堆叠的彩色边框;如果你启动这个xaml,窗口的大小将为300x300,TextBlock的字体大小为14,彩色边框的大小为100x100。如果您重新缩放窗口,您将相应地看到TextBlock和Border缩放(因此它们将不再是您在xaml中指定的大小),保持相对比例。在这方面,Viewbox非常有用,如果你需要一个窗口,其内部组件布局看起来总是独立于它将显示的最终分辨率(重要的是宽高比,思考)。这显然适用于您将放入Viewbox中的任何内容(例如,我们有一个带有视频和3D视图的应用程序)。请注意,在Visual Studio 2008中,您将无法在Designer中看到Viewbox的内容。

Hope this help.

希望这有帮助。

#2


2  

If you want to scale really everything including font sizes, you could probably apply a scale transform to your content, and bind it's X and Y values to the window's width and height. You would then also need a value converter to convert those to the appropriate scale.

如果你想真正扩展所有内容,包括字体大小,你可以对你的内容应用缩放变换,并将它的X和Y值绑定到窗口的宽度和高度。然后,您还需要一个值转换器来将它们转换为适当的比例。

#3


1  

If you want to scale everything to the size of the window just put everything inside a Viewbox control.

如果要将所有内容扩展到窗口大小,只需将所有内容放在Viewbox控件中即可。

#4


0  

Do you mean you want the window to fill the entire screen? The simplest way to do that (without causing further headaches) is to maximise the window.

你的意思是你想让窗户填满整个屏幕吗?最简单的方法(不会引起进一步的麻烦)是最大化窗口。

w.WindowState = WindowState.Maximized;

EDIT:

编辑:

A scalable window layout requires you to avoid using the XAML editor in Visual Studio! Actually you can do it in the editor, but it is very hard.

可伸缩的窗口布局要求您避免在Visual Studio中使用XAML编辑器!实际上你可以在编辑器中完成它,但这很难。

Much easier to write the XAML by hand:

手工编写XAML要容易得多:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Grid.Row="0">First Name</Label>
    <TextBox Grid.Column="1" Grid.Row="0" Name="firstName">Fred</TextBox>
    <Label Grid.Column="0" Grid.Row="1">First Name</Label>
    <TextBox Grid.Column="1" Grid.Row="1" Name="lastName">Smith</TextBox>
</Grid>

This will size to fit the window, though may look strange, as the rows and columns will by default get half the space each. You can override this so they have a height determined by their contents instead:

这将适合窗口大小,但可能看起来很奇怪,因为行和列默认情况下每个空间占一半。您可以覆盖它,以便它们的高度由其内容决定:

<RowDefinition Height="Auto"/>

It can also help to put margins on some controls, to space them out:

它还可以帮助将边距放在某些控件上,以便将它们分开:

<TextBox Grid.Column="1" Grid.Row="1" Margin="6" Name="lastName">Smith</TextBox>