[英]I want make Dynamic window content(img&font&frame) size in WPF at c#
我想以1024 * 768的窗口大小和内容大小的比率填充监视器的全屏。
在宽显示器的Fill Blank Right Left Area用x:Name =“ wide_Out”填充黑色时,无论显示器的分辨率和类型如何,我如何才能使动态全屏窗口和内容(保持主胜率)保持不变。
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>
重要的部分是窗口的宽度和高度,viewbox元素,然后是wide_Out
网格的实际宽度和高度,设置内部尺寸和边距时将使用它们。
如果需要,可以使用窗口的BackgroundColor
属性在屏幕的长宽比不是4:3时(或在`wide_Out Grid中设置的任何值)为内容未覆盖的区域设置背景色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.