一个唱片商店的小例子 可以自动旋转唱片 点击弹出唱片

效果图

 

XAML

 1.ShopShow_Page

 

  <navigation:Page x:Class="SilverlightApplication6.ShopShow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="800" d:DesignHeight="600"
           Title="ShopShow Page">
    <Grid x:Name="LayoutRoot">
        <Grid.Background>
            <ImageBrush ImageSource="../Images/win7-6.jpg" Stretch="Fill" >     
            </ImageBrush>
        </Grid.Background>
        <Image x:Name="shower" Width="180" Height="180" Stretch="Fill" Visibility="Collapsed">
            <Image.Effect>
                <DropShadowEffect Color="white" BlurRadius="10" Opacity="0.8" ShadowDepth="0">    
                </DropShadowEffect>
            </Image.Effect>
        </Image>
        <Canvas x:Name="moveCanvas"></Canvas>
        <StackPanel Orientation="Horizontal" Margin="300,500,0,0">
            <Button Width="50" Height="30" Content="Play" Margin="10" x:Name="btn_Start" Click="btn_Start_Click" ></Button>
            <Button Width="50" Height="30" Content="Stop" Margin="10" x:Name="btn_Stop" Click="btn_Stop_Click"></Button>
        </StackPanel>
    </Grid>
</navigation:Page>

 


2 ShopItem

 

<UserControl x:Class="SilverlightApplication6.ShopItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid x:Name="LayoutRoot">
        <Image x:Name="obj" Width="135" Height="135" Stretch="None">
        </Image>
    </Grid>
</UserControl>

 

C# 代码

 

class ShopShow

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace SilverlightApplication6
{
    public partial class ShopShow :Page
    {
       private DispatcherTimer timer;
       private List<ShopItem> objList = new List<ShopItem>();
       private double count = 14;//唱片数目
       private double degree = 0;//便宜变量
       private double width = 300; // 椭圆长半径
       private double height = 60; //椭圆短半径
       private double centerX = 400;//中心点 X坐标
       private double centerY = 300;//中心点 Y坐标
       private double itemWidth = 160; //物件宽度
       private double itemHeight = 80; //物件高度
       private double currentOpacity = 0;//透明度

       public ShopShow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ShopShow_Loaded);
        }

       void ShopShow_Loaded(object sender, RoutedEventArgs e)
       {
           timer = new DispatcherTimer();
           for (var i = 1; i <count; i++)
           {
               //实例化用户控件
               ShopItem item = new ShopItem();
               Image image = item.obj;
               //加载唱片图片
               Uri uri = new Uri(string.Format("../Images/album{0}.png",i),UriKind.Relative);
               BitmapImage bitmap = new BitmapImage(uri);
               image.Source = bitmap;
               //绑定控件事件
               image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);
               image.MouseEnter += new MouseEventHandler(image_MouseEnter);
               image.MouseLeave += new MouseEventHandler(image_MouseLeave);
               objList.Add(item);
               moveCanvas.Children.Add(item);
           }
           timer.Tick += new EventHandler(timer_Tick);
           TimeSpan sp = new TimeSpan(0,0,0,0,20);
           timer.Interval = sp;
           timer.Start();
       }
        //产生椭圆运动效果的方法
       public void StartMove()
       {
           for (var i = 0; i < objList.Count; i++)
           {
               //根据控件总数和圆周计算一个平均值
               var tmp=(degree+(360/count)*i)%360;
               tmp = tmp * Math.PI /180;//更新x
               var posX = width * Math.Sin(tmp);//更新X
               var posY = height * Math.Cos(tmp);//更新Y
               ShopItem obj = objList[i];
               //根据宽高计算缩放比例
               double scale =(2*height - posY)/(3*height+itemHeight/2);
               Canvas.SetLeft(obj,centerX + posX - (itemWidth / 2) * scale);
               Canvas.SetTop(obj, centerY - posY - (itemHeight / 2) * scale);
               Canvas.SetZIndex(obj,int.Parse(Math.Ceiling(count*scale).ToString()));
               ScaleTransform st = new ScaleTransform(){ CenterX = scale, ScaleY = scale };
               obj.RenderTransform = st;
               obj.Opacity = scale;
           }
           degree -= 0.3;
       }
      
       void timer_Tick(object sender, EventArgs e)
       {
           StartMove();
       }

       void image_MouseLeave(object sender, MouseEventArgs e)
       {
           Image img = sender as Image;
           img.Opacity = currentOpacity;
           timer.Start();
       }

       void image_MouseEnter(object sender, MouseEventArgs e)
       {
           timer.Stop();
           Image img = sender as Image;
           currentOpacity = img.Opacity;
           img.Opacity = 1;

       }

       void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           Image img = sender as Image;
           shower.Visibility = Visibility.Visible;
           shower.Source = img.Source;
       }

       private void btn_Start_Click(object sender, RoutedEventArgs e)
       {
           timer.Start();
       }

       private void btn_Stop_Click(object sender, RoutedEventArgs e)
       {
           timer.Stop();
       }   
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值