본문 바로가기
C#

[C#] FormBorderStyle None 사용시 아이콘, Resize, Move, taskbar click to minimize

by Jcoder 2022. 2. 18.

FormBorderStyle None

 

1. FormBorderStyle None.

2. 버튼 아이콘 설정.

3. 위에 패널 or label 더블 클릭시 폼 최대화.

4. 위에 패널 or label 마우스 클릭후 폼 이동.

5. form padding으로 폼 Resize.

6. 작업표시줄에서 아이콘 클릭시 폼 최소화, 원상태 복귀.

 

2. 아이콘

 - 환경설정 버튼 : font Wingdings, 16.2pt, style=Bold, text = R

 - 최소화, 최대화, 닫기 : font Webdings, 13.8pt, style=Bold, text = 0, 1(최대화시 원상 복귀 2), r

최대화 이전
최대화 후

C#: 아이콘 폰트를 사용해보자. (webdings, wingdings) (tistory.com)

 

C#: 아이콘 폰트를 사용해보자. (webdings, wingdings)

WindowsForms를 개발하다보면, 필연적으로 마주치는 상황이 생긴다. 타이틀바의 최소화, 최대화, 닫기 버튼 이라든지.. 뮤직플레이어의 재생, 정지 버튼이라든지.. 버튼의 텍스트를 아이콘으로 표현

vip00112.tistory.com

 

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        const int WS_MINIMIZEBOX = 0x20000;
        const int CS_DBLCLKS = 0x8;

        const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 1;
        private Point point;
        private bool isMove;

        private readonly Color _BackGroundColor = Color.FromArgb(42, 44, 52);
        private readonly Color _ForeGroundColor = Color.FromArgb(250, 250, 250);
        private bool maximized;

        public Form1()
        {
            InitializeComponent();
            SetControls();
            SetControlsEvent(); 
        }

        // 작업표시줄 아이콘 클릭시 최소화, 복귀
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= WS_MINIMIZEBOX;
                cp.ClassStyle |= CS_DBLCLKS;
                return cp;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BackColor = _BackGroundColor;
            // 폼 리사이즈시 필요한 패딩
            Padding = new Padding(3);
            label1.Text = "test";
        }

        // 폼 이동, 리사이즈
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    if (m.Result == (IntPtr)HTCLIENT)
                    {
                        var p = this.PointToClient(new Point(m.LParam.ToInt32()));

                        m.Result =
                            (IntPtr)
                            (p.X <= 6
                                 ? p.Y <= 6 ? 13 : p.Y >= this.Height - 7 ? 16 : 10
                                 : p.X >= this.Width - 7
                                       ? p.Y <= 6 ? 14 : p.Y >= this.Height - 7 ? 17 : 11
                                       : p.Y <= 6 ? 12 : p.Y >= this.Height - 7 ? 15 : p.Y <= 24 ? 2 : 1);
                    }
                    break;
            }
        }

        private void SetControls()
        {
            label1.ForeColor = _ForeGroundColor;

            btn_Setting.ForeColor = _ForeGroundColor;
            btn_Setting.BackColor = _BackGroundColor;
            btn_Setting.Cursor = Cursors.Hand;
            // 버튼 border 사이즈 0으로 변경
            btn_Setting.FlatStyle = FlatStyle.Flat;
            btn_Setting.FlatAppearance.BorderSize = 0;

            btn_Min.ForeColor = _ForeGroundColor;
            btn_Min.BackColor = _BackGroundColor;
            btn_Min.Cursor = Cursors.Hand;
            btn_Min.FlatStyle = FlatStyle.Flat;
            btn_Min.FlatAppearance.BorderSize = 0;

            btn_Max.ForeColor = _ForeGroundColor;
            btn_Max.BackColor = _BackGroundColor;
            btn_Max.Cursor = Cursors.Hand;
            btn_Max.FlatStyle = FlatStyle.Flat;
            btn_Max.FlatAppearance.BorderSize = 0;

            btn_Exit.ForeColor = _ForeGroundColor;
            btn_Exit.BackColor = _BackGroundColor;
            btn_Exit.Cursor = Cursors.Hand;
            btn_Exit.FlatStyle = FlatStyle.Flat;
            btn_Exit.FlatAppearance.BorderSize = 0;
        }

        private void SetControlsEvent()
        {
            this.FormClosed += Form1_FormClosed;
            // 최대화
            flowLayoutPanel1.MouseDoubleClick += Form_MouseDoubleClick;
            label1.MouseDoubleClick += Form_MouseDoubleClick;

            // 폼 이동
            label1.MouseDown += Label1_MouseDown;
            label1.MouseUp += Label1_MouseUp;
            label1.MouseMove += Label1_MouseMove;

            // 최소화
            btn_Min.Click += (s, e) => { WindowState = FormWindowState.Minimized; };
            // 최대화
            btn_Max.Click += (s, e) => { SetWindowState(); };
            // 닫기
            btn_Exit.Click += (s, e) => { Close(); };
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void Form_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                SetWindowState();
        }

        private void Label1_MouseDown(object sender, MouseEventArgs e)
        {
            isMove = true;
            point = new Point(e.X, e.Y);
        }
        
        private void Label1_MouseUp(object sender, MouseEventArgs e)
        {
            isMove = false;
        }

        private void Label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMove && (e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                Location = new Point(this.Left - (point.X - e.X), this.Top - (point.Y - e.Y));
            }
        }

        private void SetWindowState()
        {
            if (maximized)
            {
                this.WindowState = FormWindowState.Normal;
                maximized = false;
                // 이모티콘 변경
                btn_Max.Text = "1";
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                maximized = true;
                // 이모티콘 변경
                btn_Max.Text = "2";
            }
        }
    }
}