Asp. NET interactive bitmap Form design (7)

Source: Internet
Author: User
Tags contains visual studio
Asp.net| Interaction | Design to use a drawing object in a Windows forms application
We've talked about drawing object classes, and here's how to use these classes in Windows forms applications. First, let's talk about how Windows forms applications work.

The main part of a Windows forms application
A simple Windows Forms application contains a main window (or form) that contains child items of a control. If you're a Visual Basic programmer, you'll find this model very familiar.

Main Window
The key objects in any Windows forms application are the main window. The form will be created in the application's static/shared Main method, as shown below.

In a simple Windows forms application, such as what we write, all other controls are children of this main form.

Buttons and text boxes
Our form has a set of buttons and some text boxes. Each button has a handler that you can add shapes to the list and draw a list. Contains text boxes that show you how to get input from a form. There is also a group box that provides a visual indication of the text box and related buttons.

PictureBox
On the left is the most important control: PictureBox. This is 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, the window will need to be redrawn when it is displayed again.

This on-demand drawing is done in response to a paint (Paint) message, which is handled by an event handler in the parent Form window class.

Main routines in Windows forms applications
Let's take a quick look at the important routines in Windows forms applications. Note that the code for the user interface is very brief compared to the code that draws the object. This is the benefit of doing a lot of work with the. NET Framework. (This also shows that the work we do with the drawing object class is really good.) )

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

It also contains declarations of the data we need (DShapeList and a random number generator object), Main, and Event handlers for button click events and PictureBox paint events.

Main
Main's task is to create and run the main Window object. Its C # code is shown below.


C#

[STAThread]
static void Main ()
{
Application.Run (New MainWindow ());
}


The STAThread property is important for the Windows forms application's main-you should always use it so that depending on the functionality of the OLE automation (such as drag-and-drop and clipboard) works.

In Microsoft Visual Studio? This method is not found in the generated Visual basic. NET source code, but if you use ILDASM to find in the. exe, you will find a Main that is the same as the functionality described above--probably generated by the Visual Basic. NET Compiler.

InitializeComponent
Under Windows Form Designer generated code (which is generated by the Windows Forms Designer) (if you can't see the code in this area, click the Small plus sign), you see the code that creates and initializes all the buttons and other controls on the form.

Data declaration/random number generation
In addition to all the controls declared in the hidden area of your code, we need to declare two variables: The data structure that holds the drawing list, and an object of the Random type. We use the Random object to generate a random number for the location of the object being created.

The data declaration is located within the MainWindow class, but is outside any method. In C # and Visual Basic. NET, the code looks like this:


C#
DShapeList drawinglist = new DShapeList ();
Random Randomgen = new Random ();


Visual Basic

. NET Dim Drawinglist as New dshapelist ()
Dim Randomgen as New Random ()


We also wrote a helper method to get a random point:


C#
Private Point Getrandompoint () {
Return to New Point (Randomgen.next), Randomgen.next (30, 320));
}

Visual Basic

. NET Private Function getrandompoint () as Point
Return to New Point (Randomgen.next), Randomgen.next (30, 320))
End Function


It generates two random numbers located between 30 and 320, as the coordinates of the 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.