【问题标题】:How do I keep the ellipses in the center when the screen is resized调整屏幕大小时如何将椭圆保持在中心
【发布时间】:2017-10-20 23:50:58
【问题描述】:

我得到了以下 WPF 表单。我是 WPF 的新手 .. 即使调整大小,我如何将椭圆保持在中心?我包含了调整大小时缩放的代码

<Window x:Name="mainWindow" x:Class="SpectrumVisualizer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="369.737" Width="567.487" Loaded="Window_Loaded" SizeChanged="mainWindow_SizeChanged">
    <DockPanel>
        <Canvas x:Name="MyCanvas" Margin="0,-41,-14,0" DockPanel.Dock="Left">

            <Viewbox Stretch="Uniform" Height="380">
                <Grid Name="MainGrid" Background="Black" Height="339" Width="499" Canvas.Top="41">

                    <Grid.ColumnDefinitions>

                    </Grid.ColumnDefinitions>
                    <Ellipse x:Name="Bar01"   Fill="Red" HorizontalAlignment="Left" Height="300" Stroke="Black" VerticalAlignment="Top" Width="467" Margin="19,32,0,0"/>
                    <Ellipse x:Name="Bar02"  Fill="Orange" HorizontalAlignment="Left" Margin="105,32,0,0" Stroke="Black" Width="295" VerticalAlignment="Top" Height="300"/>
                    <Ellipse x:Name="Bar03"  Fill="Yellow" HorizontalAlignment="Left" Height="141" Margin="19,107,0,0" Stroke="Black" VerticalAlignment="Top" Width="467"/>
                    <Ellipse x:Name="Bar04"  Fill="LimeGreen" HorizontalAlignment="Left" Height="232" Margin="138,64,0,0" Stroke="Black" VerticalAlignment="Top" Width="231"/>
                    <Ellipse x:Name="Bar05"  Fill="Green" HorizontalAlignment="Left" Height="280" Margin="194,42,0,0" Stroke="Black" VerticalAlignment="Top" Width="120"/>
                    <Ellipse x:Name="Bar06"  Fill="Turquoise" HorizontalAlignment="Left" Height="106" Margin="67,125,0,0" Stroke="Black" VerticalAlignment="Top" Width="363"/>
                    <Ellipse x:Name="Bar07"  Fill="Cyan" HorizontalAlignment="Left" Height="183" Margin="167,88,0,0" Stroke="Black" VerticalAlignment="Top" Width="174"/>
                    <Ellipse x:Name="Bar08"  Fill="Blue" HorizontalAlignment="Left" Height="232" Margin="204,64,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
                    <Ellipse x:Name="Bar09"  Fill="Violet" HorizontalAlignment="Left" Height="75" Margin="105,139,0,0" Stroke="Black" VerticalAlignment="Top" Width="295"/>
                    <Ellipse x:Name="Bar10"  Fill="Magenta" HorizontalAlignment="Left" Height="129" Margin="194,119,0,0" Stroke="Black" VerticalAlignment="Top" Width="120"/>

                </Grid>
            </Viewbox>
        </Canvas>
    </DockPanel>
</Window>

后面的代码:

private void mainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
    MyCanvas.Width = e.NewSize.Width;
    MyCanvas.Height = e.NewSize.Height;

    double xChange = 1, yChange = 1;

    if (e.PreviousSize.Width != 0)
        xChange = (e.NewSize.Width / e.PreviousSize.Width);

    if (e.PreviousSize.Height != 0)
        yChange = (e.NewSize.Height / e.PreviousSize.Height);



    ScaleTransform scale = new ScaleTransform(MyCanvas.LayoutTransform.Value.M11 * xChange, MyCanvas.LayoutTransform.Value.M22 * yChange);
    MyCanvas.LayoutTransform = scale;
    MyCanvas.UpdateLayout();
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:
    1. 在其父项中居中。
    2. 摆脱它周围所有抵消它的多余东西。摆脱您的 SizeChanged 事件处理程序。使用 HoriziontalAlignment 和 VerticalAlignment 进行实际的居中。

    像这样:

    <Window 
        x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        >
        <Grid Background="Black">
            <Viewbox Stretch="Fill">
                <!-- 
                Note added right/bottom margin on this Grid. 
                The ellipses already provided a left/top margin. 
                If it were me, I'd have the ellipses aligned at zero top/left, 
                and do all the margins in the Grid. 
                -->
                <Grid Name="MyCanvas" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,19,19">
    
                    <Ellipse x:Name="Bar01"  Fill="Red"       HorizontalAlignment="Left" Height="300" Margin="19,32,0,0"  Stroke="Black" VerticalAlignment="Top" Width="467" />
                    <Ellipse x:Name="Bar02"  Fill="Orange"    HorizontalAlignment="Left" Height="300" Margin="105,32,0,0" Stroke="Black" VerticalAlignment="Top" Width="295"  />
                    <Ellipse x:Name="Bar03"  Fill="Yellow"    HorizontalAlignment="Left" Height="141" Margin="19,107,0,0" Stroke="Black" VerticalAlignment="Top" Width="467"/>
                    <Ellipse x:Name="Bar04"  Fill="LimeGreen" HorizontalAlignment="Left" Height="232" Margin="138,64,0,0" Stroke="Black" VerticalAlignment="Top" Width="231"/>
                    <Ellipse x:Name="Bar05"  Fill="Green"     HorizontalAlignment="Left" Height="280" Margin="194,42,0,0" Stroke="Black" VerticalAlignment="Top" Width="120"/>
                    <Ellipse x:Name="Bar06"  Fill="Turquoise" HorizontalAlignment="Left" Height="106" Margin="67,125,0,0" Stroke="Black" VerticalAlignment="Top" Width="363"/>
                    <Ellipse x:Name="Bar07"  Fill="Cyan"      HorizontalAlignment="Left" Height="183" Margin="167,88,0,0" Stroke="Black" VerticalAlignment="Top" Width="174"/>
                    <Ellipse x:Name="Bar08"  Fill="Blue"      HorizontalAlignment="Left" Height="232" Margin="204,64,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
                    <Ellipse x:Name="Bar09"  Fill="Violet"    HorizontalAlignment="Left" Height="75" Margin="105,139,0,0" Stroke="Black" VerticalAlignment="Top" Width="295"/>
                    <Ellipse x:Name="Bar10"  Fill="Magenta"   HorizontalAlignment="Left" Height="129" Margin="194,119,0,0" Stroke="Black" VerticalAlignment="Top" Width="120"/>
    
                </Grid>
            </Viewbox>
        </Grid>
    </Window>
    

    【讨论】:

    • 正如我提到的,我对 WPF 非常陌生。所以我仍然试图弄清楚事情。但是当我最大化形状相应缩放的窗口时,调整大小功能和画布就在那里
    • 太棒了!它保持居中。我注释掉了调整大小功能,现在缩放显然已经消失了。有没有办法在 XAML 中对其进行缩放?
    • @Eminem 查看更新。 ViewBox 接管了您在 SizeChanged 处理程序中所做的工作。 ViewBox是个放荡的野兽;直观地不清楚如何让它做你想做的事。
    • @Eminem 太棒了。我也很喜欢你的图片。
    • 你应该看看它是如何亮起来的......我对音乐进行了快速傅立叶变换,椭圆随着节拍淡入淡出...... :) 夜总会会变得疯狂。 . 多亏了你! :)
    【解决方案2】:

    我使用居中对齐中心。试试看。

    查看cannot center align canvas中的使用示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2016-02-23
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多