gpt4 book ai didi

c# - 如何随着窗口大小的变化动态测量wpf中的网格宽度/高度

转载 作者:行者123 更新时间:2023-11-30 17:55:50 25 4
gpt4 key购买 nike

总体而言,我想要做的是在 Canvas 上添加一个椭圆,以表示当高尔夫球推杆从圆心以特定角度击打高尔夫球时,高尔夫球在圆周上的位置。为此,我使用了两张图片,其中一张是一个分成多个部分的圆圈,代表击球的角度。另一个图像只是一个推杆头,它被旋转以表示球杆击球的角度。该图像位于圆形图像之上。蓝色椭圆代表球的位置。在下面的链接中查看从我的应用程序获得的图像输出:

Required application output

为此,我基本上使用以下代码计算了网格的宽度和高度:

public FacePage()
{
InitializeComponent();
GridGraphs.SizeChanged += GridGraphs_SizeChanged;
}

void GridGraphs_SizeChanged(object sender, SizeChangedEventArgs e)
{
GetMeasurements();
}

private void GetMeasurements()
{
GridGraphs.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

double width = GridGraphs.DesiredSize.Width;
double height = GridGraphs.DesiredSize.Height;

RotatePutter(width, height);
}

然后将宽度和高度传递给 RotatePutter(),其中宽度和高度除以 2 以获得网格的中心点,如下所示:

public void RotatePutter(double width, double height)
{

double radius = width * 0.5;
double centerX = width * 0.5;
double centerY = height * 0.5;

Random ran = new Random();
double angle = ran.Next(-15, 15);

if (angle >= 0)
{
if (angle <= 5)
{
lblShotStaus.Content = "Square";
}
else
{
lblShotStaus.Content = "Open";
}
}
else
{
lblShotStaus.Content = "Closed";
}

double angleradians = (angle * (Math.PI / 180));

Point putterCentre = new Point(0.5, 0.5);

imgPutter.RenderTransformOrigin = putterCentre;

RotateTransform rt = new RotateTransform(angle, 0.5, 0.5);
imgPutter.RenderTransform = rt;

BallLocation(radius, angleradians, centerX, centerY);
}

中心点、半径、角度和推杆图像在此处旋转,并将半径、角度弧度、centerX 和 centerY 传递给 BallLocation 以计算球的位置,如下所示:

public void BallLocation(double rad, double anglerad, double centerX, double centerY)
{
Ellipse ellipse = new Ellipse();

double xBallPoint = (centerX - (rad * Math.Cos(anglerad)));
double yBallPoint = (centerY - (rad * Math.Sin(anglerad)));

ellipse.Height = 10;
ellipse.Width = 10;
ellipse.Fill = new SolidColorBrush(Colors.Aqua);

Canvas.SetLeft(ellipse, xBallPoint);
Canvas.SetTop(ellipse, yBallPoint);
canvasFaceAngle.Children.Add(ellipse);
}

这在全屏模式下工作正常,一旦我改变窗口的大小,球的位置和它被击中的角度都是错误的。

有谁知道如何随着窗口大小的变化动态获取网格宽度和高度,以便计算椭圆(球)的正确位置。或者也欢迎使用任何其他替代方法来完成此操作。提前致谢。

这是我的 xaml:

<UserControl x:Class="HoleMorePuttsApplication.Pages.FacePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="60*"/>
</Grid.ColumnDefinitions>
<Viewbox Grid.ColumnSpan="1">
<Label Content="Face Angle Page" FontWeight="Bold" />
</Viewbox>
<Grid Name="GridGraphs" Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="columnLeft" Width="50*"/>
<ColumnDefinition Name="columnRight" Width="50*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Name="rowTop" Height="70*"/>
<RowDefinition Name="rowBottom" Height="30*"/>
</Grid.RowDefinitions>

<Image x:Name="imgAngles" Source="C:\Users\BernardWork\Documents\Work\Net\PuttingApp\Images\360Angles.jpg" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Image x:Name="imgPutter" Source="C:\Users\BernardWork\Documents\Work\Net\PuttingApp\Images\Putter.jpg" Opacity="0.5" Margin="130,105,70,105" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<Label x:Name="lblShotStaus" Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Canvas Name="canvasFaceAngle" Grid.ColumnSpan="2" Grid.Row="0" RenderTransformOrigin="0.543,0.511"></Canvas>


</Grid>

</Grid>

最佳答案

如果我理解正确,我认为问题可能在于:

double width = GridGraphs.DesiredSize.Width;
double height = GridGraphs.DesiredSize.Height;

您正在调用没有提供边界的 Measure,这意味着它将告诉您 Grid 在没有限制的情况下的大小(因此,DesiredSize)。 DesiredSizeActualSize 可能是两个截然不同的值。

在上述情况下,SizeChanged 方法的参数将为您提供所需的内容。

void GridGraphs_SizeChanged(object sender, SizeChangedEventArgs e)
{
RotatePutter(e.NewSize.Width, e.NewSize.Height);
}

关于c# - 如何随着窗口大小的变化动态测量wpf中的网格宽度/高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14712489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com