Get rid of Winform ControlBox and Add Your Own Customizable ControlBox

CsharpChico -2 Tallied Votes 639 Views Share
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Panel panel1 = new Panel();
        private PictureBox box = new PictureBox();
        private Panel closeButton = new Panel();
        private bool moveable;
        private Point currentPosition;
        private Image img;
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            closeButton.Dock = DockStyle.Right;
            panel1.Controls.Add(closeButton);
            closeButton.Width = 35;
            closeButton.Click += new EventHandler(closeButton_Click);
            img = box.ErrorImage;
            box.Dispose();
            closeButton.Paint += new PaintEventHandler(closeButton_Paint);
            panel1.BackColor = Color.Blue;
            panel1.Dock = DockStyle.Top;
            panel1.Height = 35;
            this.Controls.Add(panel1);
            panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
            panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
            panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
        }

        void closeButton_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(img, 0, 0, closeButton.Width, closeButton.Height);
        }

        void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveable)
            {
                Point newPosition = Control.MousePosition;
                newPosition.X = newPosition.X - currentPosition.X; // .Offset(mouseOffset.X, mouseOffset.Y);
                newPosition.Y = newPosition.Y - currentPosition.Y;
                this.Location = newPosition;
            }
        }

        void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            moveable = false;
        }

        void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            moveable = true;
            currentPosition.X = e.X;
            currentPosition.Y = e.Y;
        }

        
    }
}
Here is a Form With It's Own Customizable ControlBox.