Mainly introduces the gradual lightening (darkening) of the self-drawn form border and background color.
1. First set the form to borderless mode
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
You can also right-click the form, select Properties, find FormBorderStyler and set it to None. At this time, the minimize, maximize, and close buttons in the upper right corner of the form will disappear, and the function of dragging the form with the mouse will also disappear. These will be added in step 4.
2. Draw the border
Select the current project, right-click, add a component, let the component class Component1 inherit the Panel class, and then override OnPaint(PaintEventArgs e) to draw the border color. Generate a solution and the component will be automatically added to the toolbox under the components column. The class code is as follows:
public partial class Component1 : Panel { public Component1() { InitializeComponent(); } public Component1(IContainer container) { container.Add(this); InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.FromArgb(00,59,96), 2, ButtonBorderStyle.Solid, Color.FromArgb(00,59,96), 2, ButtonBorderStyle.Solid, Color.FromArgb(00,59,96), 2, ButtonBorderStyle.Solid, Color.FromArgb(00,59,96), 2, ButtonBorderStyle.Solid); base.OnPaint(e); } }
Color.FromArgb(00,59,96) sets the border color, 2 sets the border size.
Note: If you don’t know what rgb value to set to make the color look good. You can download a screen color picker online and read out the RBG value of the color you think looks good.
3. Add a border
Add component Component1 to the form and set its Dock property to Fill. Then set the Padding property to 2, 2, 2, 2 (corresponding to the border size). It can also be implemented dynamically. The code is as follows:
this.component11.Dock = System.Windows.Forms.DockStyle.Fill; this.component11.Padding = new System.Windows.Forms.Padding(2);
4. Add the form closing, minimizing, and mouse dragging functions
On the form component this.Component11, add Panel and set its Dock The attribute is Top, the custom background color is 0, 102, 171, and the foreground color is Transparent.
this.panel1.Dock = System.Windows.Forms.DockStyle.Top; this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(102)))), ((int)(((byte)(171))))); this.panel1.ForeColor = System.Drawing.Color.Transparent;
Then add two labels on the Panel and set their Text to __ and X respectively, which are the minimize and maximize icons. The font settings are Microsoft YaHei, Bold, and Small 5. Finally, add the MouseDown and MouseMove events to the Panel. Add a Click event to the label, and add MouseEnter and MouseLeave events so that when the mouse moves over the icon, the mouse changes to a hand shape.
Point mouseOffSet;private void panel1_MouseDown(object sender, MouseEventArgs e) { mouseOffSet = new Point(-e.X, -e.Y); }private void panel1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point mousePosition = Control.MousePosition; mousePosition.Offset(mouseOffSet.X, mouseOffSet.Y); Location = mousePosition; } }private void labelMinimize_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; }private void labelClose_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); Application.Exit(); }private void labelMinimize_MouseEnter(object sender, EventArgs e) { this.Cursor = Cursors.Hand; }private void labelMinimize_MouseLeave(object sender, EventArgs e) { this.Cursor = Cursors.Default; }private void labelClose_MouseEnter(object sender, EventArgs e) { this.Cursor = Cursors.Hand; }private void labelClose_MouseLeave(object sender, EventArgs e) { this.Cursor = Cursors.Default; }
5. Set the background color of the Panel to gradually fade
Add a Paint event to the Panel.
private void panel1_Paint(object sender, PaintEventArgs e) { int y, dy; y = this.panel1.ClientRectangle.Location.Y; dy = this.panel1.ClientRectangle.Height / 90; for (int i = 0; i <= 89;i++ ) { Color c = new Color(); //调用Color对象的FromArgb方法 c = Color.FromArgb(50, i + 50, i + 120);//0,102,171 SolidBrush sb = new SolidBrush(c); Pen p = new Pen(sb, 1); //绘制矩形 e.Graphics.DrawRectangle(p, this.panel1.ClientRectangle.X, y, this.Width, y + dy); y = y + dy; //i++; } }
The above is the content of self-study C#01 from 0-self-painted form border. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!