Winform 随机抽奖小程序

效果图:

主要代码:

Form1.cs

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        static string path = "抽奖.txt";
        string[] content = File.ReadAllLines(path);

        public Form1()
        {
            InitializeComponent();

            //控制名称变化时闪烁问题
            this.DoubleBuffered = true;//设置本窗体
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize);
            X = this.Width;
            Y = this.Height;
            setTag(this);
            Form1_Resize(new object(), new EventArgs());

            loadBackImage();
            //this.timer1.Interval = 2000;

            //设置按钮背景透明、无边框
            this.btn_BeginEnd.BackColor = Color.Transparent;
            this.btn_BeginEnd.FlatStyle = FlatStyle.Flat;
            this.btn_BeginEnd.FlatAppearance.BorderSize = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                Random r = new Random();
                int i = r.Next(0, content.Length);
                label1.Text = content[i];
            }
            catch (Exception ex)
            {
                timer1.Stop();
                MessageBox.Show(ex.Message);
                btn_BeginEnd.Text = "开始"; 
                return;
            }
        }

        private void btn_BeginEnd_Click(object sender, EventArgs e)
        {
            if (btn_BeginEnd.Text == "开始")
            {
                timer1.Start();
                btn_BeginEnd.Text = "抽奖中..."; 
            }
            else
            {
                timer1.Stop();
                btn_BeginEnd.Text = "开始"; 
            }
        }

        /// <summary>
        /// 背景图自适应窗体
        /// </summary>
        private void loadBackImage()
        {
            string fbImage = "背景.jpg";
            var bm = new Bitmap(fbImage); //fbImage图片路径
            this.BackgroundImage = bm;//设置背景图片
            this.BackgroundImageLayout = ImageLayout.Stretch;//设置背景图片自动适应
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            loadBackImage();
        }

        #region 控件跟随窗体自适应大小
        private float X;
        private float Y;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        } 
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }
        }

        //双缓冲,控制窗体改变大小时闪烁问题
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private const int WM_SETREDRAW = 0xB;
        private void Form1_Resize(object sender, EventArgs e)
        {
            SendMessage(this.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
            float newx = (this.Width) / X;
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            SendMessage(this.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
            this.Invalidate(true);
        }

        /// <summary>
        /// 设置label自适应窗体居中显示
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            int x = (int)(0.5 * (this.Width - label1.Width));
            int y = label1.Location.Y;
            label1.Location = new System.Drawing.Point(x, y);
        }
        #endregion

        #region 监听鼠标状态,改变按钮样式
        private void btn_BeginEnd_MouseLeave(object sender, EventArgs e)
        {
            this.btn_BeginEnd.ForeColor = Color.White;
            this.btn_BeginEnd.Font = new Font(btn_BeginEnd.Font, FontStyle.Regular); 
        }

        private void btn_BeginEnd_MouseMove(object sender, MouseEventArgs e)
        {
            this.btn_BeginEnd.FlatAppearance.MouseOverBackColor = Color.Transparent;
            this.btn_BeginEnd.Font = new Font(btn_BeginEnd.Font, FontStyle.Bold); 
        }

        private void btn_BeginEnd_MouseClick(object sender, MouseEventArgs e)
        {
            this.btn_BeginEnd.FlatAppearance.MouseDownBackColor = Color.Transparent; 
        }

        private void btn_BeginEnd_MouseDown(object sender, MouseEventArgs e)
        {
            this.btn_BeginEnd.FlatAppearance.MouseDownBackColor = Color.Transparent; 
        }
        #endregion
    }
}
View Code

修改为可作弊的抽奖小程序:

//修改代码:

private void btn_BeginEnd_Click(object sender, EventArgs e)
        {
            if (btn_BeginEnd.Text == "开始")
            {
                timer1.Start();
                btn_BeginEnd.Text = "抽奖中...";
                //this.txt_MaxValue.Enabled = false;
            }
            else
            {
                //timer1.Stop();
                //btn_BeginEnd.Text = "开始";
                //this.txt_MaxValue.Enabled = true;
            }
        }

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.D1 || keyData == Keys.NumPad1)
            { 
                timer1.Stop();
                label1.Text = ConfigurationManager.AppSettings["First"];
                btn_BeginEnd.Text = "开始";
            }
            if (keyData == Keys.D2 || keyData == Keys.NumPad2)
            {
                timer1.Stop();
                label1.Text = ConfigurationManager.AppSettings["Second"];
                btn_BeginEnd.Text = "开始";
            }
            if (keyData == Keys.D3 || keyData == Keys.NumPad3)
            {
                timer1.Stop();
                label1.Text = ConfigurationManager.AppSettings["Third"];
                btn_BeginEnd.Text = "开始";
            } 
            return true;
        }
View Code

txt内容:

 App.config内容:

 

 

 

 

原文地址:https://www.cnblogs.com/zhaoyl9/p/11983212.html