c#制作模拟器有不懂的地方


using System;
using System.Drawing;
using System.Windows.Forms;
namespace _1
{
    partial class Form1
    {
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Blue, 5);//设置笔的粗细为,颜色为蓝色
            Pen x = new Pen(Color.Black, 5);
            Graphics g = this.CreateGraphics();
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//实线
            g.DrawLine(p, 10, 395, 180, 395);//第一段
            g.DrawLine(p, 180, 410, 180, 385);//结
            g.DrawLine(p, 180, 395, 370, 395);//第二段
            g.DrawLine(p, 370, 395, 560, 395);//第三段
            g.DrawLine(p, 370, 410, 370, 385);//结
            g.DrawLine(p, 560, 410, 560, 385);//结
            g.DrawLine(p, 650, 395, 680, 355);//一斜
            g.DrawLine(x, 650, 395, 680, 395);//一横
            g.DrawLine(p, 560, 395, 655, 395);//第四段
            g.DrawLine(p, 680, 395, 750, 395);//下五段
            g.DrawLine(p, 750, 395, 1200, 395);//1G
            g.DrawLine(p, 680, 355, 722, 300);//二斜
            g.DrawLine(p, 720, 300, 750, 300);//上五段
            g.DrawLine(p, 750, 290, 750, 310);//结
            g.DrawLine(p, 750, 385, 750, 410);//结
            g.DrawLine(p, 750, 300, 1200, 300);//2G
            g.DrawLine(p, 1200, 290, 1200, 310);//结
            g.DrawLine(p, 1200, 385, 1200, 410);//结
            g.DrawLine(p, 1200, 300, 1230, 300);//上六段
            g.DrawLine(p, 1200, 395, 1270, 395);//下六段
            g.DrawLine(p, 1228, 300, 1270, 355);//三斜
            g.DrawLine(p, 1270, 355, 1300, 395);//四斜 
            g.DrawLine(x, 1270, 395, 1300, 395);//四横
            g.DrawLine(p, 1296, 395, 1440, 395);//第七段
            g.DrawLine(p, 1440, 395, 1630, 395);//第八段
            g.DrawLine(p, 1630, 395, 1820, 395);//第九段
            g.DrawLine(p, 1820, 395, 1900, 395);//第十段
            g.DrawLine(p, 1440, 385, 1440, 410);//结
            g.DrawLine(p, 1630, 385, 1630, 410);//结
            g.DrawLine(p, 1820, 385, 1820, 410);//结
            g.Dispose();
            p.Dispose();

        }
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            button1 = new Button();
            button2 = new Button();
            button4 = new Button();
            button5 = new Button();
            button3 = new Button();
            button6 = new Button();
            label1 = new Label();
            label2 = new Label();
            label3 = new Label();
            label4 = new Label();
            SuspendLayout();
            // 
            // button1
            // 
            button1.BackColor = Color.Lime;
            button1.ForeColor = Color.Lime;
            button1.Location = new Point(156, 340);
            button1.Name = "button1";
            button1.Size = new Size(25, 27);
            button1.TabIndex = 0;
            button1.Text = "button1";
            button1.UseVisualStyleBackColor = false;
            // 
            // button2
            // 
            button2.BackColor = Color.Lime;
            button2.ForeColor = Color.Lime;
            button2.Location = new Point(770, 425);
            button2.Name = "button2";
            button2.Size = new Size(27, 28);
            button2.TabIndex = 1;
            button2.Text = "button2";
            button2.UseVisualStyleBackColor = false;
            // 
            // button4
            // 
            button4.BackColor = Color.Lime;
            button4.ForeColor = Color.Lime;
            button4.Location = new Point(1165, 357);
            button4.Name = "button4";
            button4.Size = new Size(27, 28);
            button4.TabIndex = 2;
            button4.Text = "button3";
            button4.UseVisualStyleBackColor = false;
            // 
            // button5
            // 
            button5.BackColor = Color.Lime;
            button5.ForeColor = Color.Lime;
            button5.Location = new Point(1165, 253);
            button5.Name = "button5";
            button5.Size = new Size(27, 28);
            button5.TabIndex = 4;
            button5.Text = "button5";
            button5.UseVisualStyleBackColor = false;
            // 
            // button3
            // 
            button3.BackColor = Color.Lime;
            button3.ForeColor = Color.Lime;
            button3.Location = new Point(770, 318);
            button3.Name = "button3";
            button3.Size = new Size(27, 28);
            button3.TabIndex = 5;
            button3.Text = "button6";
            button3.UseVisualStyleBackColor = false;
            // 
            // button6
            // 
            button6.BackColor = Color.Lime;
            button6.ForeColor = Color.Lime;
            button6.Location = new Point(1828, 425);
            button6.Name = "button6";
            button6.Size = new Size(27, 28);
            button6.TabIndex = 6;
            button6.Text = "button7";
            button6.UseVisualStyleBackColor = false;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
            label1.ForeColor = SystemColors.ButtonFace;
            label1.Location = new Point(977, 254);
            label1.Name = "label1";
            label1.Size = new Size(32, 27);
            label1.TabIndex = 7;
            label1.Text = "Ⅰ";
            label1.Click += label1_Click;
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
            label2.ForeColor = SystemColors.ButtonFace;
            label2.Location = new Point(977, 357);
            label2.Name = "label2";
            label2.Size = new Size(32, 27);
            label2.TabIndex = 8;
            label2.Text = "Ⅱ";
            // 
            // label3
            // 
            label3.AutoSize = true;
            label3.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
            label3.ForeColor = SystemColors.ButtonFace;
            label3.Location = new Point(1330, 358);
            label3.Name = "label3";
            label3.Size = new Size(24, 27);
            label3.TabIndex = 9;
            label3.Text = "1";
            // 
            // label4
            // 
            label4.AutoSize = true;
            label4.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
            label4.ForeColor = SystemColors.ButtonFace;
            label4.Location = new Point(619, 357);
            label4.Name = "label4";
            label4.Size = new Size(24, 27);
            label4.TabIndex = 10;
            label4.Text = "2";
            label4.Click += label4_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(9F, 20F);
            AutoScaleMode = AutoScaleMode.Font;
            BackColor = SystemColors.Desktop;
            ClientSize = new Size(1924, 617);
            Controls.Add(label4);
            Controls.Add(label3);
            Controls.Add(label2);
            Controls.Add(label1);
            Controls.Add(button6);
            Controls.Add(button3);
            Controls.Add(button5);
            Controls.Add(button4);
            Controls.Add(button2);
            Controls.Add(button1);
            ForeColor = SystemColors.ControlText;
            Name = "Form1";
            Text = "Form1";
            Paint += Form1_Paint;
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Button button1;
        private Button button2;
        private Button button4;
        private Button button5;
        private Button button3;
        private Button button6;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
    }
}

