WPF学习篇 之 星月传说

来源:互联网 发布:java线程sleep 编辑:程序博客网 时间:2024/04/29 07:47

 xaml 文件

<Page x:Class="wpftest.MoonAndSun"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ovt="clr-namespace:BOCO.OssView.Topo;assembly=BOCO.OssView.Topo"
    Title="MoonAndSun"
    >
  <Page.Resources>
    <ContextMenu x:Key="nodeContextMenu" StaysOpen="True">
      <MenuItem Header="{Binding Path=Host}"/>
      <Separator/>
      <MenuItem Header="可拖动" IsCheckable="True" IsChecked="{Binding Path=CanDrag}"/>
      <MenuItem Header="选中" IsCheckable="True" IsChecked="{Binding Path=IsSelected}"/>
      <MenuItem Header="显示" IsCheckable="True" IsChecked="{Binding Path=IsVisible}"/>
    </ContextMenu>
  </Page.Resources>
  <Grid>
    
      <ovt:NetworkPanel Name="plotPanel" Grid.Row="1" Grid.Column="0" />
     
      <ovt:ContainerPanel Grid.Row="1" Grid.Column="0" Name="myContainer" Background="Transparent" />
    </Grid>
</Page>


.cs文件

using System;
using System.Collections.Generic;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using BOCO.OssView.Model;
using System.Windows.Threading;
using BOCO.OssView.Topo;

namespace wpftest
{
    /// <summary>
    /// Interaction logic for MoonAndSun.xaml
    /// </summary>

    public partial class MoonAndSun : System.Windows.Controls.Page
    {
        private string mDemoInfo = "星月童话";
        Node sun;
        Node earth;
        Node moon;
        Node halei;
        Network network;
        DispatcherTimer timer;
        Ellipse sunEllipse;
        Ellipse earthEllipse;

        const int radiusX = 280;
        const int radiusY = 150;
        const int earthRadius = 50;
        double theta = Math.PI / 1440;
        int tick = 0;
        static MoonAndSun instance = null;
     
        public MoonAndSun()
        {
            InitializeComponent();
            if (instance == null)
                instance = this;
        }
        public static MoonAndSun Instance()
        {
            if (instance == null)
                instance = new MoonAndSun();
            return instance;
        }
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            sunEllipse = new Ellipse();
            sunEllipse.Stroke = Brushes.Yellow;
            sunEllipse.StrokeThickness = 1;
            sunEllipse.Width = radiusX * 2;
            sunEllipse.Height = radiusY * 2;
            myContainer.Children.Add(sunEllipse);

            earthEllipse = new Ellipse();
            earthEllipse.Stroke = Brushes.Yellow;
            earthEllipse.StrokeThickness = 1;
            earthEllipse.Width = earthRadius * 2;
            earthEllipse.Height = earthRadius * 2;
            myContainer.Children.Add(earthEllipse);


            plotPanel.DataSource = CreateNetwork();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(20);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }
        public Network CreateNetwork()
        {
            string name = "sun";

            sun = new Node();
            sun.CanDrag =false;
            sun.CenterX = -100;
            sun.CenterY = 0;
            sun.Name = name;
            sun.ImageUri = "pack://application:,,,/Images/sun.png";

            name = "earth";
            earth = new Node( );
            earth.CanDrag = true;
            earth.Name = name;
            earth.ImageUri = "pack://application:,,,/Images/earth.png";

            name = "moon";
            moon = new Node( );
            moon.CanDrag = false;
            moon.Name = name;
            moon.ImageUri = "pack://application:,,,/Images/moon.png";

            name = "HaLei";
            halei = new Node();
            halei.CanDrag = true;
            halei.ImageUri = "pack://application:,,,/Images/Star.png";

            name = "network";
            network = new Network( );
            network.Width = 1000;
            network.Height = 500;
            network.ImageUri = "pack://application:,,,/Images/star_small.JPG";
            network.Name = name;
            network.Children.Add(sun);
            network.Children.Add(earth);
            network.Children.Add(moon);
            network.Children.Add(halei);


            return network;
        }

        void timer_Tick(object sender, EventArgs e)
        {
           
            earth.CenterX = radiusX * Math.Cos(theta * tick);
            earth.CenterY = radiusY * Math.Sin(theta * tick);
            PlotPanel.SetCenterX(earthEllipse, earth.CenterX);
            PlotPanel.SetCenterY(earthEllipse, earth.CenterY);
            moon.CenterX = earth.CenterX + earthRadius * Math.Sin(10 * theta * tick);
            moon.CenterY = earth.CenterY + earthRadius * Math.Cos(10 * theta * tick);

            halei.CenterX = earth.CenterX - 10;
            halei.CenterY = earth.CenterY - 200;

            tick++;
        }

    }
}

dll没放上来。yanchanggang@boco.com.cn

原创粉丝点击