[프로그램]

C# 10개의 모양으로 구동하는 버튼 소스

Blackberrys 2023. 6. 26. 20:20
반응형




C#으로 구현된 10개의 모양으로 구동하는 버튼에 대한 예시 코드입니다.
 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ShapeButtons
{
    public partial class MainForm : Form
    {
        private List<Button> shapeButtons;
        private Timer timer;
        private int currentIndex;

        public MainForm()
        {
            InitializeComponent();
            InitializeShapeButtons();
            InitializeTimer();
        }

        private void InitializeShapeButtons()
        {
            shapeButtons = new List<Button>();

            string[] shapes = { "▲", "▼", "◀", "▶", "⬤", "□", "◆", "⬥", "⭐", "❤" };

            for (int i = 0; i < shapes.Length; i++)
            {
                Button button = new Button();
                button.Size = new Size(50, 50);
                button.Location = new Point(i * 60 + 30, 100);
                button.Text = shapes[i];
                button.Click += ShapeButton_Click;
                shapeButtons.Add(button);
                Controls.Add(button);
            }
        }

        private void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = 500;
            timer.Tick += Timer_Tick;
        }

        private void ShapeButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            int shapeIndex = shapeButtons.IndexOf(button);

            if (shapeButtons[shapeIndex].BackColor == Color.Red)
            {
                shapeButtons[shapeIndex].BackColor = DefaultBackColor;
                timer.Stop();
            }
            else
            {
                shapeButtons[shapeIndex].BackColor = Color.Red;
                currentIndex = shapeIndex;
                timer.Start();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            shapeButtons[currentIndex].BackColor = DefaultBackColor;

            currentIndex = (currentIndex + 1) % shapeButtons.Count;
            shapeButtons[currentIndex].BackColor = Color.Red;
        }
    }
}

위의 예시 코드는 Windows Forms 애플리케이션에서 10개의 모양 버튼을 생성합니다. 각 버튼을 클릭하면 해당 버튼의 색상이 빨간색으로 변경되고, 타이머가 시작됩니다. 타이머는 0.5초마다 현재 빨간색인 버튼을 해제하고 다음 버튼을 빨간색으로 변경합니다. 버튼의 모양은 예시로서 10개의 다양한 모양으로 설정되어 있습니다.
주의: 위의 예시 코드는 단순한 예시를 위해 기본적인 구동 버튼을 구현한 것이며, 실제 애플리케이션에 적용하려면 해당 애플리케이션의 UI 구성과 요구사항에 맞게 코드를 수정해야 합니다.감사합니다.