Visual C # is an important component of the Microsoft. Ner FrameWork and a new generation of programming languages strongly recommended by Microsoft. WinForm is a title for Windows Form on the. Net development platform .. Net provides a wide range of Class libraries for WinForm applications ). These WinFrom class libraries support RAD (Fast Application Development). These class libraries are encapsulated in a namespace, Which is System. Windows. Forms. Many classes are defined in this namespace. When developing a. Net-based GUI application, we inherit and extend these classes so that our program has a variety of user interfaces. This article attempts to use the most basic class-Form to describe the specific process of developing the WinForm program with Visual C.
I. Development and running environment and General Introduction
All debugging programs in this question are based on Microsoft Windows 2000 Professional Edition and. Net FrameWork Beta 2.
II. The first WinForm
If your machine has reached the running environment required by our program, open a text editor
Copy the program code to the editor and save it as the first. cs file. Use the following compilation statement:
Csc/t: winexe/r: system. dll first. cs
After compilation. Run the program and you will see the following interface:
Source code: first. cs
Using System;
// Import the WinForms namespace
Using System. Windows. Forms;
// Class Form1 inherits and extends the class Form in the System. Windows. Forms namespace
Public class Form1: Form
{
Public static void Main ()
{
// Run the program
Application. Run (new Form1 ());
}
}
Summary:
1) First, use the "using System. Windows. Forms" statement to import the WinForm namespace.
2). Declare the Form1 class. This class inherits and extends the Form class in the using System. Windows. Forms namespace.
3). "Application" class, which is also defined in the using System. Windows. Forms namespace. Because of this class of closed, we cannot inherit anything. The "Application" class is mainly used to run and exit Windows applications and process Windows messages. Calling the "Run" method of the "Application" class indicates that the Application will start to Run.
3. Make a transparent WinForm
When I first saw a transparent form in Windows 2000, it would be very difficult to make such a form. You must call Many API functions. After being exposed to. Net, you can see how easy it is to Use VisualC # To make a transparent form. You only need to set a value. Next let's take a look at what a transparent form is generated using the following code.
Source code of the transparent form: second. cs
Using System;
Using System. Windows. Forms;
Using System. Drawing;
Public class Form2: Form
{
Public static void Main ()
{
Application. Run (new Form2 ());
}
Public Form2 ()
{
This. Location = new System. Drawing. Point (100,100 );
This. Cursor = System. Windows. Forms. Cursors. Hand;
// Defined on the form, the cursor is displayed as a hand
This. Text = "transparent WinForm! ";
// Define the title name of the form
This. StartPosition = System. Windows. Forms. FormStartPosition. CenterScreen;
// Define that the start position of the form is in the middle of the screen
This. FormBorderStyle = System. Windows. Forms. FormBorderStyle. Fixed3D;
// The boundary of the form is Fixed3D.
This. ForeColor = System. Drawing. SystemColors. Desktop;
// Use the foreground color of the desktop as the foreground color of the form
This. Font = new System. Drawing. Font ("", 9 );
// Define the font type and size
This. BackColor = System. Drawing. Color. Blue;
// Define the background color as blue
This. ClientSize = new System. Drawing. Size (440,170 );
// Set the size of the form
// The Opacity attribute sets the transparency of the form, which is only valid for Windows 2000
This. Opacity = 0.60;
}
}
Summary:
The above code is similar to the Code in the first example, but some attributes are defined in Form2 Class.
1). "this" keyword. I think everyone has noticed this keyword, So how should we understand it. For example: When I introduce myself (actually defining my attributes), I will say "my name is xx" and "my age is xx ", "My mailbox is xx "...... You may notice the word "my", which means me, Wang Tian. In programming, the "this" keyword is an instance pointing to an object. In the above Code, "this. Font" and "this. Text" are the attributes of the currently running Form2 instance.
2). Let's take a look at the above Code and import a namespace-System. Drawing in the program. Through the namespace defined class, you can better design the object and process the color and size.
3) The following table describes the specific meaning of the attributes set up in the above program.
Attribute description
Location: the Location where the WinForm is initialized, that is, when the application is running, a fixed position of WinFrom is displayed.
Cursor displays the Cursor status when the Cursor is on WinForm
Set the WinForm title in Text
The StartPosition attribute is somewhat similar to the "Location" attribute. The "Location" attribute defines the absolute position of WinForm, and this attribute defines the relative property of WinForm. The values of this attribute are defined as "CenterScreen", "Manual", "invalid owsdefaultlocation", "WindowsDefaultBounds", and "CenterParent"
FormBorderStyle defines the border style of the form. It is usually set to "FixedSingle", "Sizable", "FixedDialog", "FixedToolWindow", and "SizableToolWindow"
ForeColor defines the foreground color of the form
Font defines the Font type and size on WinForm.
BackColor defines the background color of the form.
ClientSize defines the WinForm size.
The Opacity attribute defines the transparency of WinForm and can only be used in Windows 2000. The zone value of the attribute is 0-1, which indicates that it is transparent to opacity.
4. Create a WinForm with a component
This example describes how to add a component to WinForm. If you want to add any components to the form, you must first initialize the component (see the following program to initialize the Label ). And Add it to the form using the "Controls. Add" method. The following is the program running interface and source code.
Source code: three. cs
Using System;
Using System. Windows. Forms;
Using System. Drawing;
Public class Form3: Form
{
// Define a tag
Private Label label1;
Public static void Main ()
{
Application. Run (new Form3 ());
}
// Construct
Public Form3 ()
{
// Create a tag and initialize it
This. label1 = new System. Windows. Forms. Label ();
// Inherit a Label class first
Label1.Location = new System. Drawing. Point (24, 16 );
// Set the Label display position
Label1.Text = "this is a label in WinForm ";
Label1.Size = new System. Drawing. Size (200, 50 );
// Set the label size
Label1.TabIndex = 0;
Label1.TextAlign = System. Drawing. ContentAlignment. MiddleCenter;
// Set the alignment of tags
This. Text = "add a label to WinForm! ";
This. StartPosition = System. Windows. Forms. FormStartPosition. CenterParent;
This. AutoScaleBaseSize = new System. Drawing. Size (8, 16 );
This. FormBorderStyle = System. Windows. Forms. FormBorderStyle. Fixed3D;
// Set the boundary type of the form
This. ForeColor = System. Drawing. SystemColors. Desktop;
This. Font = new System. Drawing. Font ("", 10, System. Drawing. FontStyle. Bold );
// Set the font and font size.
This. SizeGripStyle = System. Windows. Forms. SizeGripStyle. Hide;
This. BackColor = System. Drawing. Color. Blue;
// Set the background color
This. ClientSize = new System. Drawing. Size (250,250 );
// Add the tag to the form
This. Controls. Add (this. label1 );
}
}
V. Summary
The preceding section focuses on developing WinForm applications using Visual C. system. windows. froms namespaces encapsulate many Class (classes) related to interface design ). This article only introduces a basic Class-Form to understand the powerful functions of Visual C # And the rich development resources of. Net Class Library. This article also shows that to fully utilize the powerful functions of Visual C #, you must understand and master. Net Class Library. Only by mastering the. Net Class Library can you develop a. Net program with powerful functions and stronger vitality.