c#类似跑马灯抽签小程序

原创性申明

此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/40207663如果进行转载请注明出处。本文作者原创,邮箱zhujunxxxxx@163.com,如有问题请联系作者

最近受朋友所托,需要做一个抽签的程序。我想应该也不难,就是随机嘛,这在c#里面也是相当容易的。于是就着手做了前后也就一小时。

我不善于做UI,界面很丑,请勿喷。

先展示一下程序界面


说一下布局,左边就是一个ListView 视图模式是大图标方式,这里注意下 因为要绑定一个imagelist 记得要修改下 imagelist里面图片显示的size,否则会图片很小。

流程是这样的:

添加好预选名单后点击开始,这时候就循环切换 listview的item的ImageIndex(使用两张图片,一张是选中一张未选中的)

这里切换item的图片是单独放在一个线程里面的,线程访问ListView必须使用委托来访问,否则会出错

跑马灯效果的实现,就是刚开始切换的时间长点,接下来时间越来越短,这里用的是Thread的sleep来做的



下面给出代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Brower;

namespace Award
{
    public partial class Form1 : Form
    {
        public static string thinkshop = "";
        public static int NO_IMAGE = 4;
        public static int YES_IMAGE = 5;
        public Form1()
        {
            InitializeComponent();
            this.shoplist.View = View.LargeIcon;
            this.shoplist.LargeImageList = this.imageList1;
            timer1.Start();
            readload();
            load();//加载商家
        }
        private delegate void ListViewDelegate(int index);
        public void ChangeItem(int index)
        {
            if (this.shoplist.InvokeRequired)//不能访问就创建委托
            {
                ListViewDelegate d = new ListViewDelegate(ChangeItem);
                shoplist.Invoke(d, new object[] { index });
            }
            else
            {
                ChangeIndex(index);
            }
        }
        //让index的item成为选中状态
        public void ChangeIndex(int index)
        {
            //this.shoplist.BeginUpdate();

            for (int i = 0; i < shoplist.Items.Count; i++)//先把所有的item恢复原样
            {
                if (i != index)
                {
                    if (shoplist.Items[i].ImageIndex==YES_IMAGE)
                        shoplist.Items[i].ImageIndex = NO_IMAGE;//设置为未选中样式

                }
                else
                    shoplist.Items[i].ImageIndex = YES_IMAGE;//设置为选中状态
            }

            //this.shoplist.EndUpdate();  //结束数据处理,UI界面一次性绘制。  
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (txt_shopname.Text == "")
            {
                MessageBox.Show("不能为空!");
                return;
            }

            this.shoplist.BeginUpdate();
            ListViewItem lvi = new ListViewItem();
            lvi.ImageIndex = NO_IMAGE;
            lvi.Text = txt_shopname.Text;

            this.shoplist.Items.Add(lvi);
            this.shoplist.EndUpdate();  //结束数据处理,UI界面一次性绘制.
            txt_shopname.Text = "";
            l_shopnum.Text = shoplist.Items.Count.ToString(); 
        }

       

        private delegate string ListViewItemDelegate(int index);
        //获取item的项值
        public string GetItem(int index)
        {
            if (this.shoplist.InvokeRequired)//不能访问就创建委托
            {
                ListViewItemDelegate d = new ListViewItemDelegate(GetItem);
                return shoplist.Invoke(d, new object[] { index }).ToString();

            }
            else
            {
                if (shoplist.Items.Count > 0 && index < shoplist.Items.Count)
                    return shoplist.Items[index].Text;
                else
                {
                    return "error";
                }

            }
        }

        public bool flag = true;
        public void Effect(object result)
        {
            int time = 900;
            while (flag)
            {
                for (int j = 0; j < shoplist.Items.Count; j++)
                {
                    Thread.Sleep(time);
                    if (time > 100)
                            time -= 200;
                    ChangeItem(j);
                }
            }

            for (int j = 0; j < shoplist.Items.Count; j++)
            {
                Thread.Sleep(time);
                if (time < 900)
                    time += 100;
                ChangeItem(j);
                //在最后一圈时停止圈后停止
                if (j == int.Parse(result.ToString()))
                {
                    ShowResult(int.Parse(result.ToString()));
                    break;
                }
            }


        }

