C# 윈도우 폼에서 특정 부분의 폰트를 설정하는 창을 만들어보자. 

Form만 Load 이벤트 핸들러를 달아주고 나머지 박스들은 더블 클릭을 통해 자동으로 만들어진 껍질 사용

 

운영체제에 설치된 폰트 목록을 가져와 콤보박스에 넣는 함수  

 // Form1의 load 이벤트 핸들러 
        private void addFont(object sender, EventArgs e)
        {
            // 운영체제에 설치되어 있는 폰트 목록 가져오기
            FontFamily[] fonts = FontFamily.Families;
            // foreach 문을 통해 conboBox에 하나씩 넣기
            foreach (FontFamily font in fonts)
                comboFontBox.Items.Add(font.Name);
        }

폰트 설정 변경에 공통으로 사용할 함수

 void changeFont()
        {
            FontStyle style = FontStyle.Regular; // FontStyle 일반 스타일 설정
            
            if(boldCheckBox.Checked)
                style |= FontStyle.Bold; // Bold 효과 추가
            if (italicCheckBox.Checked)
                style |= FontStyle.Italic; // Italic 효과 추가
            if (underlineCheckBox.Checked)
                style |= FontStyle.Underline; // Underline 효과 추가

            // 설정한 폰트로 바꾸기 Font("폰트 이름", int(사이즈), FontStyle); 
            textBox.Font = new Font((string)comboFontBox.SelectedItem, 12, style);
        }

 

전체코드:

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 FontBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Form1의 load 이벤트 핸들러 
        private void addFont(object sender, EventArgs e)
        {
            // 운영체제에 설치되어 있는 폰트 목록 가져오기
            FontFamily[] fonts = FontFamily.Families;
            // foreach 문을 통해 conboBox에 하나씩 넣기
            foreach (FontFamily font in fonts)
                comboFontBox.Items.Add(font.Name);
        }
        // 폰트 변화에 공통적으로 사용할 함수
        void changeFont()
        {
            FontStyle style = FontStyle.Regular; // FontStyle 일반 스타일 설정
            
            if(boldCheckBox.Checked)
                style |= FontStyle.Bold; // Bold 효과 추가
            if (italicCheckBox.Checked)
                style |= FontStyle.Italic; // Italic 효과 추가
            if (underlineCheckBox.Checked)
                style |= FontStyle.Underline; // Underline 효과 추가

            // 설정한 폰트로 바꾸기 Font("폰트 이름", int(사이즈), FontStyle); 
            textBox.Font = new Font((string)comboFontBox.SelectedItem, 12, style);
        }

        // Bold 체크 박스 클릭 이벤트 핸들러
        private void boldCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            changeFont();
        }
        // Italic 체크 박스 클릭 이벤트 핸들러
        private void italicCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            changeFont();
        }
        // 콤보박스 클릭 이벤트 핸들러
        private void comboFontBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeFont();
        }
        // 밑줄 체크 박스 클릭 이벤트 핸들러
        private void underlineCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            changeFont();
        }
    }
}

 

 

C#의 WinForm의 경우는 Java의 JFrame과는 다르게

폼 디자이너 툴을 제공하여 프로그래머가 일일이 컴포넌트를 구현할 필요 없이 그림 그리듯 GUI를 만드는 것이 가능하며

이를 WYSIWYG(What You See Is What You Get)방식의 개발이라고 합니다.

 

java: 간단한 로그인 화면 만들기

import javax.swing.*;
import java.awt.*;

public class SimpleLoginForm extends JFrame {
   public SimpleLoginForm(){
           JPanel p = new JPanel();
           Label lid = new Label("id");
           Label lpwd= new Label("pass");
           add(lid);
           add(lpwd);
           TextField tid = new TextField();
           TextField tpwd = new TextField();
           add(tid);
           add(tpwd);
           JButton jsave = new JButton("저장");
           add(jsave);
           lid.setBounds(80, 120, 40, 40);
           lpwd.setBounds(80,190,60,40);
           
           tid.setBounds(160, 120, 200, 40);
           tpwd.setBounds(160, 190, 200, 40);        
           jsave.setBounds(165, 480, 80, 30);
      add(p);
      setSize(600,600);
      setTitle("회원가입");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }    
   public static void main(String args[]) {
	   new SimpleLoginForm();
   }
}

JFrame을 상속 받고 일일이 컴포넌트들을 코딩하여 붙이고 위치 혹은 간격 설정을 프로그래머가 해주어야 한다.

정말 손이 많이 가고 번거롭다.

 

C#: 간단한 로그인 화면 만들기

이제 옆의 도구 상자에서 원하는 컴포넌트를 드래그 앤 드롭 하여 붙여넣고

적당히 클릭하여 내용을 바꿔주고 실행시키면

다음과 같이 자바와는 다르게 정말 간단하게 화면을 만들 수가 있다.

+ Recent posts