年会抽奖 抽奖系统 抽奖软件 C# Winform

年会抽奖软件:

 Q.Q 358189777

C#、  数据库Access;

1、系统启动,自动全屏展示。

2、背景随心切换、

3、快捷键方便自如:

  F1:弹出设置界面

  F2:查询人员名单、中奖名单

  F3:基础人员信息导入

  F4:查看当前抽奖项目已经抽取的名单

  F5:刷新

  F12:退出系统

  ESC:主界面自动最小化;其他设置界面自动关闭

  Delete:数据重置

有需要源码,在我的淘宝店下单  https://item.taobao.com/item.htm?id=542768246339 ;

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CJ.Ctrl;

namespace CJ
{
    public partial class MainFrom : Form
    {
        public MainFrom()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 人员抽奖组块
        /// </summary>
        public static List<UCNum> LstUcnum = new List<UCNum> ();
        public int YgNoLength = 6;//长度,参与人员的代号长度
        public int SX = 0;
        public int SY1 = 0;//抽奖项目框的Y周初始位置
        public int SY2 = 0;//抽奖项目框的Y轴结束位置
        public TextBox tbOtherCJNum = new TextBox();//其他抽奖项目,记录人数。
        public TextBox tbOtherCJNumName = new TextBox(); //其他抽奖项目,抽奖项目名称。
        public GroupBox GBQT = new GroupBox();
        public Image imgStr;//设置抽奖按钮背景图片,开始按钮
        public Image imgEnd;//设置抽奖按钮背景图片,结束按钮
        public int MaxNum = 0;//最大抽奖人数 
        public int SYNumn = 0;
        public   bool IsTrue = true;
        public static List<YG> LstYg = new List<YG>(); 
        public RadioButton RdioOrther = new RadioButton();
        public Label LbDispyCJ = new Label();
        public Button BtntherCJ = new Button();
        //用全局变量中转
        Point mouseLocation; 
        //---------------------------------------------------------------------
        public Prize prize = new Prize();
        public void SetBackgroundImage(string Imagestr,string Mrk)
        {
            if (Mrk == "主页")
            {
                this.BackgroundImage = Image.FromFile(Imagestr);
            }
            else if (Mrk == "开始")
            {
                lbbtn.Image = Image.FromFile(Imagestr);
                imgStr = Image.FromFile(Imagestr);
            }
            else
            {
                imgEnd = Image.FromFile(Imagestr);
            }
        }

         #region =========================================全屏设置================================================  
          [DllImport("user32.dll", EntryPoint = "ShowWindow")]
          public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);
          public const Int32 SW_SHOW = 5; public const Int32 SW_HIDE = 0;
  
          [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
          private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);
          public const Int32 SPIF_UPDATEINIFILE = 0x1;
          public const Int32 SPI_SETWORKAREA = 47;
         public const Int32 SPI_GETWORKAREA = 48;
  
         [DllImport("user32.dll", EntryPoint = "FindWindow")]
         private static extern Int32 FindWindow(string lpClassName, string lpWindowName); 

         /// <summary>  
         /// 设置全屏或这取消全屏  
         /// </summary>  
         /// <param name="fullscreen">true:全屏 false:恢复</param>  
         /// <param name="rectOld">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param>  
         /// <returns>设置结果</returns>  
         public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
         {
             Rectangle rectOld = Rectangle.Empty;
             Int32 hwnd = 0;
             hwnd = FindWindow("Shell_TrayWnd", null);//获取任务栏的句柄

             if (hwnd == 0) return false;

             if (fullscreen)//全屏
             {
                 ShowWindow(hwnd, SW_HIDE);//隐藏任务栏

                 SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get  屏幕范围
                 Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范围
                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗体全屏幕显示
             }
             else//还原 
             {
                 ShowWindow(hwnd, SW_SHOW);//显示任务栏

                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗体还原
             }
             return true;
         }
         #endregion =========================================全屏设置================================================       

         private void MainFrom_Load(object sender, EventArgs e)
         {
               tbOtherCJNum.TextChanged+=tbOtherCJNum_TextChanged;  //其他抽奖项目,记录人数。
               tbOtherCJNumName.TextChanged+=tbOtherCJNumName_TextChanged;  //其他抽奖项目,抽奖项目名称。
               BtntherCJ.Click += BtntherCJ_Click;
               tbOtherCJNum.KeyPress += tbOtherCJNum_KeyPress;
             //A.[员工代号],A.[员工名称],A.[部门代号],B.[部门名称],A.[抽奖状态],a.[中奖限制],A.[中奖号],A.[中奖名称]
             #region ==================================加载所有员工======================================
             DataTable DtAllYg = ReYg();
             for (int i = 0; i < DtAllYg.Rows.Count; i++)
             {
                 YG y = new YG();
                 YG xzy = new YG();
                 y.YG_NO = DtAllYg.Rows[i]["员工代号"].ToString();
                 y.YG_Name = DtAllYg.Rows[i]["员工名称"].ToString();
                 LstYg.Add(y); 
             }
             #endregion ==================================加载所有员工======================================
             BackgroundImageLoad();//背景图片加载
             #region =====================================全屏设置============================================
             SetFormFullScreen(true);
             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
             this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
             #endregion=====================================全屏设置============================================
             PrizeLoad();//抽奖项目加载 
             GetNoLength();//获取人员工号长度
             lbbtn.Tag = "开始";
         }