img

怎么做到同时按蓝色1,2标记的按钮,使白色标记的7号蓝线消失,3号黑线变蓝,然后1号、2和3和4号,5、6,8依次变白

可以给Form1添加keydown和keyup事件,keydown判断按了ctrl来实现同时选择功能。用计时器依次描白线段,代码如下


using System;
using System.Drawing;
using System.Windows.Forms;
namespace demo
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private Button button1;
        private Button button2;
        private Button button4;
        private Button button5;
        private Button button3;
        private Button button6;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.Color.Lime;
            this.button1.ForeColor = System.Drawing.Color.Lime;
            this.button1.Location = new System.Drawing.Point(202, 369);
            this.button1.Margin = new System.Windows.Forms.Padding(2);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(17, 16);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = false;
            // 
            // button2
            // 
            this.button2.BackColor = System.Drawing.Color.Lime;
            this.button2.ForeColor = System.Drawing.Color.Lime;
            this.button2.Location = new System.Drawing.Point(611, 420);
            this.button2.Margin = new System.Windows.Forms.Padding(2);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(18, 17);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = false;
            // 
            // button4
            // 
            this.button4.BackColor = System.Drawing.Color.Lime;
            this.button4.ForeColor = System.Drawing.Color.Lime;
            this.button4.Location = new System.Drawing.Point(875, 379);
            this.button4.Margin = new System.Windows.Forms.Padding(2);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(18, 17);
            this.button4.TabIndex = 2;
            this.button4.Text = "button3";
            this.button4.UseVisualStyleBackColor = false;
            // 
            // button5
            // 
            this.button5.BackColor = System.Drawing.Color.Lime;
            this.button5.ForeColor = System.Drawing.Color.Lime;
            this.button5.Location = new System.Drawing.Point(875, 317);
            this.button5.Margin = new System.Windows.Forms.Padding(2);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(18, 17);
            this.button5.TabIndex = 4;
            this.button5.Text = "button5";
            this.button5.UseVisualStyleBackColor = false;
            // 
            // button3
            // 
            this.button3.BackColor = System.Drawing.Color.Lime;
            this.button3.ForeColor = System.Drawing.Color.Lime;
            this.button3.Location = new System.Drawing.Point(611, 356);
            this.button3.Margin = new System.Windows.Forms.Padding(2);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(18, 17);
            this.button3.TabIndex = 5;
            this.button3.Text = "button6";
            this.button3.UseVisualStyleBackColor = false;
            // 
            // button6
            // 
            this.button6.BackColor = System.Drawing.Color.Lime;
            this.button6.ForeColor = System.Drawing.Color.Lime;
            this.button6.Location = new System.Drawing.Point(1317, 420);
            this.button6.Margin = new System.Windows.Forms.Padding(2);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(18, 17);
            this.button6.TabIndex = 6;
            this.button6.Text = "button7";
            this.button6.UseVisualStyleBackColor = false;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F);
            this.label1.ForeColor = System.Drawing.SystemColors.ButtonFace;
            this.label1.Location = new System.Drawing.Point(749, 317);
            this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(26, 21);
            this.label1.TabIndex = 7;
            this.label1.Text = "Ⅰ";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F);
            this.label2.ForeColor = System.Drawing.SystemColors.ButtonFace;
            this.label2.Location = new System.Drawing.Point(749, 379);
            this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(26, 21);
            this.label2.TabIndex = 8;
            this.label2.Text = "Ⅱ";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F);
            this.label3.ForeColor = System.Drawing.SystemColors.ButtonFace;
            this.label3.Location = new System.Drawing.Point(985, 380);
            this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(19, 21);
            this.label3.TabIndex = 9;
            this.label3.Text = "1";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F);
            this.label4.ForeColor = System.Drawing.SystemColors.ButtonFace;
            this.label4.Location = new System.Drawing.Point(511, 379);
            this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(19, 21);
            this.label4.TabIndex = 10;
            this.label4.Text = "2";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Desktop;
            this.ClientSize = new System.Drawing.Size(1784, 861);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Margin = new System.Windows.Forms.Padding(2);
            this.Name = "Form1";
            this.Text = "Form1";

            this.ResumeLayout(false);
            this.PerformLayout();



            this.KeyPreview = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
            this.button1.Click += Button1_2_Click;
            this.button2.Click += Button1_2_Click;

        }


        Timer t;//用计时器依次变白色
        public Form1()
        {
            t = new Timer();
            t.Interval = 500;//依次变白间隔时间,单位ms
            t.Tick += T_Tick;
            InitializeComponent();
        }

        private void T_Tick(object sender, EventArgs e)
        {
            lineIndex++;//
            Form1_Paint(null, null);
            if (lineIndex >= 8) t.Enabled = false;//全部变白后停止计时器
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            var ctrl12True = ctrlKey && clickButton1 && clickButton2;
           
            Pen p = new Pen(Color.Blue, 5);//设置笔的粗细为,颜色为蓝色
            Pen x = new Pen(Color.Black, 5);
            Pen white = new Pen(Color.White, 5);
            Pen black = new Pen(Color.Black, 5);

            Graphics g = this.CreateGraphics();
 
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//实线
            g.DrawLine(ctrl12True && lineIndex > 5 ? white : p, 10, 395, 180, 395);//第一段
            g.DrawLine(p, 180, 410, 180, 385);//结
            g.DrawLine(ctrl12True && lineIndex > 4 ? white : p, 180, 395, 370, 395);//第二段
            g.DrawLine(ctrl12True && lineIndex > 3 ? white : p, 370, 395, 560, 395);//第三段
            g.DrawLine(p, 370, 410, 370, 385);//结
            g.DrawLine(p, 560, 410, 560, 385);//结
            g.DrawLine(ctrl12True ? black : p, 650, 395, 680, 355);//一斜  7
            g.DrawLine(ctrl12True ? (lineIndex >1?white : p): x, 650, 395, 680, 395);//一横     //3
            g.DrawLine(ctrl12True && lineIndex > 2 ? white : p, 560, 395, 655, 395);//第四段 3
            g.DrawLine(ctrl12True && lineIndex > 0 ? white : p, 680, 395, 750, 395);//下五段 2
            g.DrawLine(ctrl12True&&lineIndex>-1 ? white : p, 750, 395, 1200, 395);//1G  1
            g.DrawLine(p, 680, 355, 722, 300);//二斜
            g.DrawLine(p, 720, 300, 750, 300);//上五段
            g.DrawLine(p, 750, 290, 750, 310);//结
            g.DrawLine(p, 750, 385, 750, 410);//结
            g.DrawLine(p, 750, 300, 1200, 300);//2G
            g.DrawLine(p, 1200, 290, 1200, 310);//结
            g.DrawLine(p, 1200, 385, 1200, 410);//结
            g.DrawLine(p, 1200, 300, 1230, 300);//上六段
            g.DrawLine(p, 1200, 395, 1270, 395);//下六段
            g.DrawLine(p, 1228, 300, 1270, 355);//三斜
            g.DrawLine(p, 1270, 355, 1300, 395);//四斜 
            g.DrawLine(x, 1270, 395, 1300, 395);//四横
            g.DrawLine(p, 1296, 395, 1440, 395);//第七段
            g.DrawLine(p, 1440, 395, 1630, 395);//第八段
            g.DrawLine(p, 1630, 395, 1820, 395);//第九段
            g.DrawLine(p, 1820, 395, 1900, 395);//第十段
            g.DrawLine(p, 1440, 385, 1440, 410);//结
            g.DrawLine(p, 1630, 385, 1630, 410);//结
            g.DrawLine(p, 1820, 385, 1820, 410);//结
            g.Dispose();
            p.Dispose();
        }


        #region Windows Form Designer generated code


        #endregion
        bool ctrlKey = false, clickButton1 = false, clickButton2 = false;
        int lineIndex = -1;

        private void Button1_2_Click(object sender, EventArgs e)
        {
            if (ctrlKey)
            {
                var btn = (Button)sender;
                if (btn.Name == "button1") clickButton1 = true;
                if (btn.Name == "button2") clickButton2 = true;
                if (clickButton1 && clickButton2)
                {//安装了ctrl,点击过按钮1和2启动计时器变白色
                    t.Enabled = true;
                }
            }

        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            ctrlKey = e.Control;
        }
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            ctrlKey = e.Control;
            if (!ctrlKey)
            {
                clickButton1 = clickButton2 = false;
                Form1_Paint(null, null);
                lineIndex = -1;
                t.Enabled = false;
            }
        }

    }
}



同时按键,这个可以使用checkbox,它可以设置成类似button的外观,达到切换按键的目的

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632