Today
Total
Recent Posts
Recent Comments
Archives
05-19 03:21
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
반응형
관리 메뉴

UD_park's IT story

[C# Winform] Windows Forms 기반 프로그래밍 기초 - ② 본문

IT - Language/C# Windows Forms

[C# Winform] Windows Forms 기반 프로그래밍 기초 - ②

kingllzn 2018. 7. 12. 11:58
반응형
SMALL


[이 포스팅은

예제로 배우는 C# 프로그래밍의

http://www.csharpstudy.com/

내용을 발췌하였습니다]

 

[보기용 예제 소스 코드에서 오류 발생시에

댓글이나 방명록에 글을 남겨 주시면 수정/보완하겠습니다]

 

 

이번에는 지난 포스팅에 이어서

Windows Forms 기반으로 작성하는

기초적인 프로그래밍 두 번째를

포스팅해보도록 하겠습니다

 

 

일단 시작은 Visual C#에서

Windows Forms 앱을 선택하고

프로젝트 이름을 정해주신뒤 확인을

눌러 프로젝트를 생성해 주시기 바랍니다

 

 

그럼 위의 사진과 같이 기본적인

윈폼 기본 화면이 출력되고

생성되어져 나옵니다

 

기본적인 윈폼의 구성에 대한

설명을 위해서 Program.cs로 이동해

주시기 바랍니다

 

이동하시면 아래와 같은 소스가

출력되어져 나옵니다

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormTextBox1
{
    static class Program
    {
        /// 
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

여기서 메인 함수 부분부터

천천히 살펴보도록 하겠습니다

 

일단 메인에서는 Form1 클래스를 객체로

생성하고, 그 객체를 19번 줄처럼

Application.Run()의 안에 파라미터로 넣고

실행합니다

 

Application.Run()의 역할은

Form이라는 Window 창 객체를

화면에 보여주고, 메시지를 만들어

마우스와 키보드 등의 입력 수단을

통해 14번 줄의 UI 스레드에

전달하는 기능을 하고있습니다

 

쉽게 이야기하자면

기본적인 창을 출력하고

그 창에서 작동되는 마우스나

키보드의 동작을 모두 읽어와

출력하도록 만들어 주는

역할을 하고있습니다

 

 

그리고 실제 폼 UI는 Form1.cs 파일과

Form1.Designer.cs 파일의 정의를

쉽게 설명하면

 

 

// Form1.Designer.cs 파일

namespace WinFormTextBox1
{
    partial class Form1
    {
        /// 
        /// 필수 디자이너 변수입니다.
        /// 
        private System.ComponentModel.IContainer components = null;

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

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

        /// 
        /// 디자이너 지원에 필요한 메서드입니다. 
        /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
        /// 
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

 

Form1.Designer.cs 파일은

내가 만들고자하는 윈도우 창을

미리보기 형식으로 버튼이나

텍스트 박스 등을 설정하고

구축하는 역할입니다

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

 

 

그리고 Form1.cs는

Form1.Designer.cs에서 만들어놓은

버튼이나 텍스트 박스 등을

실제로 다루는 C# 소스 코드를

작성하는 역할입니다

 

껍데기는 Form1.Designer.cs

실제 내용물은 Form1.cs라고

생각하시면 더 이해가 편하실것 같습니다

 

 

그리고 Form1.cs의 13번 코드 줄에

public partial class Form1 : Form의

설명을 하자면

애초에 Form1.cs와 Form1.Designer.cs는

똑같은 클래스인데 2개의 cs 파일로

나눠버린 것이기 때문에

 

일반적인 public class 라고 선언하지않고

나누어 떨어진 아이들이란 말을 붙여

public partial class Form1 : Form 이라고

사용합니다

 

여기서 Form1.cs의 17번 코드 줄에

있는 InitializeComponent()는

UI를 생성하는 아이이므로

반드시 선언되어야 하는 존재입니다

 

 


혹시 다른것에 대해 궁금하신게

있으시다면

댓글에 달아주세요

 

아는것이면 친절하게 쉽게

모르는것이면 또 제가 알아내서

친절하게 쉽게 포스팅해서

설명해드리겠습니다

 

 

 

 

 

반응형
LIST
Comments