        private delegate void ResultDelegate(int index);
        public void ShowResult(int index)
        {
            if (this.l_result.InvokeRequired)//不能访问就创建委托
            {
                ListViewDelegate d = new ListViewDelegate(ShowResult);
                l_result.Invoke(d, new object[] { index });
            }
            else
            {
                string shopname=GetItem(index);

                l_result.Text = "抽中是:" + shopname;
            }
        }

        public event EventHandler<EventArgs> GetResult;

        protected void OnGetResult(EventArgs e)
        {
            if (GetResult != null)
                GetResult(this, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (shoplist.SelectedItems.Count > 0)
            {
                int index=shoplist.SelectedItems[0].Index;
                shoplist.Items.RemoveAt(index);
            }
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            flag = true;
            int num = -1;

            if (thinkshop != "")
            {
                for (int i = 0; i < shoplist.Items.Count; i++)
                {
                    string tmp = shoplist.Items[i].Text;
                    if (tmp.Equals(thinkshop))
                    {
                        num = i;
                        break;
                    }
                }
            }
            if(num==-1)
            {
                Random rd = new Random(DateTime.Now.Millisecond);

                num = rd.Next(shoplist.Items.Count);
            }


            new Thread(new ParameterizedThreadStart(Effect)).Start(num);

            btn_start.Enabled = false;
            btn_stop.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            shoplist.Items.Clear();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            btn_start.Enabled = true;
            btn_stop.Enabled = false;
            flag = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            l_time.Text = DateTime.Now.ToLongTimeString();
        }

        public void write(string msg)
        {
            FileStream fs = new FileStream("t.dat", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.Write(msg);
            sw.Close();
            fs.Close();
        }

       
        public void readload()
        {
            if (File.Exists("t.dat"))
            {
                FileStream fs = new FileStream("t.dat", FileMode.Open, FileAccess.Read, FileShare.None);

                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                thinkshop = sr.ReadLine();
                sr.Close();
                fs.Close();
            }
        }
        //保存商家
        public void save()
        {
            FileStream fs = new FileStream("s.dat", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

            for (int i = 0; i < shoplist.Items.Count; i++)
            {
                sw.WriteLine(shoplist.Items[i].Text);
            }

            sw.Close();
            fs.Close();

            MessageBox.Show("保存成功!");
        }
        //加载商家
        public void load()
        {
            this.shoplist.BeginUpdate();
           
            
            if (File.Exists("s.dat"))
            {
                FileStream fs = new FileStream("s.dat", FileMode.Open, FileAccess.Read, FileShare.None);

                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                string line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    //自动加载文件中的商家名
                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = NO_IMAGE;
                    lvi.Text = line;
                    this.shoplist.Items.Add(lvi);
                }
                sr.Close();
                fs.Close();
            }
            this.shoplist.EndUpdate();  //结束数据处理,UI界面一次性绘制.
        }
        private void Form1_Activated(object sender, EventArgs e)
        {

            //注册热键Ctrl+F,Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Ctrl, Keys.F);
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.S);
            HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.Z);

        }

        private void Form1_Leave(object sender, EventArgs e)
        {
            //注销Id号为100的热键设定   
            HotKey.UnregisterHotKey(Handle, 100);
            HotKey.UnregisterHotKey(Handle, 101);
            HotKey.UnregisterHotKey(Handle, 102);

        }
        //重载FromA中的WndProc函数  
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键   
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:
                            fshow();
                            break;
                        case 101:
                            save();
                            break;
                        case 102:
                            delete();
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }
        public void delete()
        {
            thinkshop = "";
            if (File.Exists("t.dat"))
                File.Delete("t.dat");
        }
        public void fshow()
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }

    }
}







转载于:https://my.oschina.net/zhujunxxxxx/blog/337351

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值