Notice
Recent Posts
Recent Comments
Link
«   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
Archives
Today
Total
05-17 00:00
관리 메뉴

nomad-programmer

[Programming/C#] WinForm : 윈도우 모양 바꾸기 본문

Programming/C#

[Programming/C#] WinForm : 윈도우 모양 바꾸기

scii 2020. 10. 2. 22:41

Form 클래스는 윈도우 모양을 결정짓는 크기, 배경색, 전경색, 투명도, 제목, 폰트 등 여러 가지 프로퍼티를 갖고 있다. 다음 표에는 Form 클래스의 프로퍼티 중 윈도우의 모습을 결정짓는 항목들이 나타나 있다.

종류 프로퍼티 설명
크기 Width 창의 너비를 나타낸다.
Height 창의 높이를 나타낸다.
색깔 BackColor 창의 배경 색깔을 나타낸다.
BackgroundImage 창의 배경 이미지를 나타낸다.
Opacity 창의 투명도를 나타낸다.
스타일 MaximizeBox 최대화 버튼을 설치할 것인지의 여부를 나타낸다.
MinimizeBox 최소화 버튼을 설치할 것인지의 여부를 나타낸다.
Text 창의 제목을 나타낸다.

Width & Height 예제

using System;
using System.Windows.Forms;

namespace SimpleWindow
{
    internal class MainApp : Form
    {
        static void FormMouseDown(object sender, MouseEventArgs e)
        {
            Form form = (Form)sender;
            int oldWidth = form.Width;
            int oldHeight = form.Height;

            if(e.Button == MouseButtons.Left)
            {
                if(oldWidth < oldHeight)
                {
                    form.Width = oldHeight;
                    form.Height = oldWidth;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if(oldHeight < oldWidth)
                {
                    form.Width = oldHeight;
                    form.Height = oldWidth;
                }
            }
            Console.WriteLine("윈도우 크기가 변경되었습니다.");
            Console.WriteLine($"Width: {form.Width}, Height: {form.Height}");
        }

        static int Main(string[] args)
        {
            MainApp form = new MainApp();
            form.Width = 300;
            form.Height = 200;

            form.MouseDown += new MouseEventHandler(FormMouseDown);

            Application.Run(form);

            return 0;
        }
    }
}

창의 배경색과 투명도를 조절 및 창의 배경 이미지 변경

창의 배경색은 BackColor 프로퍼티를 통해 바꿀 수 있는데, System.Drawing.Color 형식이기때문에 다음과 같이 Color 클래스의 정적 메소드나 미리 정의되어 있는 상수값을 이용해서 값을 지정해야 한다.

Form form = new Form();

// Color 구조체에는 Red, Green, Blue 같은 대표적인 색부터
// MistyRose, WhiteSmoke처럼 다양한 색상이 미리 정의되어 있다.
form.BackColor = Color.Red;

// 입맛대로 값을 지정하고 싶다면 FromArgb() 메소드를 이용하면 된다.
form.BackColor = Color.FromArgb(255, 255, 0, 0);

창의 투명도는 Opacity 프로퍼티를 통해 조절한다. 이 프로퍼티는 double 형식으로 0.00~1.00 사이의 값을 가진다. 0에 가까울수록 투명해지고, 1에 가까울수록 불투명해진다.

Form form = new Form();

form.Opacity = 0.85;
form.Opacity = 1.00;

창에 배경 이미지를 지정할 수도 있다. BackgroundImage 프로퍼티에 Image 형식의 인스턴스를 할당하면 된다. Image의 인스턴스는 여러 가지 방법으로 만들 수 있다.

Form form = new Form();

form.BackgroundImage = Image.FromFile("MyImage.jpg");

System.Windows.Forms와 System.Drawing 어셈블리를 프로젝트 참조에 추가

창의 색상, 투명도, 배경 이미지 변경의 예제

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleWindow
{
    internal class MainApp : Form
    {
        Random rand;

        public MainApp()
        {
            rand = new Random();

            this.MouseDown += new MouseEventHandler(MainApp_MouseDown);
            this.MouseWheel += new MouseEventHandler(MainApp_MouseWheel);
        }

        void MainApp_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                Color oldColor = this.BackColor;
                this.BackColor = Color.FromArgb(
                    rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
            }
            else if (e.Button == MouseButtons.Right)
            {
                if(this.BackgroundImage != null)
                {
                    this.BackgroundImage = null;
                    return;
                }
                string file = @"c:/users/tmp/pictures/clean_code.png";
                if (!System.IO.File.Exists(file))
                {
                    MessageBox.Show("이미지 파일이 없습니다.");
                }
                else
                {
                    this.BackgroundImage = Image.FromFile(file);
                }
            }
        }

        void MainApp_MouseWheel(object sender, MouseEventArgs e)
        {
            this.Opacity = this.Opacity + (e.Delta > 0 ? 0.1 : -0.1);
            Console.WriteLine($"opacity: {this.Opacity}");
        }

        static int Main(string[] args)
        {
            Application.Run(new MainApp());

            return 0;
        }
    }
}


MinimizaBox & MaximizaBox 프로퍼티와 창의 제목 Text 프로퍼티

MinimizeBox와 MaximizeBox 프로퍼티는 boolean 형식으로, 버튼을 창에 표시하고자 할 때는 true를 입력하고 감추고자 할 때는 false를 입력한다.

// 최대화 버튼은 표시, 최소화 버튼은 숨김
Form form = new Form();
form.MaximizeBox = true;
form.MinimizeBox = false;

창의 제목을 나타내는 Text 프로퍼티는 string 형식이다.

Form form = new Form();
form.Text = "Form 윈도우창의 타이틀";

최대화, 최소화, 창 타이틀 예제

using System;
using System.Windows.Forms;

namespace SimpleWindow
{
    internal class MainApp : Form
    {
        public MainApp()
        {
            this.MouseDown += new MouseEventHandler(Form_MouseDown);
        }

        static void Form_MouseDown(object sender, MouseEventArgs e)
        {
            Form form = (Form)sender;

            if(e.Button == MouseButtons.Left)
            {
                form.MaximizeBox = true;
                form.MinimizeBox = true;
                form.Text = "최소화/최대화 버튼 활성화!";
            }
            else if (e.Button == MouseButtons.Right)
            {
                form.MaximizeBox = false;
                form.MinimizeBox = false;
                form.Text = "최소화/최대화 버튼 비활성화!";
            }
        }

        static int Main(string[] args)
        {
            MainApp form = new MainApp();

            form.Width = 400;

            Application.Run(form);

            return 0;
        }
    }
}

Comments