List of Exercises

How to Create Flat Form in WinForms C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Flat_Form
{
  public partial class Form1 : Form
  {
    //Fields
    private Button currentButton;
    private Random random;
    private int tempIndex;
    private Form activeForm;
    //Constructor
    public Form1()
    {
      InitializeComponent();
      random = new Random();
      closeChild.Visible = false;
      this.Text = string.Empty;
      this.ControlBox = false;
      this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
    }
    [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
    private extern static void ReleaseCapture();
    [DllImport("user32.DLL", EntryPoint = "SendMessage")]
    private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
    //Methods
    private Color SelectThemeColor()
    {
      int index = random.Next(ThemeColor.ColorList.Count);
      while (tempIndex == index)
      {
        index = random.Next(ThemeColor.ColorList.Count);
      }
      tempIndex = index;
      string color = ThemeColor.ColorList[index];
      return ColorTranslator.FromHtml(color);
    }

    private void ActivateButton(object btnSender)
    {
      if (btnSender != null)
      {
        if (currentButton != (Button)btnSender)
        {
          DisableButton();
          Color color = SelectThemeColor();
          currentButton = (Button)btnSender;
          currentButton.BackColor = color;
          currentButton.ForeColor = Color.White;
          currentButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          topbar.BackColor = color;
          logoPanel.BackColor = ThemeColor.ChangeColorBrightness(color, -0.3);
          ThemeColor.PrimaryColor = color;
          ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3);
          closeChild.Visible = true;
        }
      }
    }
    private void DisableButton()
    {
      foreach (Control previousBtn in mainLeft.Controls)
      {
        if (previousBtn.GetType() == typeof(Button))
        {
          previousBtn.BackColor = Color.FromArgb(51, 51, 76);
          previousBtn.ForeColor = Color.Gainsboro;
          previousBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }
      }
    }
    private void OpenChildForm(Form childForm, object btnSender)
    {
      if (activeForm != null)
        activeForm.Close();
      ActivateButton(btnSender);
      activeForm = childForm;
      childForm.TopLevel = false;
      childForm.FormBorderStyle = FormBorderStyle.None;
      childForm.Dock = DockStyle.Fill;
      this.childPanel.Controls.Add(childForm);
      this.childPanel.Tag = childForm;
      childForm.BringToFront();
      childForm.Show();
      label1.Text = childForm.Text.Replace("Form","");
    }
    private void btnCloseChildForm_Click(object sender, EventArgs e)
    {
      if (activeForm != null)
        activeForm.Close();
      Reset();
    }
    private void Reset()
    {
      DisableButton();
      label1.Text = "HOME";
      topbar.BackColor = Color.FromArgb(0, 150, 136);
      logoPanel.BackColor = Color.FromArgb(39, 39, 58);
      currentButton = null;
      closeChild.Visible = false;
    }

    private void topbar_MouseDown(object sender, MouseEventArgs e)
    {
      ReleaseCapture();
      SendMessage(this.Handle, 0x112, 0xf012, 0);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      OpenChildForm(new Forms.softwareForm(), sender);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      OpenChildForm(new Forms.settingForm(), sender);
    }

    private void button4_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
//=======================ThemeColor.cs===========================================
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flat_Form
{
  public static class ThemeColor
  {

    public static Color PrimaryColor { get; set; }
    public static Color SecondaryColor { get; set; }
    public static List<string> ColorList = new List<string>() { "#3F51B5",
                                  "#009688",
                                  "#FF5722",
                                  "#607D8B",
                                  "#FF9800",
                                  "#9C27B0",
                                  "#2196F3",
                                  "#EA676C",
                                  "#E41A4A",
                                  "#5978BB",
                                  "#018790",
                                  "#0E3441",
                                  "#00B0AD",
                                  "#721D47",
                                  "#EA4833",
                                  "#EF937E",
                                  "#F37521",
                                  "#A12059",
                                  "#126881",
                                  "#8BC240",
                                  "#364D5B",
                                  "#C7DC5B",
                                  "#0094BC",
                                  "#E4126B",
                                  "#43B76E",
                                  "#7BCFE9",
                                  "#B71C46"};
    public static Color ChangeColorBrightness(Color color, double correctionFactor)
    {
      double red = color.R;
      double green = color.G;
      double blue = color.B;

      //If correction factor is less than 0, darken color.
      if (correctionFactor < 0)
      {
        correctionFactor = 1 + correctionFactor;
        red *= correctionFactor;
        green *= correctionFactor;
        blue *= correctionFactor;
      }
      //If correction factor is greater than zero, lighten color.
      else
      {
        red = (255 - red) * correctionFactor + red;
        green = (255 - green) * correctionFactor + green;
        blue = (255 - blue) * correctionFactor + blue;
      }

      return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
    }

  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


Unbelievable! Learn to Create a Flat Form in Just 100 Seconds | C# Winform Tutorial


Video Thumbnail