C# WPF抽奖程序
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
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;
using System.Windows.Threading;namespace RndChs
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public static string PicPath = "/Images";private double centerX = 400;private double centerY = 300;private double cycleWidth = 300;private double cycleHeight = 60;private double degree = 0;bool isHighSpeed = false;//速度控制double normalSpeed = 0.4;double highSpeed = 4.0;//项集合类List<ItemShow> itemList = new List<ItemShow>();List<ItemShow> removeList = new List<ItemShow>();ItemShow topItem = null;private double itemWidth = 160;private double itemHeight = 80;private double currentOpacity = 0;private DispatcherTimer timer;public MainWindow(){InitializeComponent();this.KeyUp += MainWindow_KeyUp;PicPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "/Images";}void MainWindow_KeyUp(object sender, KeyEventArgs e){if (cbKeyStart.IsChecked.GetValueOrDefault(true) && e.Key == Key.S){// 按下“静音”键btnStart_Click(this, null);}}private void MainWindow_Loaded(object sender, RoutedEventArgs e){timer = new DispatcherTimer();timer.Tick += new EventHandler(timer_Tick);TimeSpan sp = new TimeSpan(0, 0, 0, 0, 20);timer.Interval = sp;ItemSet();}private void ItemSet(){centerX = this.Width / 2;centerY = this.Height / 3 * 2;cycleWidth = this.Width / 2 * 0.8;cycleHeight = this.Height / 9;itemList = new List<ItemShow>();moveCanvas.Children.RemoveRange(0, moveCanvas.Children.Count);shower.Source = null;showerName.Text = "";DirectoryInfo theFolder = new DirectoryInfo(MainWindow.PicPath);FileInfo[] fileInfos = theFolder.GetFiles();foreach (FileInfo fileInfo in fileInfos){if (fileInfo.Extension == ".jpg"|| fileInfo.Extension == ".png"|| fileInfo.Extension == ".gif"|| fileInfo.Extension == ".bmp"){if (removeList.Where(i => i.FileName == fileInfo.Name).Count() > 0){continue;}ItemShow item = new ItemShow();Image image = item.imgPic;Uri uri = new Uri(fileInfo.FullName, UriKind.RelativeOrAbsolute);BitmapImage bitmap = new BitmapImage(uri);image.Source = bitmap;item.FileName = fileInfo.Name;image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);image.MouseLeftButtonUp += new MouseButtonEventHandler(image_MouseLeftButtonUp);itemList.Add(item);moveCanvas.Children.Add(item);}}timer.Start();}private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){timer.Stop();Image img = (Image)sender;currentOpacity = img.Opacity;img.Opacity = 1;shower.Source = img.Source;}private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){Image img = sender as Image;img.Opacity = currentOpacity;shower.Source = null;showerName.Text = "";timer.Start();}private void timer_Tick(object sender, EventArgs e){MoveNext();}private void MoveNext(){normalSpeed = normalSlider.Value;highSpeed = highSlider.Value;for (var i = 0; i < itemList.Count; i++){//创建一个圆周var tmp = (degree + (360 / itemList.Count) * i) % 360;tmp = tmp * Math.PI / 180;var posX = (cycleWidth) * Math.Sin(tmp);//更新xvar posY = (cycleHeight) * Math.Cos(tmp); //更新y ItemShow item = itemList[i];//根据宽高计算缩放比例double scale = (2 * cycleHeight - posY) / (3 * cycleHeight + itemHeight / 2);Canvas.SetLeft(item, centerX + posX - (itemWidth / 2) * scale);Canvas.SetTop(item, centerY - posY - (itemHeight / 2) * scale);Canvas.SetZIndex(item, int.Parse(Math.Ceiling(itemList.Count * 10 * scale).ToString()));item.ZIndex = int.Parse(Math.Ceiling(itemList.Count * itemList.Count * scale).ToString());//创建并应用变形属性ScaleTransform st = new ScaleTransform();st.ScaleX = scale;st.ScaleY = scale;item.RenderTransform = st;item.Opacity = scale;}if (isHighSpeed){degree = degree - highSpeed;topItem = itemList.OrderByDescending(o => o.ZIndex).FirstOrDefault();shower.Visibility = Visibility.Visible;shower.Source = topItem.imgPic.Source;var index = topItem.FileName.IndexOf('.');showerName.Text = topItem.FileName.Substring(0, index);}else{degree = degree - normalSpeed;}}private void btnRefresh_Click(object sender, RoutedEventArgs e){ItemSet();}private void btnReset_Click(object sender, RoutedEventArgs e){removeList = new List<ItemShow>();ItemSet();}private void btnCapture_Click(object sender, RoutedEventArgs e){CaptureWindow cw = new CaptureWindow();cw.Closing += cw_Closing;cw.ShowDialog();}void cw_Closing(object sender, System.ComponentModel.CancelEventArgs e){ItemSet();}private void btnStart_Click(object sender, RoutedEventArgs e){if (!isHighSpeed){if (cbNeedRemove.IsChecked.GetValueOrDefault(true) && topItem != null){moveCanvas.Children.Remove(topItem);itemList.Remove(topItem);removeList.Add(topItem);}if (itemList.Count < 2){MessageBox.Show("还抽啥,直接发奖呗!");return;}isHighSpeed = true;timer.Start();btnStart.Content = "停止";}else{btnStart.IsEnabled = false;timer.Stop();isHighSpeed = false;timer.Start();btnStart.Content = "开始";btnStart.IsEnabled = true;}}private void btnSet_Click(object sender, RoutedEventArgs e){System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();fbd.ShowDialog();if (fbd.SelectedPath != string.Empty){MainWindow.PicPath = fbd.SelectedPath;}ItemSet();}}
}
程序包链接:
https://download.csdn.net/download/weixin_43050480/90091690
需要源码请私信我!