C # language Part IV graphical interface programming (iii) Subform __ Programming

Source: Internet
Author: User
Tags static class win32

Let's take a look at the inheritance relationship of the form class first:

Figure 1 Form class inheritance diagram

In fact, Microsoft from the beginning of the development of Windows, has adopted an object-oriented approach (in the C language simulation of object-oriented), so to later C++,vb,delphi, can easily use an object-oriented language to the Windows Forms this part of the code to encapsulate.

The same is true of. NET, which still calls the API functions in the WIN32 SDK internally to generate the form and start the message loop. So the. NET Form class is a so-called "heavyweight" component. That is, these classes communicate directly with the operating system, invoking the interface functions provided by the operating system, rather than relying solely on the virtual machine and the run-time environment.

The MarshalByRefObject class provides the ability for remote proxies for a form (that is, you can cross the boundaries of a domain through the MarshalByRefObject class, allowing Windows messages to be delivered to the. NET environment, which are described later in detail).

The component class defines a component that can receive messages, and can be encapsulated into objects and distributed to different methods.

The control class defines the basic appearance and behavior of the component (such as size, position, color, font, and so on, as well as basic message response methods such as mouse movement, mouse down, mouse release, and so on), and the control class holds this important property-the form handle, which is invoked by calling Win32 The API function of the SDK CreateWindowEx obtained. So you can assume that the control class defines an area that can receive messages and can be displayed .

From control, a branch is generated, and theScrollableControl class specifies a form class that contains content that can be larger than the form's bounds , which produces a form object that has scroll bar properties. You can change the position of the content in the form by dragging the scroll bar.

Forms that do not require scroll bars inherit the control class directly, such as buttons (Button Class), Single-line text boxes (textbox classes), Labels (label classes).

The ContainerControl class contains a Controls collection property that is represented as a " container " that can accommodate other form classes inherited from the control class.

The form class defines a " top-level container ", that is, the form can contain other forms, but it cannot contain itself.

All forms outside the form class must be contained within a container window. So form is the most basic container form in the entire form application.

In popular, the object of the form class is called form, and the objects of other forms classes are called "subform" or " control ."

There are many types of controls, hundreds of them, which cannot be described in our course. Can only be explained roughly: no matter which kind of control, are derived from the control class, so the controls have appearance attributes, such as the height, width, color, style, border, layout, display text, image displayed, and so on, which have a method of message handling. The form object responds to the message by two kinds: 1, inheriting the control class, overwriting its message processing method, 2, using the message corresponding to the delegate event to process the message; These two points are consistent with the method described in the previous section.

Here we just take one example to introduce the basic usage of the control.

Program.cs

1 Using System;
2 Using System.Windows.Forms;
3
4 Namespace Edu.Study.Graphics.SubWindow {
5
6 <summary>
7 Declares a class that inherits from the button, representing a button
8 </summary>
9 public class Mybutton:button {
10
11 A reference to a text box control
12 Private textbox textbox;
13
14 <summary>
15 Construction device
16 </summary>
17 Public MyButton (textbox textbox) {
18
19 Set the width and height of a button
20 This. Width = 120;
21st This. Height = 90;
22
23 Set the text displayed on a button
24 This. Text = "Hello";
25
26 This. TextBox = textbox;
27 }
28
29 <summary>
30 A reference to a text box control contained in a button
31 </summary>
32 Public textbox TextBox {
33 get {
34 return this.textbox;
35 }
36 set {
37 if (value = = null) {
38 throw new ArgumentNullException ("TextBox");
39 }
40 This.textbox = value;
41 }
42 }
43
44 <summary>
45 Overwrite the CreateControl method in the superclass to handle the control creation message
46 This method is executed when the control is created
47 </summary>
48 protected override void Oncreatecontrol () {
49 Base. Oncreatecontrol ();
50
51 The Parent property represents the container object that contains the control, which is the form type Object
52 Sets the position of the button so that it is centered inside the parent container
53 This. top = (this. Parent.height-this. Height)/2;
54 This. left = (this. Parent.width-this. Width)/2;
55 }
56
57 <summary>
58 Click Message to Process button
59 This method is executed when the button is clicked by the mouse
60 </summary>
61 <param name= "E" ></param>
62 protected override void OnClick (EventArgs e) {
63 Base. OnClick (e);
64
65 Display a message dialog box
66 MessageBox.Show ("Hello");
67 The contents of the text box control should be hello
68 This. TextBox.Text = "Hello";
69 }
70 }
71
72
73
74 Static class

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.