Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Please if you have a any answers Share it

Thanks & Regards
Manoj
Posted
Updated 19-Feb-15 20:45pm
v2

In WebForms
C#
TextBox.Background = System.Drawing.Color.Red;

In WinForms
C#
txtName.BackColor = Color.Aqua;


If this solves your issue kindly mark this as solution.. Thanks..
 
Share this answer
 
v2
Comments
CHill60 23-Feb-15 9:58am    
Off topic - That changes the back color of a textbox not the control box of the form
I think the easiest way to archive this is to hide the standard controlbox and make your own controlbox made of buttons,and replicate the original controlbox funcionality.

Edit:

As per CHill60 request,i'm improving this a little bit. There are two ways to archive this i think. One is just doing something like this:

C#
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;


And then use a label and three buttons ,put them on top of the form and replicate the close,maximize and restore funcionality.

This is visually not ideal, as you loose the aero glass effect,you can't resize the form and some other disadvantages, but for many things it just do the job.

The other one involves using dwmapi,extending the frame into the client area and then removing the standard frame. This is a lot more advanced, but if you are interested in it i'll try to find some time to make a working example.

Edit 2

Well, this is the other way to do this. You can improve it a lot,but you'll get the idea with this. It's a bit long, so there we go:

C#
public partial class Form1 : Form
    {
        /// <summary>
        /// Obtain a value that indicates whether Desktop Window Manager
        /// (DWM) composition is enabled. 
        /// </summary>
        [DllImport("dwmapi.dll", CharSet = CharSet.Auto, PreserveSig = false,
            SetLastError = true)]
        internal static extern void DwmIsCompositionEnabled(out bool pfEnable);

        /// <summary>
        /// Extend the window frame into the client area.
        /// </summary>
        [DllImport("dwmapi.dll", CharSet = CharSet.Auto, PreserveSig = false,
            SetLastError = true)]
        internal static extern void DwmExtendFrameIntoClientArea(
            IntPtr hWnd,
            [In] ref MARGINS margins);

        /// <summary>
        /// Enable the blur effect on a specified window.
        /// </summary>
        [DllImport("dwmapi.dll", CharSet = CharSet.Auto, PreserveSig = false,
            SetLastError = true)]
        internal static extern void DwmEnableBlurBehindWindow(IntPtr hWnd,
            DWM_BLURBEHIND pBlurBehind);

        

        private Region marginRegion = null;

        /// <summary>
        /// Set the frame border. 
        /// </summary>
        public MARGINS GlassMargins { get; set; }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct MARGINS
        {
            // Width of the left border that retains its size.
            public int cxLeftWidth;

            // Width of the right border that retains its size.
            public int cxRightWidth;

            // Height of the top border that retains its size.
            public int cyTopHeight;

            // Height of the bottom border that retains its size.
            public int cyBottomHeight;

            public MARGINS(int margin)
            {
                cxLeftWidth = margin;
                cxRightWidth = margin;
                cyTopHeight = margin;
                cyBottomHeight = margin;
            }

            public MARGINS(int leftWidth, int rightWidth, 
                int topHeight, int bottomHeight)
            {
                cxLeftWidth = leftWidth;
                cxRightWidth = rightWidth;
                cyTopHeight = topHeight;
                cyBottomHeight = bottomHeight;
            }
                       
        }

        /// <summary>
        /// Specify Desktop Window Manager (DWM) blur-behind properties. 
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        internal class DWM_BLURBEHIND
        {
            // Indicate the members of this structure have been set.
            public uint dwFlags;

            // The flag specify  whether the subsequent compositions of the window
            // blurring the content behind it or not.
            [MarshalAs(UnmanagedType.Bool)]
            public bool fEnable;

            // The region where the glass style will be applied.
            public IntPtr hRegionBlur;

            // Whether the windows color should be transited to match the maximized 
            // windows or not.
            [MarshalAs(UnmanagedType.Bool)]
            public bool fTransitionOnMaximized;

            // Flags used to indicate the  members contain valid information.
            public const uint DWM_BB_ENABLE = 0x00000001;
            public const uint DWM_BB_BLURREGION = 0x00000002;
            public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004;
        }

        static Color transparentColor = Color.DarkTurquoise;

        public Form1()
        {
            InitializeComponent();
            this.GlassMargins = new MARGINS(3, 3, 30, 3);
            this.ControlBox = false;
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.TransparencyKey = transparentColor;
            this.AllowTransparency = true;

            Button b = new Button();
            b.Location = new Point(this.Width - 27, 3);
            b.Size = new Size(24, 24);
            b.Text = "X";
            b.BackColor = Color.Red;
            b.Click += new EventHandler(b_Click);
            this.Controls.Add(b);

            Label lab = new Label();
            lab.Text = "Aero Glass custom Form";
            lab.Location = new Point(3, 9);
            lab.AutoSize = true;
            lab.BackColor = Color.Transparent;
            this.Controls.Add(lab);
        }
               

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

        /// <summary>
        /// When the size of this form changes, redraw the form.
        /// <summary>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);


            using (Brush transparentBrush = new SolidBrush(transparentColor))
            {
                var glassMargins = this.GlassMargins;
                DwmExtendFrameIntoClientArea(this.Handle,
                            ref glassMargins);

                // Make the region in the margins transparent. 
                marginRegion = new Region(this.ClientRectangle);

                marginRegion.Exclude(new Rectangle(
                                this.GlassMargins.cxLeftWidth,
                                this.GlassMargins.cyTopHeight,
                                this.ClientSize.Width - this.GlassMargins.cxLeftWidth - this.GlassMargins.cxRightWidth,
                                this.ClientSize.Height - this.GlassMargins.cyTopHeight - this.GlassMargins.cyBottomHeight));
                e.Graphics.FillRegion(transparentBrush, marginRegion);
            }
        }

        protected override void WndProc(ref Message message)
        {
		    const Int32	WM_NCHITTEST	= 0x84;
            const Int32 WM_NCCALCSIZE = 0x0083;
            
            switch (message.Msg)
            {
                case WM_NCHITTEST:
                    ProcessHitTest(ref message);
                    break;
                case WM_NCCALCSIZE:
                    if (isDwmWindowFramePaintEnabled() && message.WParam != IntPtr.Zero)
                    {
                        message.Result = IntPtr.Zero;
                    }
                    else
                    {
                        base.WndProc(ref message);
                    }
                    break;
                default:
                    base.WndProc(ref message);
                    break;
            }

             
        }

        void ProcessHitTest(ref Message m)
        {
            Point p = this.PointToClient(
                new Point(m.LParam.ToInt32()));

            m.Result = (IntPtr)(p.Y < 30 ?
                UnmanagedMethods.HitTestReturnType.Caption
                : UnmanagedMethods.HitTestReturnType.Client);
        }

        private bool isDwmWindowFramePaintEnabled()
        {
            bool result;
            try
            {
                DwmIsCompositionEnabled(out result);
                return result;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }

        

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (this.marginRegion != null)
            {
                marginRegion.Dispose();
            }

        }

        
    }
 
Share this answer
 
v3
Comments
ManojProfiniti 23-Feb-15 9:58am    
Thanks, Actually i am follow this method but not uses the panel concept...
ManojProfiniti 23-Feb-15 9:59am    
any other possibility to change a color of close button
CHill60 23-Feb-15 10:00am    
Interesting idea ... do you want to propose how the OP could achieve that?
Pikoh 24-Feb-15 4:01am    
See updated solution :)
You should look at "skins" ... there are some suggestions on this post How to change the colour of Windows form control box?[^]
 
Share this answer
 
Use this Code

In WebForms
C#
Button1.BackColor = System.Drawing.Color.Red;


In WinForms
C#
button1.BackColor = Color.Red;



Happy Coding...
 
Share this answer
 
v4
Comments
CHill60 23-Feb-15 9:56am    
Please don't post multiple solutions to the same question. You can use the "Improve Solution" link to add further detail to your solutions

Also off-topic - this changes the backcolor of a Button not the control box of the form

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