Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys... i am trying to make my own custom form for my application.

i created a new class with this data

C#
<pre> <pre> public class CustomizedForm : Form
    {
        private Color backgroundColor1 = ColorTranslator.FromHtml("#74ebd5");
        private Color backgroundColor2 = ColorTranslator.FromHtml("#ACB6E5");

        private Color borderColor = ColorTranslator.FromHtml("#274046");
        private GUI.ControlBox.CustomizedControlBox CCB;
        private bool resizeable = true;
        public Size CustomFormSize;
        [CategoryAttribute("CustomizedControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool ShowCloseButton
        {
            get { return CCB.CloseIsEnabled; }
            set { this.CCB.CloseIsEnabled = value; }
        }
        [CategoryAttribute("CustomizedControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool ShowMaxmizeButton
        {
            get { return CCB.MaximizeIsEnabled; }
            set { this.CCB.MaximizeIsEnabled = value; }
        }
        [CategoryAttribute("CustomizedControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool ShowMinmizeButton
        {
            get { return CCB.MinimizeIsEnabled; }
            set { this.CCB.MinimizeIsEnabled = value; }
        }
        [CategoryAttribute("CustomizedControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool ShowReloadButton
        {
            get { return CCB.RefreshIsEnabled; }
            set { this.CCB.RefreshIsEnabled = value; }
        }

        [CategoryAttribute("CustomizedControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool Resizeable
        {
            get { return resizeable; }
            set { resizeable = value; }
        }

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
(
    int nLeftRect, // x-coordinate of upper-left corner
    int nTopRect, // y-coordinate of upper-left corner
    int nRightRect, // x-coordinate of lower-right corner
    int nBottomRect, // y-coordinate of lower-right corner
    int nWidthEllipse, // height of ellipse
    int nHeightEllipse // width of ellipse
 );
        public Bitmap THE_ICON
        {
            get
            {
                try
                {
                    return CCB.THE_ICON;
                }
                catch { return null; }
            }
            set
            {
                CCB.THE_ICON = value; Invalidate();
            }
        }

        public Color BackgroundColor1
        {
            get { return backgroundColor1; }
            set { backgroundColor1 = value; Invalidate(); }
        }

        public Color BackgroundColor2
        {
            get { return backgroundColor2; }
            set { backgroundColor2 = value; Invalidate(); }
        }

        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; Invalidate(); }
        }

        public CuteForm()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            CCB = new GUI.ControlBox.CustomizedControlBox(this.Handle, this);
            this.Invoke((MethodInvoker)delegate
            {
                //perform on the UI thread
                this.Controls.Add(this.CCB);
            });
            if (this.StartPosition == FormStartPosition.CenterScreen)
                this.CenterToScreen();
            else if (this.StartPosition == FormStartPosition.CenterParent)
                this.CenterToParent();
            BorderColor = ColorTranslator.FromHtml("#2B32B2");
            this.ShowReloadButton = false;
            if (this.Height > Screen.PrimaryScreen.WorkingArea.Size.Height)
            {
                this.Height = Screen.PrimaryScreen.WorkingArea.Size.Height;
                this.Update();
            }
        }

        private ButtonBorderStyle BBS = ButtonBorderStyle.Solid;
        private const int
    HTLEFT = 10,
    HTRIGHT = 11,
    HTTOP = 12,
    HTTOPLEFT = 13,
    HTTOPRIGHT = 14,
    HTBOTTOM = 15,
    HTBOTTOMLEFT = 16,
    HTBOTTOMRIGHT = 17;
        const int _ = 10; // you can rename this variable if you like

        Rectangle Top { get { return new Rectangle(0, 0, this.ClientSize.Width, _); } }
        Rectangle Left { get { return new Rectangle(0, 0, _, this.ClientSize.Height); } }
        Rectangle Bottom { get { return new Rectangle(0, this.ClientSize.Height - _, this.ClientSize.Width, _); } }
        Rectangle Right { get { return new Rectangle(this.ClientSize.Width - _, 0, _, this.ClientSize.Height); } }

        Rectangle TopLeft { get { return new Rectangle(0, 0, _, _); } }
        Rectangle TopRight { get { return new Rectangle(this.ClientSize.Width - _, 0, _, _); } }
        Rectangle BottomLeft { get { return new Rectangle(0, this.ClientSize.Height - _, _, _); } }
        Rectangle BottomRight { get { return new Rectangle(this.ClientSize.Width - _, this.ClientSize.Height - _, _, _); } }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (ClientRectangle.Width == 0 && ClientRectangle.Height == 0)
                return;

            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(this.Width, this.Height), backgroundColor1, backgroundColor2);
            pe.Graphics.FillRectangle(b, ClientRectangle);
            ControlPaint.DrawBorder(pe.Graphics, ClientRectangle, borderColor, 3, BBS, borderColor, 3, BBS, borderColor, 3, BBS, borderColor, 3, BBS);
            b.Dispose();
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);
            if (this.Height > Screen.PrimaryScreen.WorkingArea.Size.Height)
            {
                this.Height = Screen.PrimaryScreen.WorkingArea.Size.Height;
                this.Update();
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (this.Size.Height < CustomFormSize.Height)
                this.Height = CustomFormSize.Height;
            if (this.Size.Width < CustomFormSize.Width)
                this.Width = CustomFormSize.Width;
            if (this.Height > Screen.PrimaryScreen.WorkingArea.Size.Height)
                this.Height = Screen.PrimaryScreen.WorkingArea.Size.Height;
            if (this.Width < 120)
                this.Width = 120;
            if (this.Height < 50)
                this.Height = 50;
            this.Update();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.StartPosition == FormStartPosition.CenterScreen)
                this.CenterToScreen();
            else if (this.StartPosition == FormStartPosition.CenterParent)
                this.CenterToParent();

            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;

                this.Size = new Size(Screen.PrimaryScreen.WorkingArea.Size.Width, Screen.PrimaryScreen.WorkingArea.Size.Height);
            }
        }

        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);
            if (!Resizeable) return;
            if (message.Msg == 0x84) // WM_NCHITTEST
            {
                var cursor = this.PointToClient(Cursor.Position);

                if (TopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;
                else if (TopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;
                else if (BottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;
                else if (BottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;

                else if (Top.Contains(cursor)) message.Result = (IntPtr)HTTOP;
                else if (Left.Contains(cursor)) message.Result = (IntPtr)HTLEFT;
                else if (Right.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;
                else if (Bottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;
            }
        }
    }


and for top bar which contains (Icon, Title and Control Box) i created a new customized Panel for this purpose...

C#
<pre>public class CustomizedControlBox: Panel
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;
        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        private Color background_Color1 = ColorTranslator.FromHtml("#2B32B2");
        private Color background_Color2 = ColorTranslator.FromHtml("#2B32B2");
        private GUI.Button_.CloseButton CloseBtn;
        private GUI.Button_.MaximizeButton MaxBtn;
        private GUI.Button_.MinimizeButton MinBtn;
        private GUI.Button_.ReloadButton RefreshBtn;
        private Label Title;
        IntPtr Parent_Handle;
        private GUI.Form_.CustomizedForm CF;
        private PictureBox iCON;
        private Bitmap _I_C_O_N;
        public Bitmap THE_ICON
        {
            get
            {
                if (_I_C_O_N == null)
                    return Properties.Resources.HS_FAV_PNG;
                else
                    return _I_C_O_N;
            }
            set
            {
                _I_C_O_N = value;
            }
        }
        
        [CategoryAttribute("CuteControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool CloseIsEnabled
        {
            get { return CloseBtn.IsEnabled; }
            set { CloseBtn.IsEnabled = value; ValidateButtons(); }
        }
        [CategoryAttribute("CuteControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool MaximizeIsEnabled
        {
            get { return MaxBtn.IsEnabled; }
            set { MaxBtn.IsEnabled = value; ValidateButtons(); }
        }
        [CategoryAttribute("CuteControlBoxAppearance")]
        [IODescriptionAttribute("ControlBoxTypeDescr")]
        public bool MinimizeIsEnabled
        {
            get { return MinBtn.IsEnabled; }
            set { MinBtn.IsEnabled = value; ValidateButtons(); }
        }
        public bool RefreshIsEnabled
        {
            get { return RefreshBtn.IsEnabled; }
            set { RefreshBtn.IsEnabled = value; ValidateButtons(); }
        }
        //    [System.ComponentModel.DefaultValue(typeof(DockStyle), "Fill")]
        public CuteControlBox(IntPtr _Handle, GUI.Form_.CustomizedForm _CF)
        {
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.Parent_Handle = _Handle;
            this.CF = _CF;
            this.Dock = DockStyle.Top;
            this.Height = 30;
            this.Margin = new Padding(0, 0, 0, 10);
            this.Location = new Point(0, 0);

            this.CloseBtn = new ONTIME_CLIENT.GUI.Button_.CloseButton(CF);
            this.CloseBtn.Name = "CloseBtn";
            this.CloseBtn.TabIndex = 0;

            this.MaxBtn = new ONTIME_CLIENT.GUI.Button_.MaximizeButton(CF);
            this.MaxBtn.Name = "MaxBtn";
            this.MaxBtn.TabIndex = 1;

            this.MinBtn = new ONTIME_CLIENT.GUI.Button_.MinimizeButton(CF);
            this.MinBtn.Name = "MinBtn";
            this.MinBtn.TabIndex = 2;

            this.RefreshBtn = new ONTIME_CLIENT.GUI.Button_.ReloadButton(CF);
            this.RefreshBtn.Name = "RefreshBtn";
            this.RefreshBtn.TabIndex = 3;

            this.Title = new Label();
            this.Title.Location = new Point(26, 0);
            this.Title.Font = new Font("Calibri", 10, FontStyle.Regular);
            this.Title.AutoSize = false;
            this.Title.Height = 30;
            this.Title.Text = _CF.Text;
            this.Title.BackColor = Color.Transparent;
            this.Title.ForeColor = Color.White;
            this.Title.TextAlign = ContentAlignment.MiddleCenter;

            this.iCON = new PictureBox();
            this.iCON.BackColor = Color.Transparent;
            this.iCON.Location = new Point(5, 5);
            this.iCON.Size = new Size(19, 19);
            this.iCON.Image = THE_ICON;
            this.iCON.SizeMode = PictureBoxSizeMode.StretchImage;

            //this.Invoke((MethodInvoker)delegate
            //{
            //perform on the UI thread
            this.Controls.Add(this.CloseBtn);
            this.Controls.Add(this.MaxBtn);
            this.Controls.Add(this.MinBtn);
            this.Controls.Add(this.RefreshBtn);
            this.Controls.Add(this.Title);
            this.Controls.Add(this.iCON);
            //});
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            DrawControlBox(pe);
        }

        private void Title_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Parent_Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
            }
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            this.Dock = DockStyle.Top;
            this.Height = 30;
            this.Margin = new Padding(0, 0, 0, 10);
            this.Location = new Point(0, 0);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            //this.Height = 30;
            //this.Margin = new Padding(0, 0, 0, 10);
            //this.Location = new Point(0, 0);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                this.Cursor = Cursors.SizeAll;
                ReleaseCapture();
                SendMessage(Parent_Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                Program.Log(Parent_Handle.ToString());
            }
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Cursor = Cursors.Default;
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
                this.Cursor = Cursors.Default;
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);

            int fullwidth = this.Width;
            if (CloseBtn != null)
            {
                int ColseButtonX = fullwidth - CloseBtn.Width - 7;
                this.CloseBtn.Location = new System.Drawing.Point(ColseButtonX, 7);
                int MaxButtonX = ColseButtonX - CloseBtn.Width - 2;
                this.MaxBtn.Location = new System.Drawing.Point(MaxButtonX, 7);
                int MinButtonX = MaxButtonX - CloseBtn.Width - 2;
                this.MinBtn.Location = new System.Drawing.Point(MinButtonX, 7);
                int RefreshBtnX = MinButtonX - CloseBtn.Width - 2;
                this.RefreshBtn.Location = new System.Drawing.Point(RefreshBtnX, 7);
            }
        }

        private void DrawControlBox(PaintEventArgs pe)
        {
            int fullwidth = this.Width;
            int ColseButtonX = fullwidth - CloseBtn.Width - 7;
            this.CloseBtn.Location = new System.Drawing.Point(ColseButtonX, 7);
            int MaxButtonX = ColseButtonX - CloseBtn.Width - 2;
            this.MaxBtn.Location = new System.Drawing.Point(MaxButtonX, 7);
            int MinButtonX = MaxButtonX - CloseBtn.Width - 2;
            this.MinBtn.Location = new System.Drawing.Point(MinButtonX, 7);
            int RefreshBtnX = MinButtonX - CloseBtn.Width - 2;
            this.RefreshBtn.Location = new System.Drawing.Point(RefreshBtnX, 7);
            this.Title.Text = CF.Text;
            this.Title.Width = TextRenderer.MeasureText(CF.Text, this.Title.Font).Width;
            this.Title.MouseDown += Title_MouseDown;

            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(this.Width, this.Height), background_Color2, background_Color1);
            pe.Graphics.FillRectangle(b, ClientRectangle);
            b.Dispose();
        }

        private void ValidateButtons()
        {
            if (!this.CloseIsEnabled && !this.MaximizeIsEnabled && !this.MinimizeIsEnabled)
            {
                this.Controls.Remove(CloseBtn);
                this.Controls.Remove(MaxBtn);
                this.Controls.Remove(MinBtn);
            }
            else if (this.CloseIsEnabled && !this.MaximizeIsEnabled && !this.MinimizeIsEnabled)
            {
                this.Controls.Remove(MaxBtn);
                this.Controls.Remove(MinBtn);
            }
            try
            {
                if (!RefreshIsEnabled)
                    this.Controls.Remove(RefreshBtn);
                else this.Controls.Add(RefreshBtn);
            }
            catch { }
        }
    }


and i assigned a job for each button in control box such as (Close, Maximize and Minimize).
Also i can add more buttons if i want...

but the problem now is, i can ONLY drag the main form (MDI Parent) with the new control box, but i can not move the Childs any more...

What I have tried:

tried to assign the child's to parent using this code

C#
[System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

C#
SINGLE_DISPLAY_FORM frm = new SINGLE_DISPLAY_FORM();
                frm.Show();
                SetParent(frm.Handle, this.Handle);


Now i can move childs, but when i minimize the child form, it`s disappear while it`s still running and i can not retrieve it back to be shown again.

how i can handle this ?
Posted
Updated 7-Feb-18 20:32pm

1 solution

Okay, I fixed this by changing this

C#
SINGLE_DISPLAY_FORM frm = new SINGLE_DISPLAY_FORM();
frm.Show();
SetParent(frm.Handle, this.Handle);


to this

C#
SINGLE_DISPLAY_FORM frm = new SINGLE_DISPLAY_FORM();
frm.MdiParent = this;
frm.Show();


and changing this

C#
protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left)
    {
        this.Cursor = Cursors.SizeAll;
        ReleaseCapture();
        SendMessage(Parent_Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        Program.Log(Parent_Handle.ToString());
    }
}


to this

C#
protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left)
    {
        this.Cursor = Cursors.SizeAll;
        CF.Drag();
    }
}


and added this to customized form class...

C#
public void Drag()
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900