C# WPF抽奖程序

ops/2024/12/13 2:02:58/

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
需要源码请私信我!


http://www.ppmy.cn/ops/141401.html

相关文章

如何进行 JavaScript 性能优化?

要进行 JavaScript 性能优化&#xff0c;我们可以从多个角度进行思考&#xff0c;主要包括减少页面渲染时间、减少内存占用、优化代码执行效率等。以下是优化的一些方法&#xff0c;并结合实际项目代码示例讲解。 目录结构 减少 DOM 操作 缓存 DOM 元素批量更新 DOM 优化 Jav…

webstorm开发uniapp(从安装到项目运行)

1、下载uniapp插件 下载连接&#xff1a;Uniapp Tool - IntelliJ IDEs Plugin | Marketplace &#xff08;结合自己的webstorm版本下载&#xff0c;不然解析不了&#xff09; 将下载到的zip文件防在webstorm安装路径下&#xff0c;本文的地址为&#xff1a; 2、安装uniapp插…

【linux】shell(33)-函数

编写和使用函数是 shell 脚本编程中的一个重要方面。函数可以帮助你组织代码&#xff0c;提高代码的可读性和可重用性。 1. 定义函数 在 shell 脚本中&#xff0c;定义函数的基本语法有两种&#xff1a; # 语法 1 function function_name {# 函数体 }# 语法 2 function_name…

3356. 零数组变换 Ⅱ

3356、[中等] 零数组变换 Ⅱ 1、题目描述 给你一个长度为 n 的整数数组 nums 和一个二维数组 queries&#xff0c;其中 queries[i] [li, ri, vali]。 每个 queries[i] 表示在 nums 上执行以下操作&#xff1a; 将 nums 中 [li, ri] 范围内的每个下标对应元素的值 最多 减少…

CSS输入框动态伸缩动效

前言 下面我们将会做出如下图输入框样式&#xff0c;并且附上组件代码&#xff0c;有特殊需求的可以自行优化同理&#xff0c;下拉框的话只要把el-input标签修改掉即可 MyInput组件 <template><div class"my-input" click.stop"showInput !showInput…

搭建人工智能多模态大语言模型的通用方法

上一篇&#xff1a;《理解多模态大语言模型&#xff0c;主流技术与最新模型简介》 序言&#xff1a;动手搭建建多模态LLM的两大通用主流方法是&#xff1a;统一嵌入-解码器架构和跨模态注意力架构&#xff0c;它们都是通过利用图像嵌入与投影、跨注意力机制等技术来实现的。 …

深度学习视频编解码开源项目介绍【持续更新】

DVC (Deep Video Compression) 介绍:DVC (Deep Video Compression) 是一个基于深度学习的视频压缩框架,它的目标是通过深度神经网络来提高视频编码的效率,并降低比特率,同时尽可能保持视频质量。DVC 是一个端到端的神经网络模型,它在压缩视频时利用了视频帧之间的时间冗余…

63 基于单片机的四个速度比较

所有仿真详情导航&#xff1a; PROTEUS专栏说明-CSDN博客 目录 一、主要功能 二、硬件资源 三、主程序编程 四、资源下载 一、主要功能 基于51单片机&#xff0c;采用四个滑动变阻器连接数模转换器模拟四个速度值&#xff0c;通过LCD1602显示&#xff0c;然后检测出最高的…