在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → WPF抽奖源码下载(适合年会抽奖等)

WPF抽奖源码下载(适合年会抽奖等)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:5.89M
  • 下载次数:56
  • 浏览次数:1000
  • 发布时间:2016-05-10
  • 实例类别:C#语言基础
  • 发 布 人:hbsyuyang
  • 文件格式:.rar
  • 所需积分:2
 相关标签: wpf 抽奖 wp

实例介绍

【实例简介】

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();
        }
    }
}

标签: wpf 抽奖 wp

实例下载地址

WPF抽奖源码下载(适合年会抽奖等)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警