💻

即席でGUIアプリを作った話

2025/03/11に公開

部内ロボコン用にスコア、タイマー表示のアプリを作る

即席でなんとかする

  • OBSの画面にWebアプリをいくつか並べてなんとかする

見栄えが悪い→ネイティブアプリを作ろう

何を使う

  • C++ Win32API
    • 使ったことはあるが書きにくく面倒
    • あくまで画面表示とUIがあれば良い
      → UIの実装が面倒
  • .NET Framework
    • 使ったことはない
    • UIの作成がやりやすい
    • コーディングが少なく済む

→ .NET Frameworkで開発

.NET Frameworkで開発

環境

  • Visual Studio 2022 Community
  • Windows Form Application

UIを実装

GUIでUIを配置
それ以外は何もない

機能実装

ボタンやテキストボックス、タイマーのイベントに応じた処理を書く

namespace Robocon_LCD
{
    public partial class Form1 : Form
    {
        private int Red_Score = 0;
        private int Blue_Score = 0;
        private bool Timer_Started = false;
        private int Timer_Second = 150;
        private int Timer_Set_Second = 0;
        private int CountDown_Second = 4;
        private bool CountDown_Started = false;
        public Form1()
        {
            InitializeComponent();

        }

        private void Red_Team_Name_Box_TextChanged(object sender, EventArgs e)
        {
            Red_Team_Name.Text = Red_Team_Name_Box.Text;
        }
        private void Blue_Team_Name_Box_TextChanged(object sender, EventArgs e)
        {
            Blue_Team_Name.Text = Blue_Team_Name_Box.Text;
        }

        private void red_score_plus_Click(object sender, EventArgs e)
        {
            Red_Score++;
            Red_Team_Score.Text = Red_Score.ToString();
        }

        private void Blue_Point_Plus_Click(object sender, EventArgs e)
        {
            Blue_Score++;
            Blue_Team_Score.Text = Blue_Score.ToString();
        }

        private void Red_Point_Minus_Click(object sender, EventArgs e)
        {
            Red_Score--;
            Red_Team_Score.Text = Red_Score.ToString();
        }

        private void Blue_Point_Minus_Click(object sender, EventArgs e)
        {
            Blue_Score--;
            Blue_Team_Score.Text = Blue_Score.ToString();
        }

        private void Timed_Tick(object sender, EventArgs e)
        {
            if (Timer_Second <= 1)
            {
                Timer_Started = false;
                Visual_Timer.Text = "TIME UP";
            }
            else
            if (Timer_Started && Timer_Second > 1)
            {
                Timer_Second--;
                Visual_Timer.Text = (Timer_Second - (Timer_Second % 60)) / 60 + ":" + (Timer_Second % 60);
            }
        }

        private void Timer_Start_Click(object sender, EventArgs e)
        {
            CountDown_Started = true;
        }

        private void Timer_Stop_Click(object sender, EventArgs e)
        {
            Timer_Started = false;
        }

        private void Timer_Reset_Click(object sender, EventArgs e)
        {
            Timer_Started = false;
            Timer_Second = Timer_Set_Second;
            Visual_Timer.Text = (Timer_Second - (Timer_Second % 60)) / 60 + ":" + (Timer_Second % 60);
        }

        private void Timer_Sec_TextChanged(object sender, EventArgs e)
        {
            if (Timer_Sec.Text == "")
            {
                Timer_Set_Second = 0;
                return;
            }
            Timer_Set_Second = int.Parse(Timer_Sec.Text);
        }

        private void Visual_Timer_Click(object sender, EventArgs e)
        {
            return;
        }

        private void CountDown_Tick(object sender, EventArgs e)
        {
            if (CountDown_Started && CountDown_Second > 1)
            {
                CountDown_Second--;
                Visual_Timer.Text = CountDown_Second.ToString();
            }
            else if (CountDown_Second <= 1)
            {
                Visual_Timer.Text = "START";
                CountDown_Started = false;
                Timer_Started = true;
                CountDown_Second = 3;
            }
        }
    }
}

完成

2時間で作成したため粗削りでバグが多いがとりあえず使えなくはない状態まで持っていった

GitHubリポジトリ

なお部内ロボコン終了後に機能追加やバグ修正を行ってまともなものに改善

Discussion