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

需要源码请私信我!

相关推荐
凤城老人26 分钟前
C++使用拉玛努金公式计算π的值
开发语言·c++·算法
HAH-HAH2 小时前
【Python 入门】(2)Python 语言基础(变量)
开发语言·python·学习·青少年编程·个人开发·变量·python 语法
递归不收敛3 小时前
一、Java 基础入门:从 0 到 1 认识 Java(详细笔记)
java·开发语言·笔记
zhangfeng11334 小时前
win7 R 4.4.0和RStudio1.25的版本兼容性以及系统区域设置有关 导致Plots绘图面板被禁用,但是单独页面显示
开发语言·人工智能·r语言·生物信息
子午6 小时前
Python的uv包管理工具使用
开发语言·python·uv
HMBBLOVEPDX6 小时前
C++(静态函数)
开发语言·c++
张晓~183399481217 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
dpxiaolong7 小时前
RK3588 Android12默认移除导航栏
开发语言·python
招风的黑耳7 小时前
Java生态圈核心组件深度解析:Spring技术栈与分布式系统实战
java·spring·wpf
Pocker_Spades_A7 小时前
Python快速入门专业版(二十九):函数返回值:多返回值、None与函数嵌套调用
服务器·开发语言·python