C# winfrom 超详细UI创建过程 实现双色球选号器UI界面设计过程

public FrmMain() { InitializeComponent(); //初始化红色球和蓝色球 InitRedPanel(); InitBluePanel(); } //创建两色球标签控件集合,为了方便后面用户选择等相关操作,省的每次都去访问panel集合 // key=球的数字 value=球对象 private Dictionary<string, Label> redBallLables = new Dictionary<string, Label>(); private Dictionary<string, Label> blueBallLables = new Dictionary<string, Label>(); #region 拖动窗体的实现 private Point mouseOff;//鼠标移动位置变量 private bool leftFlag;//标签是否为左键 private void FrmMain_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //得到变量的值 leftFlag = true; //点击左键按下时标注为true; } } private void FrmMain_MouseMove(object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 Location = mouseSet; } } private void FrmMain_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//释放鼠标后标注为false; } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } #endregion #region 红色球区域初始化 private void InitRedPanel() { for (int i = 1; i <= 33; i++) //下面的属性是我们直接在UI中添加控件后,生成的,复制过来的 { //这些是不变的属性 Label lbl = new Label(); lbl.Cursor = System.Windows.Forms.Cursors.Hand; lbl.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); lbl.ForeColor = System.Drawing.Color.DimGray; lbl.Image = Image.FromFile("images/gray.png"); lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; lbl.Size = new System.Drawing.Size(48, 45); //这些是变化的属性 lbl.Name = "lblnum" + i; lbl.Text = i < 10 ? "0" + i : i.ToString(); //如果数字小于10,自动补齐0 lbl.Tag = "0";// 默认0 表示灰色 1 表示红色,为了后面切换背景使用 (实际开发中,根据需要也可以存储数据) //坐标需要单独计算(分3组) if (i <= 11) //第1排 { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1), 13); } else if (i >= 12 && i <= 22) { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1 - 11), 58); // Y 增加了45,这个是我们通过观察后台代码得到的数字 } else if (i >= 23) { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1 - 22), 103); } //lbl.Location = new System.Drawing.Point(13, 13); //这个是第一个红色球的原始坐标 //添加到面板集合中 this.panelRed.Controls.Add(lbl); //当你写到这个地方的时候,可以把UI中自己添加的全部删掉 //增加一个单击事件,实现有颜色切换 lbl.Click += new EventHandler(lblRed_Click); //添加到红色球集合中 redBallLables.Add(lbl.Text, lbl); } } #endregion #region 蓝色球区域初始化 private void InitBluePanel() { for (int i = 1; i <= 16; i++) { //这些是不变的属性 Label lbl = new Label(); lbl.Cursor = System.Windows.Forms.Cursors.Hand; lbl.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); lbl.ForeColor = System.Drawing.Color.DimGray; lbl.Image = Image.FromFile("images/gray.png"); lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; lbl.Size = new System.Drawing.Size(48, 45); //这些是变化的属性 lbl.Name = "lblnum" + i; lbl.Text = i < 10 ? "0" + i : i.ToString(); //如果数字小于10,自动补齐0 lbl.Tag = "0";// 默认0 表示灰色 1 表示蓝色,为了后面切换背景使用 //坐标需要单独计算(分3组) if (i <= 6) //第1排 { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1), 13); } else if (i >= 7 && i <= 12) { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1 - 6), 58); // Y 增加了45,这个是我们通过观察后台代码得到的数字 } else if (i >= 13) { lbl.Location = new System.Drawing.Point(13 + 48 * (i - 1 - 12), 103); } //添加到面板集合中 this.panelBlue.Controls.Add(lbl); //增加一个单击事件,实现有颜色切换 lbl.Click += new EventHandler(lblBlue_Click); //添加到蓝色球集合中 blueBallLables.Add(lbl.Text, lbl); } } #endregion #region 单击球变色,演示复位 //红色球单击 private void lblRed_Click(object sender, EventArgs e) { Label lbl = (Label)sender; if (lbl.Tag.ToString().Equals("0"))//如果这个球是灰色的 { SetRedOrBlue(lbl, "red"); } else { SetGray(lbl); } ShowRedCount();//显示红色球的总数 } //蓝色球单击 private void lblBlue_Click(object sender, EventArgs e) { Label lbl = (Label)sender; if (lbl.Tag.ToString().Equals("0")) { SetRedOrBlue(lbl, "blue"); } else { SetGray(lbl); } ShowBlueCount(); //显示蓝色球选择的总数 } //设置成红色或蓝色 private void SetRedOrBlue(Label lbl, string color) { if (lbl.Tag.ToString().Equals("0"))//如果这个球是灰色的 { lbl.Image = Image.FromFile($"images/{color}.png"); lbl.Tag = "1"; lbl.ForeColor = System.Drawing.Color.White; } } //统一设置成灰色 private void SetGray(Label lbl) { lbl.Image = Image.FromFile("images/gray.png"); lbl.Tag = "0"; lbl.ForeColor = System.Drawing.Color.DimGray; } #endregion #region 显示红色球和蓝色球的选择总数 //显示红色球的总数 private void ShowRedCount() { int redCount = 0; foreach (var item in this.redBallLables.Values) { if (item.Tag.ToString().Equals("1")) redCount++; } this.lblRedCount.Text = redCount.ToString(); } //显示蓝色球的总数 private void ShowBlueCount() { int blueCount = 0; foreach (var item in this.blueBallLables.Values) { if (item.Tag.ToString().Equals("1")) blueCount++; } this.lblBlueCount.Text = blueCount.ToString(); } #endregion #region 清空按钮 //清空红色球选择 private void btnResetRed_Click(object sender, EventArgs e) { foreach (Label item in this.redBallLables.Values) { SetGray(item); } this.lblRedCount.Text = "0"; } //清空蓝色球选择 private void btnResetBlue_Click(object sender, EventArgs e) { foreach (Label item in this.blueBallLables.Values) { SetGray(item); } this.lblBlueCount.Text = "0"; } #endregion
上一篇:lua整合redis


下一篇:链表中LinkList L与LinkList *L( & * L.elem L->elem)