ASP. NET interactive bitmap Form Design (7)

Source: Internet
Author: User

Use plotting objects in Windows Forms applications
We have discussed the object class that can be drawn. Next we will talk about how to use these classes in Windows Forms applications. First, let's talk about how Windows Forms applications work.

Windows Forms applications
A simple Windows Forms application contains a main window (or form) that contains the control subitem. If you are a Visual Basic programmer, you will find this model very familiar.

Main Window
Key objects in any Windows Forms application are the main window. This form will be created in the static/Shared Main method of the application, as shown below.

In a simple Windows form application (such as the one we wrote), all other controls are subitems of the main form.

Button and text box
Our form has a set of buttons and text boxes. Each button has a handler that can add shapes to the list and draw a list. The text box contains how to obtain input from the form. There is also a grouping box that provides visual instructions on text boxes and related buttons.

PictureBox
The left side is the most important control: PictureBox. This is the position where the image is drawn and displayed. In a Windows application, you may need to redraw the image at any time-for example, if the window is minimized or overwritten by another window, re-paint is required when the window is displayed again.

This type of on-demand drawing is completed in response to a painting message, which is processed by an event handler in the parent form window class.

Main routines in Windows Forms applications
Let's take a brief look at the important routines in Windows Forms applications. Note that the code on the user interface is very short compared to the Code on which the object can be drawn. This is the benefit of using the. NET Framework to do a lot of work. (This also indicates that we do a good job of using the object class that can be drawn .)

Form Method
The Form (or main window) is derived from System. Windows. Forms. Form, so it inherits all its actions. All these controls are declared as members of this class, so that they will also be cleaned up (the cleanup is actually done explicitly in the Dispose method ).

It also contains the declaration of the data we need (DShapeList and a random number generator object), Main, and the event handler for button-clicking events and PictureBox drawing events.

Main
The Main task is to create and run the Main window object. Its C # code is as follows.


C #

[STAThread]
Static void Main ()
{
Application. Run (new MainWindow ());
}


The STAThread attribute is very important to the Main of a Windows Forms Application-you should always use this item so that the function of OLE Automation (such as drag-and-drop and clipboard) can work properly.

In Microsoft Visual Studio? The generated Visual Basic. NET source code does not find this method, but if you use ILDASM in. find a Main that has the same function as the preceding one -- possibly by Visual Basic.. NET compiler.

InitializeComponent
Under Windows Form Designer generated code (code generated by Windows Form Designer) (if you cannot see the code in this area, click the plus sign ), the code used to create and initialize all buttons and other controls on the form is displayed.

Data Declaration/Random Number Generation
In addition to all controls declared in the hidden area of the code, we also need to declare two variables: the data structure of the drawing list, and a Random type object. We use the Random object to generate a Random number for the location of the created object.

The data declaration is in the MainWindow class, but is outside of any method. In C # and Visual Basic. NET, the Code is as follows:


C #
DShapeList drawingList = new DShapeList ();
Random randomGen = new Random ();


Visual Basic

. NET Dim drawingList As New DShapeList ()
Dim randomGen As New Random ()


We also compiled a helper method to get a random vertex:


C #
Private Point GetRandomPoint (){
Return new Point (randomGen. Next (30,320), randomGen. Next (30,320 ));
}

Visual Basic

. NET Private Function GetRandomPoint () As Point
Return New Point (randomGen. Next (30,320), randomGen. Next (30,320 ))
End Function


It generates two random numbers between 30 and 320 as the coordinates of random points.

Related Article

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.