         void tbOtherCJNum_KeyPress(object sender, KeyPressEventArgs e)
         { 
             if (e.KeyChar != '\b')//这是允许输入退格键  
             {
                 if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                 {
                     e.Handled = true;
                 }
             }
         } 
         /// <summary>
         /// 获取所有参与抽奖的人员信息。
         /// </summary>
         /// <returns></returns>
         public DataTable ReYg()
         {
             DataTable dt = new DataTable();
             try
             {
                 string sql = "SELECT A.[员工代号],A.[员工名称],A.[部门代号],B.[部门名称],A.[抽奖状态],a.[中奖限制],A.[中奖号],A.[中奖名称]  FROM 人员表 as A left join 部门表  as B on A.[部门代号] = B.[部门代号] ;";
                 dt = AccessHelper.DataSet(sql).Tables[0];
             }
             catch (Exception ex)
             {
             }
             return dt;
         }
         void tbOtherCJNumName_TextChanged(object sender, EventArgs e)
         {
             lb项目号.Text = tbOtherCJNumName.Text;
             lbPrizeTile.Text = tbOtherCJNumName.Text;
         }
         void tbOtherCJNum_TextChanged(object sender, EventArgs e)
         {
             lb总人数.Text = tbOtherCJNum.Text;            
         }
        /// <summary>
        /// 获取人员工号长度
        /// </summary>
         public void GetNoLength()
         {
             //MaxNum
             string sql = "SELECT Num,NumType FROM MaxNum";
             DataTable dt = AccessHelper.DataSet(sql).Tables[0];
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 if (dt.Rows[i]["NumType"].ToString() == "W")
                 {
                     YgNoLength =Convert.ToInt32(dt.Rows[i]["Num"].ToString());
                 }
                 if (dt.Rows[i]["NumType"].ToString() == "N")
                 {
                     MaxNum =Convert.ToInt32( dt.Rows[i]["Num"].ToString());
                 }
             }
         }
        /// <summary>
        /// 背景图片加载
        /// </summary>
         public void BackgroundImageLoad()
         {
             try
             {
                 string sql0 = "SELECT A.[Path],a.[IType] FROM BackImage AS A";
                 DataTable DtBakImage = AccessHelper.DataSet(sql0).Tables[0];
                 for (int i = 0; i < DtBakImage.Rows.Count; i++)
                 {
                     if (DtBakImage.Rows[i]["IType"].ToString() == "Main")
                     {
                         this.BackgroundImage = Image.FromFile(DtBakImage.Rows[i]["Path"].ToString()); ;
                     }
                     else if (DtBakImage.Rows[i]["IType"].ToString() == "BtnStr")
                     {
                         imgStr = Image.FromFile(DtBakImage.Rows[i]["Path"].ToString());
                         lbbtn.Image = Image.FromFile(DtBakImage.Rows[i]["Path"].ToString()); ;
                     }
                     else if (DtBakImage.Rows[i]["IType"].ToString() == "BtnEnd")
                     {
                         imgEnd = Image.FromFile(DtBakImage.Rows[i]["Path"].ToString());
                     }
                     else if (DtBakImage.Rows[i]["IType"].ToString() == "Title")
                     {
                         LbGSMC.Text = DtBakImage.Rows[i]["Path"].ToString();
                     }
                     else
                     {
                         lb标语.Text = DtBakImage.Rows[i]["Path"].ToString();
                     }
                 }
             }
             catch (Exception)
             {

             }
         }
        /// <summary>
        /// 抽奖项目加载
        /// </summary> 
        public void PrizeLoad()
         { 
             try
             {
                 PMeiun.Controls.Clear();
                 int W = this.Width;//当前宽度
                 int H = this.Height;//当前高度
                 int PX = Convert.ToInt32(Math.Floor((H - 400) * 1.0 / 2));
                 PMeiun.Location = new System.Drawing.Point(0, PX); //位置
                 SY1 = PX;//获取并设置抽奖项目Y坐标初始位置
                 SY2 = PX + 400;//获取抽奖项目Y坐标做大范围的坐标
             }
             catch (Exception ex)
             { 
             }

             try
             {
                 string sql = "SELECT a.[排序],a.[项目号],a.[奖项名称],a.[中奖人数],a.[限制因素],a.[已经抽取],a.[批次数] FROM 抽奖规则 AS a ORDER BY a.排序 ASC ";
                 DataTable DtCJ = AccessHelper.DataSet(sql).Tables[0];
                 int RH = 0;
                 int RW = 0;
                 if (DtCJ.Rows.Count > 0)
                 {
                     RH = 28;// Convert.ToInt32(Math.Floor(400 * 1.0 / (DtCJ.Rows.Count + 2)));
                     RW = 178;
                     for (int i = 0; i < DtCJ.Rows.Count; i++)
                     {
                         RadioButton rad = new RadioButton();
                         rad.Location = new System.Drawing.Point(8, 5 + RH * i);
                         rad.Size = new System.Drawing.Size(RW, RH - 2);
                         rad.TabIndex = 0;
                         rad.TabStop = true;
                         rad.Text = DtCJ.Rows[i]["中奖人数"] + "人:" + DtCJ.Rows[i]["奖项名称"].ToString();
                         rad.UseVisualStyleBackColor = true;
                         rad.CheckedChanged += rad_CheckedChanged;
                         rad.Tag = DtCJ.Rows[i]["项目号"].ToString();                          
                         this.PMeiun.Controls.Add(rad);
                     }
                     RdioOrther.Location = new System.Drawing.Point(8, 5 + RH * DtCJ.Rows.Count);
                     RdioOrther.Size = new System.Drawing.Size(RW, RH - 1);
                     RdioOrther.TabIndex = 0;
                     RdioOrther.TabStop = true;
                     RdioOrther.Text = "临时加抽";
                     RdioOrther.UseVisualStyleBackColor = true;
                     RdioOrther.CheckedChanged += RdioOrther_CheckedChanged;
                     this.PMeiun.Controls.Add(RdioOrther);
                     //-------------------------------------------------------------------------------
                     GroupBox gb = new GroupBox();
                     gb.BackColor = System.Drawing.Color.Transparent;
                     gb.Location = new System.Drawing.Point(4, RH * (DtCJ.Rows.Count + 1) - 4);
                     gb.Name = "gB";
                     gb.Size = new System.Drawing.Size(170, 130);
                     gb.TabIndex = 7;
                     gb.TabStop = false;
                     gb.Visible = false;
                     GBQT = gb;
                     this.PMeiun.Controls.Add(GBQT);
                     //--------------------------------------------------------------------------------
                     Label lbotherCJ = new Label();
                     lbotherCJ.Text = "抽奖人数";
                     lbotherCJ.AutoSize = true;
                     lbotherCJ.Location = new System.Drawing.Point(10, 40);
                     lbotherCJ.Size = new System.Drawing.Size(32, 12);
                     this.GBQT.Controls.Add(lbotherCJ);
                     tbOtherCJNum.Location = new System.Drawing.Point(62, 35);
                     tbOtherCJNum.Size = new System.Drawing.Size(100, 21);
                     this.GBQT.Controls.Add(tbOtherCJNum);
                     //--------------------------------------------------
                     Label lbotherName = new Label();
                     lbotherName.Text = "奖项名称";
                     lbotherName.AutoSize = true;
                     lbotherName.Location = new System.Drawing.Point(10, 70);
                     lbotherName.Size = new System.Drawing.Size(32, 12);
                     this.GBQT.Controls.Add(lbotherName);
                     tbOtherCJNumName.Location = new System.Drawing.Point(62, 65);
                     tbOtherCJNumName.Size = new System.Drawing.Size(100, 21);
                     tbOtherCJNumName.Text = "加抽奖" + DateTime.Now.ToString("fffff") ;
                     this.GBQT.Controls.Add(tbOtherCJNumName);
                     //
                     BtntherCJ.Text = "保 存";
                     BtntherCJ.Location = new System.Drawing.Point(62, 95);
                     BtntherCJ.Size = new System.Drawing.Size(70, 21);
                     this.GBQT.Controls.Add(BtntherCJ);
                 }
             }
             catch (Exception ex)
             {
                 string xxxx = ex.Message.ToString();
             } 
         }

        /// <summary>
        /// 临时加抽奖,保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtntherCJ_Click(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(tbOtherCJNum.Text.Trim())==0)
                {
                    return;
                }
                string sql = "";
                prize.IsidNum = 1;                
                prize.Name = this.tbOtherCJNumName.Text.Trim();
                prize.Num = Convert.ToInt32(tbOtherCJNum.Text.Trim());
                prize.YJNum = 0;
                prize.YJIsidNum = 0;
                prize.ThisNum = prize.Num;
                prize.ReMark = "";
                sql = "Select  count(1)  From [抽奖规则] as A  Where A.[奖项名称] = '" + prize.Name + "' ";
                int count = Convert.ToInt32(AccessHelper.DataSet(sql).Tables[0].Rows[0][0]);
                if (count < 1)
                {
                    prize.Item = DateTime.Now.ToString("HHmmssffffff");
                    sql = "Select  Max(A.[排序])+1 From [抽奖规则] as A ";
                    prize.ID = Convert.ToInt32(AccessHelper.DataSet(sql).Tables[0].Rows[0][0]);
                    sql = "INSERT INTO  抽奖规则([排序],[项目号],[奖项名称],[中奖人数],[已经抽取],[批次数]) VALUES("
                        + prize.ID + ",'"
                        + prize.Item + "','"
                        + prize.Name + "',"
                        + prize.Num + ",  "
                        + prize.YJNum + ","
                        + prize.IsidNum + ")";
                    AccessHelper.ExecuteSql(sql);
                    lbPrizeTile.Text = prize.Name.ToString();
                }
            }
            catch (Exception ex)
            {
                 
            }
        }
        /// <summary>
        /// 事先设置好的项目的单选按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RdioOrther_CheckedChanged(object sender, EventArgs e)
        {
            LstUcnum.Clear();//清空已经抽奖明细
            LstUcnum = new List<UCNum>();
            //------------------------------------------
            panel1.Controls.Clear();
            //------------------------------------------
            tbOtherCJNum.Text="0";// = new TextBox();//其他抽奖项目,记录人数。
            tbOtherCJNumName.Text = "加抽"+">";// new TextBox(); //其他抽奖项目,抽奖项目名称。
            GBQT.Visible = true;
            lbPrizeTile.Text = "加抽奖";
            prize.ReMark = "新添加";
            this.lb项目号.Text = "加抽奖";
            this.lb总人数.Text = "0";
            this.lb总批次.Text = "1";
            this.lb已抽取.Text = "0";
            lb完成批次.Text = "0"; 
        }
        /// <summary>
        /// 未事先设置好的项目的单选按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void rad_CheckedChanged(object sender, EventArgs e)
        {
            LstUcnum.Clear();//清空已经抽奖明细
            LstUcnum = new List<UCNum>();
            //------------------------------------------
            panel1.Controls.Clear();
            //------------------------------------------
            RadioButton R = (RadioButton)sender;
            GBQT.Visible = false;
            prize = GetCJItem(R.Tag.ToString());           
            this.lb项目号.Text = prize.Name;
            this.lb项目号.Tag = prize.Item;
            this.lb已抽取.Text = prize.YJNum.ToString();
            this.lb总批次.Text = prize.IsidNum.ToString();
            this.lb总人数.Text = prize.Num.ToString();
            lb完成批次.Text = prize.YJIsidNum.ToString();
            if (prize.YJIsidNum != prize.IsidNum)
            {
                lb抽奖状态.Text = "" + (prize.YJIsidNum).ToString() + "轮抽奖结束";
                lbPrizeTile.Text = prize.Name+"" + (prize.YJIsidNum).ToString() + "轮抽奖结束";
            }
            else
            {
                lb抽奖状态.Text = "抽奖结束!";
                 lbPrizeTile.Text = prize.Name+ ":抽奖结束!";
            }           
            if (prize.YJIsidNum == 0)
            {
                lbPrizeTile.Text = prize.Name+":待抽奖!";
            } 
        }

        #region ========================================抽奖四步操作 ========================================
        /// <summary>
        /// 第一步、 获取抽奖人数
        /// </summary>
        /// <param name="item">抽奖号,抽奖项目号</param>
        /// <returns>返回抽奖信息</returns>
        public Prize GetCJItem(string item)
        {
            Prize Pze = new Prize();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT    ");
            sb.Append("    A.[排序],A.[项目号],A.[奖项名称],A.[中奖人数],A.[限制因素],A.[已经抽取],A.[批次数]    ");
            sb.Append("FROM [抽奖规则] AS A   ");
            sb.Append("WHERE A.[项目号] = '" + item + "'   ");
            DataTable DtCJNO = AccessHelper.DataSet(sb.ToString()).Tables[0];
            sb.Clear();
            sb.Append("SELECT     COUNT(1) FROM  [中奖人员] AS A  WHERE A.[中奖号] =  '" + item + "'    ");
            //抽奖人数
            int Count = Convert.ToInt32(AccessHelper.DataSet(sb.ToString()).Tables[0].Rows[0][0]);
            sb.Clear();
            string 奖项名称 = (DtCJNO.Rows[0]["奖项名称"] == null) ? "" : DtCJNO.Rows[0]["奖项名称"].ToString().Trim();
            int 中奖人数 = (DtCJNO.Rows[0]["中奖人数"] == null) ? 0 : Convert.ToInt32(DtCJNO.Rows[0]["中奖人数"].ToString().Trim());
            int 已经抽取 = (DtCJNO.Rows[0]["已经抽取"] == null) ? 0 : Convert.ToInt32(DtCJNO.Rows[0]["已经抽取"].ToString().Trim());
            int 批次数 = (DtCJNO.Rows[0]["批次数"] == null) ? 0 : Convert.ToInt32(DtCJNO.Rows[0]["批次数"].ToString().Trim());
            //、、----------------------------------------------------------
            int 已抽奖批数 = 0;
            int _MaxNum = Convert.ToInt32(Math.Floor(中奖人数 * 1.0 / 批次数));//每批抽奖最少人数
            //、、----------------------------------------------------------
            int Max = _MaxNum;
            for (int i = 1; i <= 批次数; i++)
            {
                if (i == 批次数)
                {
                    Max = 中奖人数 - (Max * (i - 1));
                }
                dic.Add(i, Max);
            }
            if (已经抽取 < 中奖人数)
            {
                已抽奖批数 = Convert.ToInt32(已经抽取 * 1.0 / _MaxNum);
            }
            else
            {
                已抽奖批数 = 批次数;
            }
            Pze.IsidNum = 批次数;
            Pze.Item = item;//项目号
            Pze.Name = 奖项名称;
            Pze.Num = 中奖人数;
            Pze.YJNum = 已经抽取;
            Pze.YJIsidNum = 已抽奖批数;
            Pze.Dic = dic;//每批次,抽取
            Pze.IsTrue = true;
            if (已抽奖批数 != 批次数)
            {
                Pze.ThisNum = dic[已抽奖批数 + 1];
            }
            //--------------------------------------------------------------------------------------------
            //抽奖程序异常,调整。
            if (已经抽取 != Count)
            {
                Pze.IsTrue = false;
                //已经抽取的人数 设置为系统后台抽奖的实际总人数
                已经抽取 = Count;
                //已经抽取 除以 每次抽取人数 然后取最小整数,作为已经抽奖完成的批次
                Pze.YJIsidNum = Convert.ToInt32(Math.Floor(已经抽取 * 1.0 / _MaxNum));
                //本次抽奖人数
                Pze.ThisNum = Pze.Dic[已经抽取 + 1] - (Count - _MaxNum * Pze.YJIsidNum);
            }
            //-----------------------------------------------------------------
            return Pze;
        }

        /// <summary>
        /// 第二步、根据抽奖人数随机抽取
        /// </summary>
        /// <param name="DtNoPrize">员工列表</param>
        /// <param name="Num">当前要抽出多少人</param>
        /// <param name="ItemNumber">抽奖号</param>
        /// <returns>返回中奖人员明细:List<YG></returns>
        public List<YG> SetZJNum(DataTable DtNoPrize, int Num, string ItemNumber)
        {
            List<YG> lst = new List<YG>();
            //所有未中奖的
            DataRow[] DrCJItem = DtNoPrize.Select("[抽奖状态] = '否' and (([中奖限制] = '普通') or ([中奖限制] = '必须中奖' and [中奖号] = '" + ItemNumber + "')) ");
            DataRow[] DrMax = DtNoPrize.Select("[抽奖状态] = '否' and [中奖限制] = '必须中奖' and [中奖号] = '" + ItemNumber + "'   ");
            if (DrCJItem.Length > Num)
            {
                foreach (DataRow dr in DrMax)
                {
                    YG yg = new YG();
                    yg.YG_NO = dr["员工代号"].ToString();
                    yg.YG_Name = dr["员工代号"].ToString();
                    int Count = lst.Where(t => t.YG_NO == yg.YG_NO).ToList().Count;
                    if (Count < 1) //.必须不能重复
                    {
                        lst.Add(yg);
                    }
                    if (lst.Count == Num)//如果等于设定数量自动跳出
                    {
                        break;
                    }
                }
                //循环随机获取一个员工
                while (true)
                {
                    Random ran = new Random();//
                    int i = ran.Next(DrCJItem.Length - 1);
                    YG yg = new YG();
                    yg.YG_NO = DrCJItem[i]["员工代号"].ToString();
                    yg.YG_Name = DrCJItem[i]["员工名称"].ToString();
                    int Count = lst.Where(t => t.YG_NO == yg.YG_NO).ToList().Count;
                    if (Count < 1)//.必须不能重复
                    {
                        lst.Add(yg);
                    }
                    if (lst.Count == Num)//如果等于设定数量自动跳出
                    {
                        break;
                    }
                }
            }
            return lst;
        }
        /// <summary>
        /// 第三步、分组框的设计
        /// </summary>
        /// <param name="lst">中奖人员列表</param>
        /// <param name="NoLength">工号长度</param>
        /// <param name="Num">此处中奖人数</param>
        public void LoadContrl(List<YG> lst, int NoLength, int Num, int ThisWidth, int ThisHeight, bool blcj)
        {
            int H = 0;//组块高度;
            int Hs = 0;//竖向高度 组块高度+间距
            int W = 0;//组块宽度
            int JG = 0;//组块高度间隔; 
            int Nqueue = 1;//最大放几排;
            int NC = 0;//每排人数
            int YJG = 0;//组块Y轴间隔; 
            int Mark = 93;//基数;需要大于等于93
            //--------------------------------------------
            int Hts = 0;//组块高度;
            int Hsts = 0;//竖向高度 组块高度+间距
            int Wts = 0;//组块宽度
            int JGts = 0;//组块高度间隔;

            int Nqueuets = 1;//最大放几排;
            int NCts = 0;//每排人数
            int YJGts = 0;//组块Y轴间隔;
            int XJGts = 0;//X轴间隔;
            int KJGts = 0;//空白间隔; 
            //--------------------------------------------
            H = Convert.ToInt32(Math.Floor(ThisHeight * 1.0 / Num)) - 3;
            W = H * (NoLength + 1) + H * 3 + 4;//"Num+1":每个小框的间隔;"H * 3":留足3个空位填名字,+4留足每排的间隔
            //组块宽度>容器宽度
            if (W > ThisWidth)//只放一排
            {
                W = Convert.ToInt32(ThisWidth * 1.0);
                H = Convert.ToInt32(
                        (
                            (ThisWidth - 4) * 1.0
                        ) / (Num + 1 + 3)
                    );
                JG = Convert.ToInt32(
                        Math.Floor(
                                     (
                                         ThisHeight - H * Num
                                    ) * 1.0 / Num
                            )
                    );
                for (int i = 0; i < lst.Count; i++)
                {
                    ContrlSet(0, i * (H + JG), W, NoLength, LstYg, lst[i].YG_NO, lst[i].YG_Name, blcj);
                }
            }
            else
            {
                Hts = 0;
                Wts = 0;
                Hsts = 0;
                YJGts = 0;
                Nqueuets = 1;
                NCts = 0;
                Nqueue = Convert.ToInt32(Math.Floor(ThisWidth * 1.0 / W));//最多可以放多少排!  
                //多排计算方式
                for (int i = 1; i <= Nqueue; i++)
                {
                    H = 0;//小模块高度;
                    W = 0;//小木块宽度;
                    NC = Convert.ToInt32(Math.Ceiling(Num * 1.0 / i));//总人数 / 排数 每排人数
                    H = Convert.ToInt32(Math.Floor((ThisHeight) * 1.0 / NC)) - 3;
                    Hs = H + 3;
                    W = H * (NoLength + 1) + H * 3 + 4;//"NoLength+1":每个小框的间隔;"H * 3":留足3个空位填名字,+4留足每排的间隔
                    if (W > (ThisWidth * 1.0 / i))//一排宽度;Width-3:留3个位置空余
                    {
                        W = Convert.ToInt32(Math.Floor(ThisWidth * 1.0 / i));
                        H = Convert.ToInt32(Math.Floor((W - 4 - NC) * 1.0 / (NC + 3))) - 3;
                        Hs = H + 3;
                    }
                    //-------------------------------------------------------------------------
                    YJG = Convert.ToInt32(
                            Math.Floor(
                                        (
                                            ThisHeight - Hs * NC
                                       ) * 1.0 / NC
                               )
                             );
                    //---------------------------------------------------------------------
                    if (H < Hts)
                    {
                        break;
                    }
                    Nqueuets = i;
                    Hts = H;
                    Wts = W;
                    YJGts = YJG;// JG2 = JG0;
                    NCts = NC;
                }
                Nqueue = Nqueuets;//最优排数
                H = Hts;//最优高度
                W = Wts;//最优组块宽度
                YJG = YJGts;//间隔
                NC = NCts; //每排人数
                int Counts = 0;
                for (int j = 0; j < NC; j++)
                {
                    Counts = j;
                    for (int t = 0; t < Nqueue; t++)
                    {
                        Counts = j * Nqueue + t;
                        ContrlSet(t * (W + 1), j * (H + YJG), W, NoLength, LstYg, lst[Counts].YG_NO, lst[Counts].YG_Name, blcj);
                        if (Counts == lst.Count - 1)
                        {
                            return;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 第四步、获取当前奖项抽奖人员明细
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public List<YG> SetZJNum(string item)
        {
            List<YG> lst = new List<YG>();
            string sql = "SELECT A.[员工代号] ,A.[员工名称],A.[部门代号],A.[中奖号],A.[中奖名称] FROM [中奖人员] AS A WHERE A.[中奖号]= '" + item + "'";
            DataTable dt = AccessHelper.DataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                YG yg = new YG();
                yg.YG_NO = dt.Rows[i]["员工代号"].ToString();
                yg.YG_Name = dt.Rows[i]["员工名称"].ToString();
                lst.Add(yg);
            }
            return lst;
        }
        /// <summary>
        /// 设置一个 单元块的长宽,以及各模块的大小
        /// </summary>
        /// <param name="X">模块的位置:X轴</param>
        /// <param name="Y">模块的位置:Y轴</param>
        /// <param name="Width">模块宽度;高度:(Width - 50) * 1.0 / N(小模块个数)</param>
        /// <param name="N">小模块个数</param>
        public void ContrlSet(int X, int Y, int Width, int N, List<YG> _lstyg, string ygno, string ygName, bool blcj)
        {
            //----------------------------------------------------------------------
            int H = Convert.ToInt32(
                                    (
                                        (Width - 4) * 1.0
                                    ) / (N + 1 + 3)
                                );
            UCNum uc = new UCNum(this);
            //uc.dt = DtTemp;
            uc.LstYg = _lstyg;
            uc.name = ygName;
            uc.YgNo = ygno;
            uc.Width = Width;//留足空余写名字
            uc.Height = H;// Convert.ToInt32(Math.Floor((Width - 50) * 1.0 / N));//高度
            uc.H = uc.Height - 4;
            uc.W = uc.Height - 4;
            //----------------------------------------------------------------------
            uc.Num = N;
            uc.Size = new System.Drawing.Size(uc.Width, uc.Height);//大小
            uc.Location = new System.Drawing.Point(X, Y); //位置
            uc.Set();
            LstUcnum.Add(uc);
            uc.Istrue = blcj;
            this.panel1.Controls.Add(uc);
            //----------------------------------------------------------------------
        } 
        #endregion ========================================抽奖四步操作 ========================================
        /// <summary>
        /// 获取所有人员明细
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public DataTable ReCJGZ(string Item)
        {
            if (Item.Trim() != "")
            {
                Item = "  Where  a.[项目号] = '" + Item + "'  ";
            }
            string sql = "SELECT a.[排序],a.[项目号],a.[奖项名称],a.[中奖人数],a.[限制因素],a.[已经抽取] FROM 抽奖规则 AS a " + Item + "  ORDER BY a.排序 ASC ";
            DataTable DtCJ = AccessHelper.DataSet(sql).Tables[0];
            return DtCJ;
        }
        #region ==========================================抽奖按钮========================================
        /// <summary>
        /// 抽奖按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbbtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbbtn.Tag.ToString() == "开始")
                {
                    IsTrue = true;
                    lbbtn.Tag = "结束";
                    lbbtn.Image = imgEnd;
                    //------------------------------------------
                    LstUcnum.Clear();//清空已经抽奖明细
                    LstUcnum = new List<UCNum>();
                    //------------------------------------------
                    panel1.Controls.Clear();
                    prize = GetCJItem(prize.Item);
                    lb项目号.Text = prize.Name;
                    lb总人数.Text = prize.Num.ToString();
                    lb已抽取.Text = prize.YJNum.ToString();
                    lb总批次.Text = prize.IsidNum.ToString();
                    lb完成批次.Text = prize.YJIsidNum.ToString();
                    List<YG> lstyg;
                    if (prize.YJIsidNum != prize.IsidNum)
                    {
                        lbPrizeTile.Text = prize.Name + ":当前正在抽取第" + (prize.YJIsidNum + 1).ToString() + "轮奖";
                        lb抽奖状态.Text = "当前正在抽取第" + (prize.YJIsidNum + 1).ToString() + "轮奖";
                        DataTable DtTemp = ReYg();//所有参与人员表                    
                        //---------------------------------------------
                        lstyg = SetZJNum(DtTemp, prize.ThisNum, prize.Item);
                        LoadContrl(lstyg, YgNoLength, prize.ThisNum, panel1.Width - 4, panel1.Height, true);
                        return;
                    }
                    else
                    {
                        lb抽奖状态.Text = "抽奖结束!";
                        lbbtn.Image = imgStr;
                        lbbtn.Tag = "开始";
                        lbPrizeTile.Text = prize.Name + ":抽奖完毕!";
                    }
                }
                else
                {
                    try
                    {
                        lbbtn.Image = imgStr;
                        lbbtn.Tag = "开始";
                        IsTrue = false;
                        for (int i = 0; i < LstUcnum.Count; i++)
                        {
                            LstUcnum[i].Istrue = false;
                        }
                        SaveYJ_CJ();//抽奖结束保存抽奖结果。
                        LstUcnum.Clear();
                        LstUcnum = new List<UCNum>();
                        lbPrizeTile.Text = prize.Name + "" + prize.YJIsidNum + "轮抽奖完毕!";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        } 
        /// <summary>
        /// 抽奖保存
        /// </summary>
        public void SaveYJ_CJ()
        {
            try
            {
                if (LstUcnum.Count > 0)
                {
                    int Count = 0;
                    string sql = "  ";
                    OleDbConnection conn = new OleDbConnection();
                    OleDbCommand comm = new OleDbCommand();
                    conn.ConnectionString = AccessHelper.connectionString;
                    comm.Connection = conn;
                    conn.Open();
                    comm.CommandType = CommandType.Text;
                    for (int i = 0; i < LstUcnum.Count; i++)
                    {
                        Count = Count + 1;                       
                        comm.CommandText = "UPDATE [人员表] SET [抽奖状态] = '是' WHERE [员工代号] ='" + LstUcnum[i].YgNo + "'";
                        comm.ExecuteNonQuery();
                        comm.CommandText = "INSERT INTO [中奖人员] ([员工代号],[员工名称],[中奖号],[中奖名称] )VALUES('" + LstUcnum[i].YgNo + "','" + LstUcnum[i].name + "','" + prize.Item + "','" + prize.Name + "')";
                        comm.ExecuteNonQuery();
                    }
                    //----------------------------------------------------
                    //获取此奖项已经抽过奖的数量
                    DataTable dt = new DataTable(); 
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    try
                    {
                        comm.CommandType = CommandType.Text;
                        comm.CommandText = "SELECT 已经抽取 FROM 抽奖规则 Where 项目号 = '" + prize.Item + "'";
                        da.SelectCommand = comm;
                        da.Fill(dt);
                    }
                    catch (Exception e)
                    {
                    }
                    int NumTemp = 0;
                    try
                    {
                       NumTemp = Convert.ToInt32(dt.Rows[0]["已经抽取"]);
                    }
                    catch (Exception )
                    {
                        NumTemp = 0;
                    }
                    //------------------------------------  
                    //保存已经并汇总已经抽奖过的
                    sql = "UPDATE 抽奖规则 SET 已经抽取 = " + (NumTemp + LstUcnum.Count).ToString() + "  WHERE 项目号 = '" + prize.Item + "' ";
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sql;
                    comm.ExecuteNonQuery();
                    //---------------------------------------------------------
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                        comm.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            //----------------------------------------------------------------
            try
            {
                prize = GetCJItem(prize.Item); 
                this.lb项目号.Text = prize.Name;
                this.lb项目号.Tag = prize.Item;
                this.lb已抽取.Text = prize.YJNum.ToString();
                this.lb总批次.Text = prize.IsidNum.ToString();
                this.lb总人数.Text = prize.Num.ToString();
                lb完成批次.Text = prize.YJIsidNum.ToString();
                if (prize.YJIsidNum != prize.IsidNum)
                {
                    lb抽奖状态.Text = "" + (prize.YJIsidNum).ToString() + "轮抽奖结束";
                }
                else
                {
                    lb抽奖状态.Text = "抽奖结束!";
                }                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            } 
        } 
        #endregion ==========================================抽奖按钮========================================

        /// <summary>
        /// 窗体大小变化后自动调整功能框的位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainFrom_Resize(object sender, EventArgs e)
        {
            int W = this.Width;
            int H = this.Height;
            int lw = Convert.ToInt32(Math.Floor(W * 1.0 / 2));
            int lw2 = Convert.ToInt32(Math.Floor(lbPrizeTile.Width * 1.0 / 2));
            int lw3 = Convert.ToInt32(Math.Floor(lb标语.Width * 1.0 / 2));
            lbPrizeTile.Location = new System.Drawing.Point(lw - lw2, 110); //位置
            lb标语.Location = new System.Drawing.Point(lw - lw3, 60); //位置

            //---------------------------------------------------------------------------
            int lwgsmc = Convert.ToInt32(Math.Floor(LbGSMC.Width * 1.0 / 2));
            LbGSMC.Location = new System.Drawing.Point(lw - lwgsmc, 5); //位置
            //---------------------------------------------------------------------------
            int PX = Convert.ToInt32(Math.Floor((H - 373) * 1.0 / 2));
            PMeiun.Location = new System.Drawing.Point(0, PX); //位置
            SY1 = PX;
            SY2 = PX + 400;
            //---------------------------------------------------------------------------
            groupBox1.Location = new System.Drawing.Point(W - groupBox1.Width - 5, PX); //位置 
            //----------------------------------------------------------------------------
        }

        private void timer1_Tick(object sender, EventArgs e)
        {  
            if (this.mouseLocation.X < 178 && mouseLocation.Y > SY1 && mouseLocation.Y < SY2)
            {
                PMeiun.Width = 178;
                PMeiun.Height = 400;
                Cursor = Cursors.Hand;
            }
            else
            {
                PMeiun.Width = 1;
                PMeiun.Height = 1; 
            } 
        }
        private void MainFrom_MouseMove(object sender, MouseEventArgs e)
        {
            this.mouseLocation = e.Location;
        }
        private void lbbtn_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }
        private void lbbtn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }         
        /// <summary>  
        /// 快捷键操作  
        /// </summary>  
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Space://空格  
                    {
                        try
                        {
                            if (lbbtn.Tag.ToString() == "开始")
                            {
                                IsTrue = true;
                                lbbtn.Tag = "结束";
                                lbbtn.Image = imgEnd;
                                //------------------------------------------
                                LstUcnum.Clear();//清空已经抽奖明细
                                LstUcnum = new List<UCNum>();
                                //------------------------------------------
                                panel1.Controls.Clear();
                                prize = GetCJItem(prize.Item);
                                lb项目号.Text = prize.Name;
                                lb总人数.Text = prize.Num.ToString();
                                lb已抽取.Text = prize.YJNum.ToString();
                                lb总批次.Text = prize.IsidNum.ToString();
                                lb完成批次.Text = prize.YJIsidNum.ToString();
                                List<YG> lstyg;
                                if (prize.YJIsidNum != prize.IsidNum)
                                {
                                    lbPrizeTile.Text = prize.Name + ":当前正在抽取第" + (prize.YJIsidNum + 1).ToString() + "轮奖";
                                    lb抽奖状态.Text = "当前正在抽取第" + (prize.YJIsidNum + 1).ToString() + "轮奖";
                                    DataTable DtTemp = ReYg();//所有参与人员表                    
                                    //---------------------------------------------
                                    lstyg = SetZJNum(DtTemp, prize.ThisNum, prize.Item);
                                    LoadContrl(lstyg, YgNoLength, prize.ThisNum, panel1.Width - 4, panel1.Height, true);
                                   
                                }
                                else
                                {
                                    lb抽奖状态.Text = "抽奖结束!";
                                    lbbtn.Image = imgStr;
                                    lbbtn.Tag = "开始";
                                    lbPrizeTile.Text = prize.Name + ":抽奖完毕!"; 
                                }

                            }
                            else
                            {
                                try
                                { 
                                    lbbtn.Image = imgStr;
                                    lbbtn.Tag = "开始";
                                    IsTrue = false;
                                    for (int i = 0; i < LstUcnum.Count; i++)
                                    {
                                        LstUcnum[i].Istrue = false;
                                    }
                                    SaveYJ_CJ();//抽奖结束保存抽奖结果。
                                    LstUcnum.Clear();
                                    LstUcnum = new List<UCNum>();
                                    lbPrizeTile.Text = prize.Name + "" + prize.YJIsidNum + "轮抽奖完毕!";
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message.ToString());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }                         
                    }
                    break;
                case Keys.F4://设置
                    {
                        try
                        {
                            panel1.Controls.Clear();
                            LstUcnum.Clear();
                            LstUcnum = new List<UCNum>();

                            List<YG> lstyg;
                            lstyg = SetZJNum(prize.Item);
                            LoadContrl(lstyg, YgNoLength, lstyg.Count, panel1.Width - 4, panel1.Height, false);
                        }
                        catch (Exception)
                        {
                             
                        }
                    }
                    break;
                case Keys.F1://设置
                    {
                        FormPrizeSet f = new FormPrizeSet(this);
                        f.ShowDialog();
                    }
                    break;
                case Keys.F2://查询功能等节目
                    {
                        FormSearch f = new FormSearch();
                        f.ShowDialog();
                        //MessageBox.Show("F2");
                    }
                    break;
                case Keys.F3://查询功能等节目
                    {
                        Form1 f = new Form1();
                        f.ShowDialog();
                    }
                    break;
                case Keys.Escape://查询功能等节目
                    {
                        SetFormFullScreen(false);
                        WindowState = System.Windows.Forms.FormWindowState.Minimized;
                    }
                    break;
                case Keys.Delete://查询功能等节目
                    {

                        try
                        {
                            panel1.Controls.Clear();
                            LstUcnum.Clear();
                            LstUcnum = new List<UCNum>();
                            int Count = 0;
                            string sql = "  ";
                            OleDbConnection conn = new OleDbConnection();
                            OleDbCommand comm = new OleDbCommand();
                            conn.ConnectionString = AccessHelper.connectionString;
                            comm.Connection = conn;
                            conn.Open();
                            //----------------------------------------------------------------------
                            comm.CommandType = CommandType.Text;
                            comm.CommandText = "UPDATE [人员表] SET [抽奖状态] = '否',中奖限制='普通',[中奖号]='',[中奖名称]='' ";
                            comm.ExecuteNonQuery();
                            //----------------------------------
                            comm.CommandText = "DELETE  FROM [中奖人员]";
                            comm.ExecuteNonQuery();
                            //-----------------------------------
                            comm.CommandText = "UPDATE [抽奖规则] SET [已经抽取]=0 ";
                            comm.ExecuteNonQuery();
                            //----------------------------------------------------------------------
                            if (conn.State == ConnectionState.Open)
                            {
                                conn.Close();
                                conn.Dispose();
                                comm.Dispose();
                            }
                            MessageBox.Show("重置完成");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                    break;
                case Keys.F5://查询功能等节目
                    {
                        panel1.Controls.Clear();
                        LstUcnum.Clear();
                        LstUcnum = new List<UCNum>();
                        //A.[员工代号],A.[员工名称],A.[部门代号],B.[部门名称],A.[抽奖状态],a.[中奖限制],A.[中奖号],A.[中奖名称]
                        #region ==================================加载所有员工======================================
                        DataTable DtAllYg = ReYg();
                        for (int i = 0; i < DtAllYg.Rows.Count; i++)
                        {
                            YG y = new YG();
                            YG xzy = new YG();
                            y.YG_NO = DtAllYg.Rows[i]["员工代号"].ToString();
                            y.YG_Name = DtAllYg.Rows[i]["员工名称"].ToString();
                            LstYg.Add(y);
                        }
                        #endregion ==================================加载所有员工======================================
                        BackgroundImageLoad();//背景图片加载
                        #region =====================================全屏设置============================================
                        SetFormFullScreen(true);
                        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                        #endregion=====================================全屏设置============================================
                        PrizeLoad();//抽奖项目加载 
                        GetNoLength();//获取人员工号长度
                        lbbtn.Tag = "开始";
                    }
                    break;
                case Keys.F12://查询功能等节目
                    {
                        SetFormFullScreen(false);
                        Application.Exit();
                    }
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            SetFormFullScreen(false);
        } 
    }
}
 

 源码9 9...¥,加我Q

posted @ 2016-12-14 10:50  蓝雨冰城  阅读(7394)  评论(0编辑  收藏  举报