Irregular forms and controls

Source: Internet
Author: User

In earlier versions of Visual Basic or visual C ++, It is very complicated to create irregular forms and controls.
You only need to call a large number of API functions, and the workload is not small. However, in Visual C #, the situation is completely different.
. Using Windows Forms, you can easily create an irregular form and controls on the form.
An application with irregular forms and controls Program It will certainly attract more users, Microsoft's Windows Media
Player 7 shows this point. As a programmer, you must use this technology in your own program.
The forms and controls of a program can be created in a non-traditional way. Ben
Create irregular forms, and create various controls with custom shapes on the forms.
Note: The process of creating irregular forms and controls involves a lot of Graphic programming work.
Different memory and video card may lead to different effects. Therefore, before publishing your application, you must
Perform tests on different types of computers.
Implementation Method
First, create a bitmap file as a program form. Bitmap can be in any shape, but the bitmap file area must be
It must be large enough to include all controls on the form. Then, you can set some attributes to make the Graph
Order Form.
Remove the title bar from the program. Otherwise, the entire interface may appear uncoordinated. Of course, you can remove the title bar.
To maximize, minimize, close, and move the form. To make the program still have these features, we need
Add some Code In this way, the user can still interact with the program as before.
Therefore, you need to do the following:
1. Create a bitmap file as a form.
2. Create a Windows application, use the bitmap file as the program form, and remove its title bar.
3. Add the Code required for the functions of the original title bar.
Procedure
Next I will introduce how to create irregular forms.
Create a bitmap file with irregular shapes
1. You can use any plotting program to create an irregular bitmap. You can use the easiest and most convenient plotting procedure.
.
2. Use a color to draw an irregular area as the form of the program, and use another color to draw the background of the bitmap.
(You Need To make the irregular area large enough .)
3. Save the bitmap file.
The following is an example:
Create a new project in vs.net
First, set the background of the form to create the form shape.
1. Select the form in the Form Designer to get the focus.
2. Make the following settings in the Properties dialog box:
● Set the formborderstyle attribute to none. This property removes the title bar of the program and the title.
Bar, but I will introduce how to add code to restore these functions later.
● Set the backgroundimage attribute to the bitmap file you created. You do not have to add this file to the project because
Once you specify the file, it will be automatically added to the project.
● Transparencykey
Set the property to the background color of the bitmap file (in this example, it is blue)
The background of the bitmap, that is, the blue part of the image, is invisible, and the form is displayed with an irregular elliptical shape.
3. Save the project. Press Ctrl + F5 to run the program. (Note: because there is no title bar, you can use Alt + F4
To close the program)

After the formborderstyle attribute is set to none, the title bar of the program is removed. In this way, in order to obtain the original
You must manually add code to the title bar. Next, I will introduce how to add code to disable the Function
And the function of moving the form.
Close and move forms
1. Drag a button control to the form.
2. In the Properties dialog box, set the text property of the control to "off ".
3. Double-click the button to add a click event handler function.
4. Add the following code to the Code Editor:

Private void button#click (Object sender, system. eventargs E)
{
This. Close ();
}
5. The next step is to implement the form movement function. Add the following code to create a point object (
A variable) determines when to move the form.

Private point mouse_offset;
6. Create an event handler for the mousedown event of the form. After you add code for this event, you can
Where to move the form. The Code is as follows:

Private void form=mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
Mouse_offset = new point (-E. X,-E. y );
}
7. Create an event handler for the form's mousemove event. When the left mouse button is pressed and the mouse is moved,
The location attribute of the form is set to the new position, so that the form is dragged by the user.

Private void form=mousemove (Object sender,
System. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left)
{
Point mousepos = control. mouseposition;
Mousepos. offset (mouse_offset.x, mouse_offset.y );
Location = mousepos;
}
}
8. Save the project. Press Ctrl + F5 to run the program. Now the interface of the program is the same as before, but you can
Move the form with the mouse and press the button to close the form.
Now, we have created an irregular form and implemented some basic mobile forms and closed
However, the button controls on the form are still the same, so the square is positive, making the entire interface not beautiful. Next, I will
This article describes how to create a custom shape control.
Previously we used the transparencykey attribute when creating an irregular form, but the control did not.
So we have to find other methods to implement the irregular shape of the control. Draw a custom shape control on the form

