C# WPF抽奖程序

C# WPF抽奖程序

python 复制代码
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);//更新x
                var 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

需要源码请私信我!

相关推荐
潜意识起点5 分钟前
Java数组:静态初始化与动态初始化详解
java·开发语言·python
小猪写代码20 分钟前
C语言:递归函数(新增)
算法·c#
点云SLAM22 分钟前
C++创建文件夹和文件夹下相关操作
开发语言·c++·算法
2301_8091774726 分钟前
2025.01.15python商业数据分析
开发语言·python
滴_咕噜咕噜28 分钟前
学习笔记(prism--视频【WPF-prism核心教程】)--待更新
笔记·学习·wpf
_小柏_42 分钟前
C/C++基础知识复习(46)
c语言·开发语言·c++
SomeB1oody1 小时前
【Rust自学】6.4. 简单的控制流-if let
开发语言·前端·rust
明月逐人归4641 小时前
输出语句及变量定义
开发语言·python
tatasix1 小时前
Go Redis实现排行榜
开发语言·redis·golang
吴冰_hogan1 小时前
Java虚拟机(JVM)的类加载器与双亲委派机制
java·开发语言·jvm