怎么看一个网站是不是仿站,关于网络编辑作业做网站栏目新闻的ppt,江苏建设教育网官网,云南建设网站【 声明#xff1a;版权所有#xff0c;欢迎转载#xff0c;请勿用于商业用途。 联系信箱#xff1a;feixiaoxing 163.com】 每到年末或者是尾牙的时候#xff0c;很多公司都会办一些年终的清楚活动#xff0c;感谢员工过去一年辛苦的付出。这个时候#xff0c;作为年会…【 声明版权所有欢迎转载请勿用于商业用途。 联系信箱feixiaoxing 163.com】 每到年末或者是尾牙的时候很多公司都会办一些年终的清楚活动感谢员工过去一年辛苦的付出。这个时候作为年会的重头戏那就是抽奖环节了。抽奖一般来说有很多的方法。比如说可以是物理抽奖的方法即每一个人写一个抽奖券放在统一的盒子里面也可以是电子的方法这就是今天说的抽奖程序。 前面既然我们已经学习了c# wpf那么完全可以自己做一个抽奖程序出来。在做之前我们可以假定抽奖的数字有几位有几个按钮显示效果怎么样。这部分不要怕麻烦可以自己设计一个效果图出来。 1、设计界面 界面部分的设计可以简单一点比如说只需要显示三个label和两个按钮。三个label也就是快速跳动的数字。而两个按钮则分别是开始按钮、结束按钮。开始按钮按下去的时候数字开始跳动等到结束按钮被按下去的时候那么屏幕上留下来的数字就是我们需要的抽奖数字。当然目前为止这里显示的数字只有三个如果公司规模比较大现实的数字可以根据需求添加几位也是可以的。设计完毕之后它的代码是这样的
Window x:ClassWpfApp.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfAppmc:IgnorabledTitleLottery Height450 Width800GridLabel x:Namenum1 Content0 ForegroundBlue FontSize80 HorizontalAlignmentLeft Margin297,103,0,0 VerticalAlignmentTop/Label x:Namenum2 Content0 ForegroundBlue FontSize80 HorizontalAlignmentLeft Margin362,103,0,0 VerticalAlignmentTop/Label x:Namenum3 Content0 ForegroundBlue FontSize80 HorizontalAlignmentLeft Margin432,103,0,0 VerticalAlignmentTop/Button x:Namestart ContentStart ForegroundGreen ClickStart_Click HorizontalAlignmentLeft Margin263,270,0,0 VerticalAlignmentTop Width95 Height45Button.ResourcesStyle TargetType{x:Type Border}Setter PropertyCornerRadius Value5/Setter/Style/Button.Resources/ButtonButton x:Namestop ContentStop ForegroundGreen ClickStop_Click HorizontalAlignmentLeft Margin415,270,0,0 VerticalAlignmentTop Width95 Height45Button.ResourcesStyle TargetType{x:Type Border}Setter PropertyCornerRadius Value5/Setter/Style/Button.Resources/Button/Grid
/Window 2、实际的设计效果 一般写完xaml之后对于效果还需要进行微调处理。比如字体大小字体位置按钮的大小按钮的位置按钮的颜色看一下效果 3、代码编写 实现了界面之后下面要做的就是界面代码编写。之前我们编写了倒计时软件它的做法就是每周期减去1。抽奖程序的代码其实很类似。首先我们需要定时生成一个随机数。当然为了体现公平性这个生成的周期要快一点不再是1s更新一次而是几十ms更新一次。随机数的生成不需要自己考虑随机算法而是直接使用c#自带的sdk即可。接着有了这个随机数之后下面就是把相关的数据显示到界面之上这和之前的倒计时软件是一样的。最后就是丰富start和stop按钮的编写start按钮代表开始随机数的生成而stop按钮则表示结束随机数的生成。因为抽奖是需要不停迭代进行的所以两个按钮基本上是一个互斥的关系。
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.Navigation;
using System.Windows.Shapes;namespace WpfApp
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{private int flag 0;private System.Windows.Threading.DispatcherTimer dispatcherTimer null;private int num 0;// construct functionpublic MainWindow(){InitializeComponent();stop.IsEnabled false;}// start button functionprivate void Start_Click(object sender, RoutedEventArgs e){dispatcherTimer null;dispatcherTimer new System.Windows.Threading.DispatcherTimer();dispatcherTimer.Tick new EventHandler(dispatcherTimer_Tick);dispatcherTimer.Interval new TimeSpan(0, 0, 0, 0, 20);dispatcherTimer.Start();start.IsEnabled false;stop.IsEnabled true;flag 1;}// timer functionprivate void dispatcherTimer_Tick(object sender, EventArgs e)//计时执行的程序{Random Ran new Random();num Ran.Next(1,999);if (flag 0){dispatcherTimer.Stop();return;}display_number();}private void display_number(){// display numnum1.Content Convert.ToString(num / 100);num2.Content Convert.ToString((num % 100) /10);num3.Content Convert.ToString(num % 10);}// stop button functionprivate void Stop_Click(object sender, RoutedEventArgs e){flag 0;start.IsEnabled true;stop.IsEnabled false;}}
}如上面代码所示整个逻辑当中比较重要的内容就是两部分分别是Start_Click和Stop_Click。前者创建一个定时器每20ms调用一下回调函数。在dispatcherTimer_Tick函数中利用Random生成一个随机数再利用display_number显示出来。当然如果发现flag0则代表定时器已经停止。后者Stop_Click则比较简单主要就是flag置位。 4、测试运行 测试相对而言就比较简单单击运行即可。观察一下Start按下去的时候数字有没有发生跳动Stop按下去的时候数字跳动有没有停止。如果一切ok那代表软件是编写成功的。 另外细心的同学发现了整个软件编写过程中有一个bug那就是已经中奖的同学还有可能再次中奖。所以这部分代码大家可以考虑下如何才能编写才能保证这个错误不再发生。