언어 자료구조 알고리즘/프로그래밍 실습

[C# 실습] 레코드 코팅 - 기계 구현(Windows Forms)

언제나휴일 2020. 10. 7. 12:08
반응형

이전 게시글에는 레코드 코팅 기계 컨트롤(RCMachineControl)을 만드는 코드를 소개했어요.

해당 글에서는 레코드 코팅 기계(RCMachine)개체를 Form1 Load시점에 하드코딩으로 만들어 컨트롤에 설정하고 가동하게 작성하였습니다.

 

이번에는 UI 컨트롤을 통해 레코드 코팅 기계 컨트롤을 제어하는 코드를 소개할게요.

1. Form1에 제어하기 위한 컨트롤을 추가 배치합니다.

nud로 시작하는 컨트롤은 NumericUpdawn컨트롤입니다.

Form1 자식 컨트롤 배치

2. Form의 Load이벤트 핸들러와 각 버튼의 클릭 이벤트 핸들러를 추가한 후에 다음처럼 구현합니다.

using Microsoft.Win32;
using RCMachineControlLib;
using System;
using System.Windows.Forms;
using 콘솔_예광탄;

namespace RCManchine_공장_예광탄
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            rccon.OnEnd += Rccon_OnEnd;
        }

        private void Rccon_OnEnd(object sender, RCMachineControlLib.EndCotingEventArgs e)
        {
            //인자가 있는 메서드의 크로스 스레드 해결방법
            if (this.InvokeRequired)
            {
                object[] objs = new object[] { sender, e };//전달할 인자를 object 배열 개체로 만듦
                EndCotingEventHandler dele = Rccon_OnEnd;
                this.Invoke(dele, objs);//인자 배열을 두 번째 인자로 전달
            }
            else
            {
                btn_stop.Enabled = false;
                btn_start.Enabled = btn_basic.Enabled = true;
            }
        }

        RCMachine rc;
        private void btn_basic_Click(object sender, EventArgs e)
        {
            if (rc == null)
            {
                rc = new RCMachine();
                nud_source.Enabled = nud_liq.Enabled = true;
                btn_source.Enabled = btn_liq.Enabled = true;
                btn_start.Enabled = true;
                rccon.RCMachine = rc;
            }
            rccon.SetArea(nud_area.Value);
            rccon.SetRadius(nud_radius.Value);
            rccon.SetSpeed(nud_speed.Value);
        }

        private void btn_source_Click(object sender, EventArgs e)
        {
            rccon.AddSource(nud_source.Value);
        }

        private void btn_liq_Click(object sender, EventArgs e)
        {
            rccon.AddLiq(nud_liq.Value);
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            btn_basic.Enabled = false;
            rc.Start();
            if(btn_start.Enabled == true)
            {
                MessageBox.Show("가동 조건에 맞지 않습니다.");
            }
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            rc.Stop();
            btn_stop.Enabled = false;
            btn_start.Enabled = btn_basic.Enabled = true;
        }
    }
}

*다음은 Form1.Designer.cs 코드입니다.

namespace RCManchine_공장_예광탄
{
    partial class Form1
    {
        /// <summary>
        /// 필수 디자이너 변수입니다.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 사용 중인 모든 리소스를 정리합니다.
        /// </summary>
        /// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 디자이너에서 생성한 코드