You need to precisely tell the location of the form and how to draw the control. The. NET Framework has the corresponding
Classes and methods help you achieve this, so you don't have to worry about the specific implementation.
The class in. NET Framework provides an instruction to the control, which can determine the shape of the control.
With different instructions, you can draw controls as you want. This indicates that graphicspath is used.
This class represents a series of straight lines and curves used for drawing. First, you must specify a graphicspath class.
And tell it what graphics you want to draw. Then, you set the region property of the control to graphicspath
Class Object. In this way, you can create any custom shape control.
The procedure is as follows:
● Create an Instance Object of the graphicspath class.
● Specify the details (such as size and shape) of the object ).
● Set the region property of the control to the Instance Object of the graphicspath class created above.
Create a text image
1. Drag and Drop a button control to the form.
2. Make the following settings in the Properties dialog box:
● Set the name attribute to custombutton.
● Set the backcolor attribute to a color value different from the background color of the form.
● Set the text attribute to an empty string.
3. Add the form's paint event processing function.
4. Add the following code to draw controls using graphicspath class instance objects. The following code is a string
You can also set the font, size, style, and other attributes of the string. The string is assigned
The instance object of the graphicspath class. Then, the instance object is set as the region attribute of the button control. Such
Custom shape controls are complete.
Private void custombutton_paint (Object sender,
System. Windows. Forms. painteventargs E)
{
// Initialize a graphicspath Class Object
System. Drawing. drawing2d. graphicspath mygraphicspath = new
System. Drawing. drawing2d. graphicspath ();
// Determine a string, which is the shape of the control.
String stringtext = "Click me! ";
// Determine the font of the string
Fontfamily family = new fontfamily ("Arial ");
// Determine the string Style
Int fontstyle = (INT) fontstyle. Bold;
// Determine the string height
Int emsize = 35;
// Determine the start position of the string, which is calculated from the control rather than the form
Pointf origin = new pointf (0, 0 );
// A stringformat object to determine the Character spacing and alignment of a string
Stringformat format = new stringformat (stringformat. genericdefault );
// Use addstring to create a string

Mygraphicspath. addstring (stringtext, family, fontstyle, emsize, origin, format );
// Set the region property of the control to the graphicspath object created above.
Custombutton. region = new region (mygraphicspath );

}
5. Create an event handler for the Click Event of the button. Add the processing function to change the background color of the control.
Verify that the original functions of the control are not removed.

Private void custombutton_click (Object sender, system. eventargs E)
{
Custombutton. backcolor = color. blanchedalmond;
}
6. Save the project and run it.
Further optimization results
The above example uses the graphicspath class to create a button control with a custom shape. But we
It uses a shape in the form of a text string. Can it be a triangle or a circle? The answer is yes.
.. NET framework can provide us with some pre-defined shapes for our use in the program. Run
With this, you can create controls in almost any shape, and you can combine them to use them for greater purpose.
Yes.
The following example uses four ovans. When they are applied to controls, they look like human eyes and are very interesting.
Right.

private void button0000paint (Object sender,
system. windows. forms. painteventargs e)
{< br> system. drawing. drawing2d. graphicspath mygraphicspath = new
system. drawing. drawing2d. graphicspath ();
mygraphicspath. addellipse (New rectangle (0, 0,125,125);
mygraphicspath. addellipse (New rectangle (75, 75, 20, 20);
mygraphicspath. addellipse (New rectangle (120, 0,125,125);
mygraphicspath. addellipse (New rectangle (145, 75, 20, 20);
// change the background color of the button to make it easy to recognize
button1.backcolor = color. chartreuse;
button1.size = new system. drawing. size (256,256);
button1.region = new region (mygraphicspath);

}< br> finally, you have to figure out that the form class is inherited from the system. Windows. Forms. control class. That is to say,
the form provided by the Form Designer is still a control. Therefore, you can use bitmap files to create an irregular
form. You can also use graphicspath class objects to create irregular forms like creating custom shape controls. Readers who are interested in
may wish to try this method.

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.