关闭

WPF学习篇 之 星月传说

682人阅读 评论(0) 收藏 举报

 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

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:68577次
    • 积分:1117
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:7篇
    • 译文:0篇
    • 评论:17条
    最新评论
    wpf文章