C # form design Learning Record

Source: Internet
Author: User
Tags transparent color
1. Change the size of the form

Windows Forms can change the size at will. However, for some forms with strict requirements, developers do not want users to change the size at will, such as positioning accurate maps and game software. In this case, the window size must be limited. In this example, we design a form with a limited size. Although you can change the size, the size range is limited.

Technical Points
It is very convenient to limit the size in C #. You only need to set the maximum and minimum range of the form. The following describes the relevant attributes.
The form. minimumsize attribute is used to obtain or set the minimum size that the form can be adjusted to. The syntax format is as follows:

Public override size minimumsize {Get; set ;}
Attribute Value: Size, indicating the minimum size of the form.

The form. maximumsize attribute is used to obtain or set the maximum size that the form can be adjusted to. The syntax format is as follows:

Public override size maximumsize {Get; set ;}
Attribute Value: Size, indicating the maximum size of the form.

Implementation Process
(1) create a project. The default form is form1.
(2) Main program code.

Private void form1_load (Object sender, eventargs E)
{
Minimumsize = new size (200,200 );
Maximumsize = new size (400,400 );
}

Summary:

Based on this instance, you can develop the following programs.
1. Specify the size when the form is displayed.
2. Specify the size when running the form.

2. A form with no title bar to change the size

After hiding the title bar of a Windows window, there is only one customer area left in the window, which is a bit like a panel control in the window. Such a window cannot be changed. After the title bar is blocked, the window will also remove the border by default. In this example, a form without a title bar can be created in a special way but its size can be changed.

Technical Points

The window style is determined when the window is created. in C #, there is no title bar but the size of the window can be changed, one clever way is to set the text attribute of the form to null and the controlbox attribute to false. The following describes the relevant attributes.

The controlbox attribute is used to obtain or set a value that indicates whether the control box is displayed in the title bar of the form. Its syntax structure is as follows:

Public bool controlbox {Get; set ;}

Property Value: true if the control box is displayed in the upper left corner of the form; otherwise, false. The default value is true.

Implementation Process

(1) create a project. The default form is form1.
(2) Add the label and button controls in the form1 window to design the interface.
(3) Main program code.

Private void form1_load (Object sender, eventargs E)
{
Controlbox = false;
}

Note: You must set the text attribute of the form to null.

Let alone
Based on this instance, you can develop the following programs.
1. Minimize the display of the form.
2. Maximize when the form is displayed.

 

3. Obtain the desktop size

You can use the API function getdevicecaps to retrieve desktop resolutions. However, the API function has many parameters and is inconvenient to use. How can you conveniently obtain desktop resolutions? In this example, the desktop resolution information is obtained by reading the properties of the screen object, in pixels.
C # provides a screen object, which encapsulates screen-related information. You can obtain the screen information by reading the related properties of the Screen Object,

Screen. primaryscreen. workingarea. width is used to read the desktop width;

Screen. primaryscreen. workingarea. height can read the height of the desktop.

The following describes the relevant attributes.
The screen. primaryscreen. workingarea attribute is used to obtain the display workspace. The work area is the desktop area of the monitor, excluding the taskbar, dock window, and dock toolbar.

Its structure is as follows:

Public rectangle workingarea {Get ;}
Attribute Value: a rectangle, indicating the display workspace.

Implementation Process

(1) create a project. The default form is form1.
(2) Add a button control on the form1 form to get the desktop size. Add two textbox controls to output the obtained desktop size.
(3) Main program code.

Private void button#click (Object sender, eventargs E)
{
Textbox2.text = screen. primaryscreen. workingarea. Height. tostring ();
Textbox1.text = screen. primaryscreen. workingarea. Width. tostring ();
}

Summary
1. Based on this instance, you can develop the following programs.
2. Set the form size and position based on the display resolution information.
3. Adjust the form Interface Based on the display resolution information.

4. Create a text form

We have seen irregular forms. How can we create a text form? Text forms are generally used in screen prompts, such as cash registers.

Technical Points

Previously, creating a font form was a time-consuming and labor-consuming process, involving API calls and a lot of programming work. In the. NET 2.0 Framework, you can easily implement this function without calling APIs. You only need to first draw the font on a graph, then override the onpaint method of the form, re-draw the form with the graph in the method, and set the form to transparent with the background color.

Implementation Process
(1) create a project. The default form is form1.
(2) Main program code.

Namespace specialsharpwindows
{
Public partial class form1: Form
{
Bitmap bit;

Public form1 ()
{
Initializecomponent ();
}

// Set the transparent color of the image. The Code is as follows:

Private void form1_load (Object sender, eventargs E)
{
Bit = new Bitmap ("1.bmp ");
Bit. maketransparent (color. Blue );
}

 

// Rewrite the implementation of the base class method. The Code is as follows:

 

Protected override void onpaint (painteventargs E)
{
E. Graphics. drawimage (image) bit, new point (0, 0 ));
}

Private void label1_click (Object sender, eventargs E)
{
This. Close ();
}
}
}

 

Summary
Based on this example, you can create a circular form \ to create a jagged form.

5. Design a color gradient form

During program design, you can set the backcolor attribute of the form to change the background color of the window. However, after this attribute is changed, the customer area of the entire form will become this color and it is very monotonous. If the customer area of the form can reflect the gradient effect of color like the title bar, the form style will have another flavor. In this example, a color gradient form is designed.

Technical Points

In C #, you can use the color. fromargb () method to return a color. The following describes the method in detail.
The color. fromargb () method is used to return the color value. The syntax structure of this method is as follows:

Public static color fromargb (
Int red,
Int green,
Int blue
)

The parameters are described as follows.
RED: The Red weight of the new color. Valid values range from 0 ~ 255.
Green: the green component value of the new color. Valid values range from 0 ~ 255.
Blue: the blue component value of the new color. Valid values range from 0 ~ 255.
Returned value: The color created by this method.

This function returns a color with three different color values, and slightly adjusting a color value can slightly change the overall color, fill the top and bottom lines in the form with a slightly adjusted color, which will produce a gradient effect as a whole. You can use the graphics object of the form to plot the form. This object can fully manipulate the customer zone of the form.
Note: The color value ranges from 0 ~ In the range of 255.
 
Implementation Process

(1) create a project. The default form is form1.
(2) Add a button in the form1 form to make the color gradient; add a Textbox Control to enter the color RGB value.
(3) Main program code.

The implementation code for triggering the re-draw event is as follows:

 

Private void button2_click (Object sender, eventargs E)
{
Invokepaintbackground ();
This. Hide ();
This. Visible = true;
}

 
The following code re-draws the background color of a form:

Protected override void onpaintbackground (painteventargs E)
{
Int y, Dy;
Y = This. clientrectangle. Location. Y;
DY = This. clientrectangle. Height/256;
For (INT I = 255; I> = 0; I --)
{
Color c = new color ();
C = color. fromargb (convert. toint32 (textbox1.text. tostring (), I, convert. toint32 (textbox2.text. tostring ()));
Solidbrush sb = new solidbrush (C );
Pen P = new pen (SB, 1 );
E. Graphics. drawrectangle (p, this. clientrectangle. X, Y, this. Width, Y + dy );
Y = Y + dy;
}
}

 

Summary
Based on this instance, you can develop the following programs:
1. Set the form to a single color.
2. Use the timer group to dynamically change the color of the form.

 

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.