我想在 C# 的 WPF 中制作 Dynamic window content(img&font&frame) 大小
I want make Dynamic window content(img&font&frame) size in WPF at c#
enter image description here
enter image description here
我想用 window 尺寸和内容尺寸的 1024*768 比例填充显示器的全屏。
当使用宽屏显示器的填充空白左右区域填充黑色时使用x:Name="wide_Out"
无论显示器分辨率和类型如何,如何制作动态全屏 window 和内容(保持主要获胜率)。
mainwindow.xaml
<Window x:Class="C.MainWindow"
xmlns:local="clr-namespace:M_C"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024" WindowStyle="None">
<Grid x:Name="wide_Out" Margin="0,0,0,0">
<Grid Height="768" Width="1024">
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737"
LastChildFill="False" VerticalAlignment="Top" Width="62"
Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top"
Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top"
Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80"
TextWrapping="Wrap"
Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top"
Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7"
Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top"
Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70"
VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left"
Height="537" LastChildFill="False" Margin="144,100,0,0"
VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Grid>
</Window>
如果我没理解错的话,您需要做的是用 Viewbox
包裹 Window
元素的内容。下面是示例代码,每个元素我只写了重要的部分,其他可以根据需要设置:
<Window
Height="{x:Static SystemParameters.PrimaryScreenHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}" <!--To set your window size to the size of monitor-->
WindowStyle="None" <!--To not display any controls-->
>
<Viewbox> <!--You can try using different 'Stretch' attribute values-->
<Grid Width="768" Height="1024" x:Name="wide_Out"> <!--Or whatever dimensions you want your 'base' window to work with-->
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737" LastChildFill="False" VerticalAlignment="Top" Width="62" Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top" Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top" Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top" Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7" Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top" Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70" VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left" Height="537" LastChildFill="False" Margin="144,100,0,0" VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Viewbox>
</Window>
重要的部分是 window 宽度和高度、视图框元素以及 wide_Out
网格的实际宽度和高度,您将在设置内部尺寸和边距时使用它们。
如果需要,您可以使用 window 的 BackgroundColor
属性设置屏幕宽高比不是 [=21 时未被内容覆盖的区域的背景颜色=](或您在 `wide_Out 网格中设置的任何内容)。
enter image description here
enter image description here
我想用 window 尺寸和内容尺寸的 1024*768 比例填充显示器的全屏。
当使用宽屏显示器的填充空白左右区域填充黑色时使用x:Name="wide_Out" 无论显示器分辨率和类型如何,如何制作动态全屏 window 和内容(保持主要获胜率)。
mainwindow.xaml
<Window x:Class="C.MainWindow"
xmlns:local="clr-namespace:M_C"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024" WindowStyle="None">
<Grid x:Name="wide_Out" Margin="0,0,0,0">
<Grid Height="768" Width="1024">
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737"
LastChildFill="False" VerticalAlignment="Top" Width="62"
Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top"
Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top"
Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80"
TextWrapping="Wrap"
Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top"
Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7"
Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top"
Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70"
VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left"
Height="537" LastChildFill="False" Margin="144,100,0,0"
VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Grid>
</Window>
如果我没理解错的话,您需要做的是用 Viewbox
包裹 Window
元素的内容。下面是示例代码,每个元素我只写了重要的部分,其他可以根据需要设置:
<Window
Height="{x:Static SystemParameters.PrimaryScreenHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}" <!--To set your window size to the size of monitor-->
WindowStyle="None" <!--To not display any controls-->
>
<Viewbox> <!--You can try using different 'Stretch' attribute values-->
<Grid Width="768" Height="1024" x:Name="wide_Out"> <!--Or whatever dimensions you want your 'base' window to work with-->
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737" LastChildFill="False" VerticalAlignment="Top" Width="62" Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top" Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top" Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top" Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7" Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top" Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70" VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left" Height="537" LastChildFill="False" Margin="144,100,0,0" VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Viewbox>
</Window>
重要的部分是 window 宽度和高度、视图框元素以及 wide_Out
网格的实际宽度和高度,您将在设置内部尺寸和边距时使用它们。
如果需要,您可以使用 window 的 BackgroundColor
属性设置屏幕宽高比不是 [=21 时未被内容覆盖的区域的背景颜色=](或您在 `wide_Out 网格中设置的任何内容)。