컴퓨터 이야기/c#

How to move and resize a form without a border?

golfin 2018. 6. 9. 15:07



- Form의 BorderStyle을 Node으로 하면 Border가 사라진다.
- 그러면 Resize 기능도 사라진다.
- 이런 경우 Grip을 실시간 추가 하고 OnPaint로 그려주면 Resize Grip을 이용 Resize가 가능하다.
- 단, Form을 다른 콘트롤이 꽉 채우게 되면 Resizing Grip이 Background로 그려지기 때문에 보이지를 않으므로 주의한다.

출처 :



        public FormMain()
        {               

            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }
        private const int cGrip = 16;      // Grip size
        private const int cCaption = 32;   // Caption bar height;

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
            ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
            rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
            e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x84)
            {  // Trap WM_NCHITTEST
                Point pos = new Point(m.LParam.ToInt32());
                pos = this.PointToClient(pos);
                if (pos.Y < cCaption)
                {
                    m.Result = (IntPtr)2;  // HTCAPTION
                    return;
                }
                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                    return;
                }
            }
            base.WndProc(ref m);
        }
        ///////////////////////////////////////////