当前位置: 首页 > article >正文

C# WPF抽奖程序

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);//更新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
需要源码请私信我!


http://www.kler.cn/a/428910.html

相关文章:

  • 事务处理系统 (Transaction Processing System, TPS)
  • 【前端】CSS学习笔记
  • 软考中级复习篇章:数据结构部分的复习
  • 安路FPGA开发工具TD:问题解决办法 及 Tips 总结
  • Jenkins-Pipeline简述
  • Web前端------表单标签
  • 如何在UI自动化测试中创建稳定的定位器?
  • 笔记:在WPF中BitmapSource都有哪些派生类,他们主要功能,使用方法,使用场景
  • SpringBoot实现前后端传输加密设计
  • php项目的sdk封装成composer包的创建与发版
  • 【光电融合集成电路制造与封测】第四讲:扩散工艺,扩散的类型,恒定表面源扩散,限定表面源扩散,硼扩散
  • 分享一个您在汽车软件安全性测试中发现严重漏洞的案例,以及如何处理
  • 30天学会Go--第6天 GO语言 RESTful API 学习与实践
  • Tomcat(基础篇)
  • <router-view> 中key和name属性的用法详解以及案例
  • 试题转excel;pdf转excel;试卷转Excel,word试题转excel
  • 力扣 对称二叉树-101
  • 如何利用Python爬虫获得商品类目
  • ARM寄存器简介
  • 基于单片机的书写坐姿规范提醒器设计(论文+源码)
  • 粉丝生产力与开源 AI 智能名片 2+1 链动模式商城小程序的融合创新与价值拓展
  • PyTorch 本地安装指南:全面支持 macOS 、 Linux 和 Windows 系统
  • wazuh-modules-sca
  • 麒麟 V10 系统(arm64/aarch64)离线安装 docker 和 docker-compose
  • 使用trace-cmd跟踪Linux内核函数:一次愉快的内核探险
  • BurpSuite-7(自动化漏扫)