同时点击两个按钮使线条消失或变色,之后使几个线条依次变色这种代码大概咋写。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ButtonClickDemo
{
public partial class Form1 : Form
{
private bool button1Clicked = false;
private bool button2Clicked = false;
private int currentLineIndex = 0;
private Color[] lineColors = { Color.Red, Color.Green, Color.Blue };
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1Clicked = true;
if (button2Clicked)
{
// 同时点击了两个按钮,隐藏或变色线条
HideOrColorLine();
}
}
private void button2_Click(object sender, EventArgs e)
{
button2Clicked = true;
if (button1Clicked)
{
// 同时点击了两个按钮,隐藏或变色线条
HideOrColorLine();
}
}
private void HideOrColorLine()
{
if (currentLineIndex < panel1.Controls.Count)
{
// 隐藏或变色当前线条
Control line = panel1.Controls[currentLineIndex];
if (line.Visible)
{
line.Visible = false;
}
else
{
line.BackColor = lineColors[currentLineIndex % lineColors.Length];
}
// 移动到下一个线条
currentLineIndex++;
// 延迟一段时间后再次调用自己,实现依次变色的效果
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += (sender, e) =>
{
timer.Stop();
HideOrColorLine();
};
timer.Start();
}
else
{
// 所有线条都处理完毕,重置状态
button1Clicked = false;
button2Clicked = false;
currentLineIndex = 0;
}
}
}
}
这个你可以定义一个成员变量,点击按钮,改变它的值
而窗体的paint根据这个变量,用特定的颜色在窗口的 Graphic里用 LineTo MoveTo划线