ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# 윈폼 (2020.06.15)
    C# 2020. 6. 15. 17:57

    윈폼 애플리케이션

     

    - 컨트롤을 사용하여 프로그래머가 원하는 화면을 구성하고 이벤트가 발 생했을 때 처리하고자 하는 작업을 이벤트 처리기에 기술하는 방식으로 프로그래밍된 프로그램.

    - 윈도우 폼에 컨트롤 또는 컴포넌트를 배치하고 이벤트 처리기를 등록하 여 사용자의 다양한 요구를 입력으로 받아 처리한 후 실행 결과를 응답 해 주는 방식으로 작동.

     

    윈도우 폼

    - 단순히 폼이라고 부름.

    - 운영체제에서 제공하는 기본적인 화면 단위인 창을 말하는 개념

    - 사각형 모양의 작은 화면 영역을 의미

    - 사용자에게 정보를 제공하고 사용자가 입력하는 자료를 받음

     

    폼 클래스

    - 윈도우 폼을 나타내는 클래스.

    - System.Windows.Forms 네임스페이스 속함.

     

    -컴포넌트 클래스

    컨트롤 클래스의 베이스 클래스이며 화면에 직접적으로 나타나지 않으 나 개념적인 부분을 나타내는 클래스.

     

    -컨트롤 클래스

    폼에 직접 표시되는 컨트롤을 위한 클래스.

     

    생성된 프로젝트의 파일

    Program.cs: 윈폼 응용 프로그램의 시작점을 포함하는 C# 소스파일 entry point

    Form1.cs: 윈폼 응용 프로그램의 C# 소스파일

    Form1.Designer.cs: 윈폼 응용 프로그램 폼 디자인 정의를 포함하는 C# 소스파일

    WindowsApplication1.csproj: 윈폼 응용프로그램 프로젝트 파일

    Properties/AssemblyInfo.cs: 프로젝트가 생성하는 어셈블리를 설명하고 버전 관리 정보를 지정하는 데 사용하며, 애트리뷰트 정의를 포함하는 C# 소스파일

    Properties/Resources.Designer.cs: 윈폼 응용 프로그램의 자원에 대한 C# 정의를 포함하는 C# 소스파일 Properties/Resources.resx: 윈폼 응용 프로그램의 자원 파일

    Properties/Settings.Designer.cs: 프로젝트 설정에 대한 C# 정의를 포함 하는 C# 소스파일

    Properties/Settings.settings : 프로젝트에 대한 설정 파일 폼 생성된 프로젝트의 파일

    Program.cs: 윈폼 응용 프로그램의 시작점을 포함하는 C# 소스파일

    Form1.cs: 윈폼 응용 프로그램의 C# 소스파일

    Form1.Designer.cs: 윈폼 응용 프로그램 폼 디자인 정의를 포함하는 C# 소스파일

    WindowsApplication1.csproj: 윈폼 응용프로그램 프로젝트 파일

    Properties/AssemblyInfo.cs: 프로젝트가 생성하는 어셈블리를 설명하고 버전 관리 정보를 지정하는 데 사용하며, 애트리뷰트 정의를 포함하는 C# 소스파일

    Properties/Resources.Designer.cs: 윈폼 응용 프로그램의 자원에 대한 C# 정의를 포함하는 C# 소스파일 Properties/Resources.resx: 윈폼 응용 프로그램의 자원 파일

    Properties/Settings.Designer.cs: 프로젝트 설정에 대한 C# 정의를 포함 하는 C# 소스파일

    Properties/Settings.settings : 프로젝트에 대한 설정 파일

     

    위와 같은 항목이 없다면 visual studio installer에서 추가하도록 하자.

    program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WinFormApp
    {
        static class Program
        {
            /// <summary>
            /// 해당 애플리케이션의 주 진입점입니다.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                
            }
        }
    }
    

    Form1.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 WinFormApp
    {
        public partial class Form1 : Form  //Form으로부터 상속 받음
        								   //partial : Designers.cs 랑 Form1이랑 묶여있다.
        {
            public Form1()
            {
                InitializeComponent(); //<-요것도 건들이면 안된다.
                
            }
        }
    }
    

     

     

    Form1.Designer.cs

    namespace WinFormApp
    {
        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.components = new System.ComponentModel.Container();
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 450);
                this.Text = "Form1";
            }
    
            #endregion
        }
    }
    //**************************어드밴스드 한 부분이기 때문에 손대지말고 마우스로 하자
    

     

     

    partial은 하나의 클래스를 두개로 분리해서 쓰기 위한 부분이다.

     

     

    디자인

    - 컨트롤

    - 프로퍼티

    - 컴포넌트

    - 이벤트

     

    코드

    -멤버

    -이벤트 처리기

     

    ctrl+shift+s 한꺼번에 저장

     

    위의 도구들 끌어다가 쓰면 된다. 

    컴포넌트는 밑에 나타난다. 특정 이벤트가 발생하면 호출된다.

     

    *******속성 창********

    윈도우 창 안에 있는 버튼, 윈도우창, 도구 창을 통해서 넣을 수 있는 도구들을 프로그래머가 맘대로 설정할 수 있는 역할을 한다. (예시 : 버튼의 폰트, 배경 색깔, 패딩)

     

    네이밍 주기 (저 버튼 안에 있는 내용은 텍스트일 뿐 버튼에 대한 이름이 아니다.)

    자주 쓰는 값들은 굵은 글자로 나온다. 또는 우리가 바꾼 것들이 굵게 표시된다.

     

    MainForms1.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 WinFormApp
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime now=DateTime.Now;
                MessageBox.Show("Hello World" + now);
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    

     

     

    label 과 textbox를 사용한 현재시간 출력

    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 WinFormApp
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime now=DateTime.Now;
                TxtCurrentDate.Text= now.ToString();
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    폼 클래스

    - 윈도우 폼을 사용하는 모든 클래스의 베이스 클래스

    - System.Windows.Forms 네임스페이스에 포함

    Component 클래스

    - .NET 프레임워크에서 컴포넌트 기반 프로그래밍 기법에서의 컴포넌트 개념 을 지원하는 클래스

    - System.ComponentModel 네임스페이스에 포함

    - 윈폼 애플리케이션뿐만 아니라 컴포넌트 개념이 필요한 다른 곳에서도 사용 윈폼에서 제공하는 화면을 구성하는 여러 요소뿐만 아니라 화면에 표시되지 않는 요소도 컴포넌트로 표현.

     

    Control 클래스

    - 윈폼 애플리케이션에서 화면에 표시되는 구성요소를 나타내기 위해서 사용 되는 컨트롤들의 베이스 클래스

    - 폼과 폼에 배치되는 여러 요소에서 공통적으로 필요한 멤버를 정의

     

    ScrollableControl 클래스

    -스크롤 개념이 필요한 컨트롤을 정의하기 위해서 사용되는 베이스 클래스

     

    ContainerControl 클래스

    -여러 컨트롤이나 컴포넌트를 포함할 수 있는 컨트롤에서의 포커스 관리를 하기 위한 베이스 클래스

     

    주요 프로퍼티

    FormBorderStyle 열거형 : 폼의 테두리 모양을 설정하는 프로퍼티

    label과 timer은 이용한 타이머 

    레이블 설정은 글자크기 변경하고, 타이머 설정

    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 WinFormsApp2
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                DateTime now = DateTime.Now;
                label1.Text = now.ToString("HH:mm:ss");
            }
        }
    }
    

    https://www.flaticon.com/

    아이콘도 원하는 아이콘도 바꾼다. 저작권 걸릴 수 있으니 무료만 쓴다.

     

     

    Flaticon, the largest database of free vector icons

    Download all icons in SVG, PSD, PNG, EPS format or as webfonts

    www.flaticon.com

    https://convertio.co/kr/png-ico/

     

    PNG ICO 변환 (온라인 무료) — Convertio

    png 파일(들) 업로드 컴퓨터, Google Drive, Dropbox, URL에서 선택하거나 이 페이지에서 드래그하여 선택해 주세요.

    convertio.co

    확장자가 ico여야 한다.

     

    아이콘이 변경됬다.

     

    폼 클래스의 메소드

    폼 클래스의 가장 중요한 메서드는 FormLoad이다. 나머진 몰라도 되나 Load() 폼이 열릴 때 Colse() 폼이 닫히는 순간과 Activate()폼이 다시 활성화 될 때 동작한다.

     

    여기서 더블클릭으로 활성화 및 비활성화 

    private void MainForm_Load(object sender, EventArgs e)
            {
                MessageBox.Show("폼 로드 시 발생");
            }
            private void MainForm_Close(object sender, EventArgs e)
            {
                MessageBox.Show("폼 종료시 발생");
            }
            
            private void MainForm_Activated(object sender, EventArgs e)
            {
                MessageBox.Show("폼 활성화 시 발생");
            }

     

    종료시 프롬프트창 열어서 경고하기

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("진짜 닫을래?", "경고", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                { //취소를 안시켜서 종료
                    e.Cancel = false;
                }    
                else
                { //취소시키니까 종료안됨
                    e.Cancel = true;
                }
            }

     

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)

     

    모든 이벤트 메소드는 objuect sender와 FormClosingEventArgs e를 가지고 있다. 'EventArgs e' 로 핸들링한다.

     

     

    컨트롤 클래스의 프로퍼티

    컨트롤

    화면에 표시되어 사용자와 상호작용을 수행하는 컴포넌트를 의미.

    종류 : 버튼, 레이블, 텍스트, 리스트

     

    버튼 기반 컨트롤

    - 버튼

    - 체크 상자

    - 라디오 버튼

     

    버튼 프로퍼티 설정 변경

     

Designed by Tistory.