效果圖:
主要代碼:
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 } }
修改為可作弊的抽獎小程序:

//修改代碼:
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; }
txt內容:
App.config內容: