1

我想创建一个小程序,让我从一些互连件的图像中进行选择,以创建本质上是一条路径。这个想法是有一个带有按钮的起始部分,您可以在其中连接下一段路径。单击时,此按钮将提供可以放置在那里的选项的选择,然后将放置所选图像并将按钮移动到路径的新端。

我的问题是我的 c# 体验到目前为止仅限于静态 UI 和操作文本字段或整个新窗口。我不知道从哪里开始制作一个在程序初始启动后移动按钮和放置图像的 UI。我想也许使用网格控件和一些代码来操作它可能是答案,但真的不知道这样做的命令。我一直在为我以前的程序使用 WPF,并假设在这种情况下它仍然是可行的。

谁能指出我正确的方向来弄清楚如何动态控制程序窗口的一部分以实现我的目标?对于这个半模糊的问题,我很抱歉,但作为一个还是非常新的业余程序员,这已经超出了我的驾驶室。

4

1 回答 1

1

这是一个快速的、相当粗略的示例,但它显示了在运行时添加和定位控件的一些基础知识。

注意事项:

  • 如果您希望能够显式定位 x,y,请将控件添加到 Canvas。
  • 我将 UserControls 用于位置项,并添加了实例
  • 如果您正在构建一个游戏,并且它的大小和复杂性都很大,请不要使用 WPF。我去过那里,做到了,在上面建立了一个完整的多人太空街机/动作游戏。玩游戏太慢了。

这是下面的代码转储。

//PathBuilding.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TravianResourceProd" x:Class="TravianResourceProd.PathBuilding"
        Title="PathBuilding" Height="466.377" Width="621.509">
    <Grid x:Name="drawingGrid"  Background="#FFC2C2C2" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>
        <Canvas Background="#FFB3B3B3" Margin="0,0,0,-0.377" x:Name="DrawCanvas" MouseMove="DrawCanvas_MouseMove" MouseUp="Grid_MouseUp">
            <local:ActiveLocation x:Name="primarySegment" HorizontalAlignment="Left" VerticalAlignment="Top" Loaded="ActiveLocation_Loaded" Canvas.Left="67" Canvas.Top="98"/>

        </Canvas>
    </Grid>
</Window>

//PathBuilding.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TravianResourceProd
{
    /// <summary>
    /// Interaction logic for PathBuilding.xaml
    /// </summary>
    public partial class PathBuilding : Window
    {
        public PathBuilding()
        {
            InitializeComponent();
            _ConnectorLine = new Line();
            _ConnectorLine.Stroke = new SolidColorBrush(Colors.DarkBlue);
            _ConnectorLine.Visibility = System.Windows.Visibility.Hidden;
            _locationSelector = new LocationOptions();
            _locationSelector.Visibility = System.Windows.Visibility.Hidden;
            DrawCanvas.Children.Add(_ConnectorLine);
            DrawCanvas.Children.Add(_locationSelector);

        }

        private Line _ConnectorLine;
        private bool _AddMode = false;


        private LocationOptions _locationSelector;

        private void ActiveLocation_Loaded(object sender, RoutedEventArgs e)
        {
            primarySegment.btnAddSegment.Click += (object sender1, RoutedEventArgs e1) =>
            {
                //show the type selector
                _locationSelector.Visibility = System.Windows.Visibility.Visible;
                var loc = _locationSelector.TransformToAncestor(drawingGrid)
                          .Transform(new Point(0, 0));
                Canvas.SetLeft(_locationSelector, Mouse.GetPosition(DrawCanvas).X + 80);
                Canvas.SetTop(_locationSelector, Mouse.GetPosition(DrawCanvas).Y - 50);

            };
            _locationSelector.btnTypeOne.Click += (object s, RoutedEventArgs e2) =>
            {
                _AddMode = true;
                _ConnectorLine.Visibility = System.Windows.Visibility.Visible;
                _locationSelector.Visibility = System.Windows.Visibility.Hidden;
            };
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (!_AddMode)
                return;
            _AddMode = false;
            _ConnectorLine.Visibility = System.Windows.Visibility.Hidden;

            //Add the one we picked
            var oldLoc = new OldLocation();
            Canvas.SetLeft(oldLoc, Canvas.GetLeft(primarySegment));
            Canvas.SetTop(oldLoc, Canvas.GetTop(primarySegment));
            DrawCanvas.Children.Add(oldLoc);

            //Add a line connecting old to new
            var newestLine = new Line();
            newestLine.Visibility = System.Windows.Visibility.Visible;

            newestLine.Stroke = new SolidColorBrush(Colors.Brown);

            newestLine.X1 = _ConnectorLine.X1;
            newestLine.Y1 = _ConnectorLine.Y1;
            newestLine.X2 = _ConnectorLine.X2 + 40;
            newestLine.Y2 = _ConnectorLine.Y2 + 50;
            DrawCanvas.Children.Add(newestLine);

            //Move the active/primary to the new location
            Canvas.SetLeft(primarySegment, e.GetPosition(this).X);
            Canvas.SetTop(primarySegment, e.GetPosition(this).Y);

        }

        private void DrawCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {//reposition the line going from active location to mouse
                _ConnectorLine.X1 = Canvas.GetLeft(primarySegment) + 70;
                _ConnectorLine.Y1 = Canvas.GetTop(primarySegment) + 50;
                _ConnectorLine.X2 = e.GetPosition(this).X - 5;
                _ConnectorLine.Y2 = e.GetPosition(this).Y - 5;
            }
            catch (Exception)
            {
            }
        }
    }
}

//LocationOptions.xaml

<UserControl x:Class="TravianResourceProd.LocationOptions"
             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" Height="109.359" Width="117.057">
    <Grid Margin="0,0,-0.17,0.094">
        <Button x:Name="btnTypeOne" Content="Type One" HorizontalAlignment="Left" VerticalAlignment="Top" Width="117" Height="33" Margin="0,0,-0.17,0"  />
        <Button x:Name="btnTypeTwo" Content="Type Two" HorizontalAlignment="Left" VerticalAlignment="Top" Width="117" Margin="0,38,-0.17,0" Height="33"  />

    </Grid>
</UserControl>

//旧位置.xaml

<UserControl x:Class="TravianResourceProd.OldLocation"
             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" Height="80" Width="80">
    <Grid>
        <Ellipse Stroke="#FF686868" StrokeThickness="8">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF373737" Offset="1"/>
                    <GradientStop Color="#FF929292"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>

    </Grid>
</UserControl>

//ActiveLocation.xaml

<UserControl x:Class="TravianResourceProd.ActiveLocation"
             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" Height="80" Width="80">
    <Grid>
        <Ellipse Stroke="#FF1A9000" StrokeThickness="6">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF62745E" Offset="1"/>
                    <GradientStop Color="#FF929292"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <Button x:Name="btnAddSegment" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="20" Height="22" FontSize="30" Margin="60,30,-0.302,0"/>

    </Grid>
</UserControl>
于 2013-08-05T02:42:34.737 回答