        /// <summary>
        /// 디자이너 지원에 필요한 메서드입니다. 
        /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
        /// </summary>
        private void InitializeComponent()
        {
            this.rccon = new RCManchine_공장_예광탄.RCMachineControl();
            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.label5 = new System.Windows.Forms.Label();
            this.btn_start = new System.Windows.Forms.Button();
            this.btn_stop = new System.Windows.Forms.Button();
            this.nud_area = new System.Windows.Forms.NumericUpDown();
            this.nud_radius = new System.Windows.Forms.NumericUpDown();
            this.nud_speed = new System.Windows.Forms.NumericUpDown();
            this.nud_source = new System.Windows.Forms.NumericUpDown();
            this.nud_liq = new System.Windows.Forms.NumericUpDown();
            this.btn_basic = new System.Windows.Forms.Button();
            this.btn_source = new System.Windows.Forms.Button();
            this.btn_liq = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.nud_area)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_radius)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_speed)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_source)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_liq)).BeginInit();
            this.SuspendLayout();
            // 
            // rccon
            // 
            this.rccon.Font = new System.Drawing.Font("굴림", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
            this.rccon.Location = new System.Drawing.Point(403, 0);
            this.rccon.Margin = new System.Windows.Forms.Padding(5);
            this.rccon.Name = "rccon";
            this.rccon.RCMachine = null;
            this.rccon.Size = new System.Drawing.Size(597, 456);
            this.rccon.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(129, 21);
            this.label1.TabIndex = 1;
            this.label1.Text = "레코드 면적:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(17, 75);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(129, 21);
            this.label2.TabIndex = 2;
            this.label2.Text = "투입구 반경:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(21, 126);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(108, 21);
            this.label3.TabIndex = 3;
            this.label3.Text = "회전 속도:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(17, 216);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(129, 21);
            this.label4.TabIndex = 4;
            this.label4.Text = "레코드 투입:";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(17, 268);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(136, 21);
            this.label5.TabIndex = 5;
            this.label5.Text = "코팅 액 투입:";
            // 
            // btn_start
            // 
            this.btn_start.Enabled = false;
            this.btn_start.Location = new System.Drawing.Point(25, 316);
            this.btn_start.Name = "btn_start";
            this.btn_start.Size = new System.Drawing.Size(85, 36);
            this.btn_start.TabIndex = 6;
            this.btn_start.Text = "가동";
            this.btn_start.UseVisualStyleBackColor = true;
            this.btn_start.Click += new System.EventHandler(this.btn_start_Click);
            // 
            // btn_stop
            // 
            this.btn_stop.Enabled = false;
            this.btn_stop.Location = new System.Drawing.Point(116, 316);
            this.btn_stop.Name = "btn_stop";
            this.btn_stop.Size = new System.Drawing.Size(83, 36);
            this.btn_stop.TabIndex = 7;
            this.btn_stop.Text = "멈춤";
            this.btn_stop.UseVisualStyleBackColor = true;
            this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click);
            // 
            // nud_area
            // 
            this.nud_area.Location = new System.Drawing.Point(149, 29);
            this.nud_area.Name = "nud_area";
            this.nud_area.Size = new System.Drawing.Size(120, 32);
            this.nud_area.TabIndex = 8;
            // 
            // nud_radius
            // 
            this.nud_radius.Location = new System.Drawing.Point(149, 73);
            this.nud_radius.Name = "nud_radius";
            this.nud_radius.Size = new System.Drawing.Size(120, 32);
            this.nud_radius.TabIndex = 9;
            // 
            // nud_speed
            // 
            this.nud_speed.Location = new System.Drawing.Point(149, 124);
            this.nud_speed.Name = "nud_speed";
            this.nud_speed.Size = new System.Drawing.Size(120, 32);
            this.nud_speed.TabIndex = 10;
            // 
            // nud_source
            // 
            this.nud_source.Enabled = false;
            this.nud_source.Location = new System.Drawing.Point(149, 214);
            this.nud_source.Name = "nud_source";
            this.nud_source.Size = new System.Drawing.Size(120, 32);
            this.nud_source.TabIndex = 11;
            // 
            // nud_liq
            // 
            this.nud_liq.Enabled = false;
            this.nud_liq.Location = new System.Drawing.Point(149, 266);
            this.nud_liq.Name = "nud_liq";
            this.nud_liq.Size = new System.Drawing.Size(120, 32);
            this.nud_liq.TabIndex = 12;
            // 
            // btn_basic
            // 
            this.btn_basic.Location = new System.Drawing.Point(285, 30);
            this.btn_basic.Name = "btn_basic";
            this.btn_basic.Size = new System.Drawing.Size(88, 126);
            this.btn_basic.TabIndex = 13;
            this.btn_basic.Text = "설정";
            this.btn_basic.UseVisualStyleBackColor = true;
            this.btn_basic.Click += new System.EventHandler(this.btn_basic_Click);
            // 
            // btn_source
            // 
            this.btn_source.Enabled = false;
            this.btn_source.Location = new System.Drawing.Point(285, 214);
            this.btn_source.Name = "btn_source";
            this.btn_source.Size = new System.Drawing.Size(88, 32);
            this.btn_source.TabIndex = 14;
            this.btn_source.Text = "추가";
            this.btn_source.UseVisualStyleBackColor = true;
            this.btn_source.Click += new System.EventHandler(this.btn_source_Click);
            // 
            // btn_liq
            // 
            this.btn_liq.Enabled = false;
            this.btn_liq.Location = new System.Drawing.Point(285, 265);
            this.btn_liq.Name = "btn_liq";
            this.btn_liq.Size = new System.Drawing.Size(88, 33);
            this.btn_liq.TabIndex = 15;
            this.btn_liq.Text = "투입";
            this.btn_liq.UseVisualStyleBackColor = true;
            this.btn_liq.Click += new System.EventHandler(this.btn_liq_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 21F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1029, 467);
            this.Controls.Add(this.btn_liq);
            this.Controls.Add(this.btn_source);
            this.Controls.Add(this.btn_basic);
            this.Controls.Add(this.nud_liq);
            this.Controls.Add(this.nud_source);
            this.Controls.Add(this.nud_speed);
            this.Controls.Add(this.nud_radius);
            this.Controls.Add(this.nud_area);
            this.Controls.Add(this.btn_stop);
            this.Controls.Add(this.btn_start);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.rccon);
            this.Font = new System.Drawing.Font("굴림", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
            this.Margin = new System.Windows.Forms.Padding(5);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.nud_area)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_radius)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_speed)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_source)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nud_liq)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private RCMachineControl rccon;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button btn_start;
        private System.Windows.Forms.Button btn_stop;
        private System.Windows.Forms.NumericUpDown nud_area;
        private System.Windows.Forms.NumericUpDown nud_radius;
        private System.Windows.Forms.NumericUpDown nud_speed;
        private System.Windows.Forms.NumericUpDown nud_source;
        private System.Windows.Forms.NumericUpDown nud_liq;
        private System.Windows.Forms.Button btn_basic;
        private System.Windows.Forms.Button btn_source;
        private System.Windows.Forms.Button btn_liq;
    }
}

반응형