폰트 적용

 

예제로 창원 단감 폰트 사용..!

ChangwonDangamAsac-Bold_0712.ttf

using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;

namespace ApplyFontFromCode
{
    public partial class Form1 : Form
    {
        private PrivateFontCollection privateFontCollection;

        public Form1()
        {
            InitializeComponent();

            //해당 리소스가 해제되면 폰트 오류가 발생되므로 전역 변수로 선언 필요.
            privateFontCollection = new PrivateFontCollection();
            privateFontCollection.AddFontFile(Path.Combine(Application.StartupPath, "ChangwonDangamAsac-Bold_0712.ttf"));

            SetControlFont(this);
        }

        private void SetControlFont (Control parent)
        {
            foreach (Control child in parent.Controls) //모든 하위 컨트롤들에 대해서 적용시켜줍니다.
            {
                child.Font = new Font(privateFontCollection.Families[0], child.Font.Size, child.Font.Style);

                if (child.Controls.Count > 0) //panel, groupbox 와 같이 하위 컨트롤이 더 있는 경우도 적용시켜줍니다.
                {
                    SetControlFont(child);
                }
            }
        }
    }
}

+ Recent posts