单击时用图片替换按钮,然后在WPF中的图片顶部创建一个新按钮

时间:2013-08-04 09:34:52

标签: c# wpf

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

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

是否有人能指出我正确的方向来弄清楚如何动态控制程序窗口的一部分以实现我的目标?对于这个半模糊的问题,我感到很遗憾,但作为一个非常新的爱好者程序员,这个问题远远超出了我的驾驶室。

1 个答案:

答案 0 :(得分:1)

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

注意事项:

  • 如果您希望能够明确定位x,y,请将控件添加到画布。
  • 我使用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>

// OldLocation.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>