我的首个silverlight软件---幸运转盘
这次公司要求做一个转盘抽奖游戏,后台程序不难,几天完工,但是前台的转盘难倒了我。我主要是做后台程序的,美工之类的很是差劲。刚开始想借助网络的力量,下载一个开源的转盘就可以了。但是,网上的软件要么不泄露源码要么收费。经过再三考虑决定用silverlight做一个。
先说一下思路,做两张图片,底部是一个转盘,上部覆盖一张透明的指针图片。然后让转盘图片旋转,指针固定,经过一段时间后,转盘图片停在某个角度。同时显示一个中奖提示。
下面是个人的核心代码:
<Grid x:Name="LayoutRoot" Background="White">
<Image x:Name="wheel" Width="550" Height="550" Source="gift.jpg" Stretch="Uniform" RenderTransformOrigin="0.5,0.5" Canvas.Top="212" Canvas.Left="228">
<Image.RenderTransform>
<RotateTransform x:Name="myTransform"/>
</Image.RenderTransform>
</Image>
<Image Width="25" Height="134" Source="point.png" Margin="174,278,401,138">
<Image.RenderTransform>
<TransformGroup>
<RotateTransform Angle="-85" />
</TransformGroup>
</Image.RenderTransform>
</Image>
<Button Content="抽 奖" Height="23" HorizontalAlignment="Left" Name="play" VerticalAlignment="Top" Width="75" Margin="600,150,0,0" Click="play_Click"/>
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="480,315,0,0" Name="end" VerticalAlignment="Top" Width="350" Foreground="#FFF21F1F" FontWeight="Bold" FontStyle="Italic" FontStretch="ExtraCondensed" Background="#FFF4F5E2" Content="谢谢参与!" FontSize="12" />
</Grid>
然后写一下button的单击事件处理函数:
private void play_Click(object sender, RoutedEventArgs e)
{
play.Visibility = Visibility.Collapsed;
myDispatcherTimer.Start();
if (this.gift != Gift.未中奖)
{
play.Click -= new RoutedEventHandler(play_Click);
play.Click += new RoutedEventHandler(Nav_Click);
}
}
private void Nav_Click(object sender, RoutedEventArgs e)
{
HtmlWindow html = HtmlPage.Window;
html.Navigate(new Uri("Client/Update?email=" + email, UriKind.Relative));
}
在这个事件当中,设置了一个DispatcherTimer,下面是该timer的tick函数:
private void Timer_Tick(object sender, EventArgs e)
{
if (speed > 15)
speed--;
else
{
if (speed < 22)
speed++;
}
myTransform.Angle = (myTransform.Angle + speed);
startTime = startTime.AddMilliseconds(50);
if (startTime.CompareTo(endTime) != -1)
{
if (gift == Gift.一等奖)
{
myTransform.Angle += 9;
myDispatcherTimer.Tick -= new EventHandler(Timer_Tick);
myDispatcherTimer.Tick += new EventHandler(Timer_Tick_three);
end.Content = "恭喜您中得一等奖,请点击顶部按钮填写信息以方便客服联系您!";
}
else if (gift == Gift.二等奖)
{
myTransform.Angle += 3;
myDispatcherTimer.Tick -= new EventHandler(Timer_Tick);
myDispatcherTimer.Tick += new EventHandler(Timer_Tick_three);
end.Content = "恭喜您中得二等奖,请点击顶部按钮填写信息以方便客服联系您!";
}
else if (gift == Gift.三等奖)
{
this.count = -1;
myTransform.Angle += 14;
myDispatcherTimer.Tick -= new EventHandler(Timer_Tick);
myDispatcherTimer.Tick += new EventHandler(Timer_Tick_three);
end.Content = "恭喜您中得三等奖,请点击顶部按钮填写信息,以方便客服联系您!";
}
else
{
myDispatcherTimer.Stop();
end.Content = "谢谢参与,您未中奖!";
end.Visibility = Visibility.Visible;
}
}
}
//本函数用来微调角度,以固定到某个奖品上面
private void Timer_Tick_three(object sender, EventArgs e)
{
myTransform.Angle += 8;
this.count++;
if (this.count == 3)
{
play.Visibility = Visibility.Visible;
play.Content = "填写信息";
myDispatcherTimer.Stop();
end.Visibility = Visibility.Visible;
}
}
最后,该sl程序需要接收两个参数,在app.xaml.cs中修改如下:
private void Application_Startup(object sender, StartupEventArgs e)
{
MainPage page = new MainPage(e.InitParams["gift"].ToString().Trim(), e.InitParams["email"].ToString().Trim());
this.RootVisual = page;
}
这两个参数需要在调用xap的页面中设置:
<param name="initParams" value="gift=2,email=zhaojw83@126.com" />
好了,大功搞成!
下面是效果图: