Windows Forms (i)

Source: Internet
Author: User

Guide

1. What is Windows Forms

2. Need to learn Windows Forms?

3. How to handwriting a simple Windows Forms program

4. Description of the above procedure

5. Form class and Control class

6. Event handling in Windows forms and handwriting a form with a mouse move event

What is Windows Forms

Generally speaking, Windows Forms refers to a class of GUI (graphical user interface) programs collectively, Windows Forms is published along with. NET 1.0, but the Windows Forms programs on the market are mostly in. NET 2.0 or later,. Net 3.5 The GUI program that is pushed by the wpf,windows becomes the 2 line of the Forms.

Need to learn Windows Forms?

The answer to this question varies from person to person, and if it is for students, I recommend skipping Windows Forms WPF,WPF Thinking mode is a lot better than the event handling mode of Windows Forms. For programmers looking for work (or work), this depends on the company's needs, and many companies that do industry software use Windows Form more than WPF, although the interface for WPF is much more than native Windows Forms, but Windows Forms Third-party control libraries compensate for this problem.

How to handwriting a simple Windows Forms program

Directly on the code: FirstWinForm.cs

using System; using System.Windows.Forms; namespace demowinform{    class  app:form    {        static void  Main ()        {            Application.Run (new  App ());     }}}

Compile command: Csc/t:winexe FirstWinForm.cs

Run effect

You can see a simple, easy-to-read Windows Form program running.

Description of the above program

As can be seen from the above code, Windows Forms is a class (in fact, the Windows Forms Form class is a Class), a class to run a main method, the above main method has a Application.Run (new App ()); This seems to be the statement (run) that lets Windows Forms run. Look at the reference again, System.Windows.Forms from this name to see that this should be the assembly of Windows Forms. Okay, so we're officially open to answers.

Windows Forms is any class called form that inherits form

The System.Windows.Forms namespace contains the core content of the Windows Forms program, which includes the core architecture of Windows Forms, visual controls ( Design-time visible and run-time visible), components (both design-time and run-visible components such as ToolTip and components that are not visible at run time only at design time, such as a timer), dialog boxes (common dialog boxes such as OpenFileDialog)

System.Windows.Forms core types in a namespace

1. Application This class encapsulates the Windows Forms application Runtime actions

2. Common control classes (Button, ComboBox, CheckBox, TextBox, Label, DateTimePicker, ListBox, PictureBox, TreeView)

3. Form Forms Class

4. Layout control class (Panel, Splitter, GroupBox, TabControl)

5. Menu controls (menus, MenuItem, ContextMenu)

6. Various dialog boxes

methods to be followed in System.Windows.Forms.Application class
Run () Runs the Windows Forms form
DoEvents () provides the ability of an application to handle the information currently queued in a message queue during lengthy operations
Exit () Terminates the window application and unloads from the hosted application domain
EnableVisualStyles () Configuring the application to support the Windows XP interface appearance

Now let's analyze the above program:

1, the above code is written in an executable program (EXE), so it has a main method.

2, the App class is a form class, because it inherits the System.Windows.Forms.Form class

3, through Application.Run (Windows forms instance); Run to get the Windows Forms program we see

The above code coupling is too strong, the application's run (Create Appdomian) and form logic are coupled together (the form class should focus only on the form logic, not on who will load the form into the AppDomain), so the above code is best changed to the following code:

using System; using System.Windows.Forms; namespace demowinform{    //  Note here I'm using static class statically classes      App    {         Static void Main ()        {            Application.Run (new  mainform ());        }    }          Public class mainform:form{}}

Form class and Control class

Let's take a look at System.Windows.Forms.Form's inheritance diagram.

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form

The red is the class that I think should focus on, all visual controls (such as Button) are inherited from System.Windows.Forms.Control, and the components are inherited from System.ComponentModel.Component.

The System.Windows.Forms.Control class provides a visual control of the vast majority of members (control name, Size, font, foreground and background color, parent container, common events such as mouse-related, keyboard-related, etc.) details see MSDN

Examples of using the control class:

usingSystem;usingSystem.Windows.Forms;usingSystem.Drawing;namespacedemowinform{Static classApp {Static voidMain () {Application.Run (Newmainform ()); }    }     Public classMainform:form { PublicMainForm () {Text="a window"; Height= -; Width= -; BackColor=Color.green; Cursor=Cursors.hand; }    }}

Compiling Csc/t:winexe UseControlDemo.cs

Event handling in Windows forms and handwriting a form with a mouse move event

System.EventHandler
Prototype for
public delegate void EventHandler (Object Sender,eventargs e);
It is the most primitive delegate for WinForm program event handling, sender represents the object that sent the event, E represents event-related information

The related delegate definitions for Windows Forms events are similar to System.EventHandler, such as mouse-related delegate MouseEventHandler, which are prototyped as follows

public delegate void MouseEventHandler (Object Sender,mouseeventargs e)

let's look at the MouseMove event .
All mouse-related events (MouseMove MouseUp, etc.) work with the MouseEventHandler delegate, MouseEventArgs expands the EventArgs to add a bit of property
Button gets which mouse is clicked
Clicks gets the number of times the mouse is pressed and released
x The x-coordinate at the mouse click
Y y-coordinate at mouse click

Look at the code

usingSystem;usingSystem.Windows.Forms;usingSystem.Drawing;namespacedemowinform{Static classApp {Static voidMain () {Application.Run (Newmainform ()); }    }     Public classMainform:form { PublicMainForm () { This. Text ="a window";  This. Height = -;  This. Width = -; BackColor=Color.green; Cursor=Cursors.hand;  This. MouseMove + =NewMouseEventHandler (Mainform_mousemove); }        voidMainform_mousemove (Objectsender, MouseEventArgs e) {             This. Text =string. Format ("current mouse coordinates: [{0},{1}]", E.X,E.Y); }    }}

Compiling Csc/t:winexe MouseMoveDemo.cs

Run effect


End of this article

Windows Forms (i)

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.