From 0 self-taught c#01– self-painted form border

Source: Internet
Author: User
The main introduction is the self-painted form border and background color progressively lighter (deep).

1. Set the form to no-border mode first

This. FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

You can also right-click the form, select Properties, and find Formborderstyler set to none. The minimize, maximize, and Close buttons in the upper right corner of the form disappear, and the function of the mouse drag form disappears, which is added in the 4th step.

2. Draw a border

Select the current project right-click, add components, let the component class Component1 inherit the panel class, and then override OnPaint (PaintEventArgs e) to draw the border color. Build the solution, the component is automatically added to the toolbox, under the component bar. 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, B                          Uttonborderstyle.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 do not know what RGB value to set, make the color look good. You can download a screen color picker on the Internet to read the RBG value of the color you think is good.

3. Add a border

Add the component Component1 to the form, setting its Dock property to fill. Then set the Padding property to 2,2,2,2 (corresponding to the border size). 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 close, minimize, drag the mouse function

On the form component this.component11, add a panel, set its Dock property to top, a custom background color of 0,102,171, and a foreground color of transparent.

This.panel1.Dock = System.windows.forms.dockstyle.top;this.panel1.backcolor = System.Drawing.Color.FromArgb ((int) (((Byte) (0))), ((int) (((102))), ((int) (((Byte) (171)))), This.panel1.ForeColor = System.Drawing.Color.Transparent;


The

Then adds two labels on pannel, setting its text to __ and X, respectively, to minimize and maximize the icon. Font is set to Microsoft Black, bold, small fifth. Finally, add the MouseDown and MouseMove events to Pannel. Add the Click event to the label, add the MouseEnter and MouseLeave events, and the mouse changes to hand when you move the mouse over the icon.

Point mouseoffset;private void Panel1_mousedown (object sender, MouseEventArgs e) {mouseoffset = new Poi        NT (-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.Panel background color set to gradual fade

Adds a paint event to pannel.

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 ();                Call the FromArgb method of the Color object                c = Color.FromArgb (i + +),//0,102,171                solidbrush sb = new SolidBrush (c); 
  
   pen p = new Pen (SB, 1);                Draws a rectangle                e.graphics.drawrectangle (p, this.panel1.clientrectangle.x, Y, this. Width, y + dy);                y = y + dy;                i++;            }                }
  

The above is from 0 self-c#01– self-painted form border content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.