| I am studying the Microsoft. NET Framework and do not quite understand the differences between controls and components. I know these terms can be used with each other, but when will they be derived from Control and Component? Linda Berno
Good question! In short, controls are components with user interfaces. To be specific, You have to review the historical root cause of early Windows. At that time, the control refers to static text in any subwindow-buttons, list boxes, edit boxes, or a dialog box. In terms of concept, these windows-controls-are similar to the knob and buttons used to operate radio or small appliances. As the number of controls increases (for example, a combo box or a date-time control), controls gradually become synonymous with subwindows, whether used in a dialog box or in other types of main windows. It wasn't long before BASIC programmers began to write their own controls, and naturally people thought of sharing them. One way to share code is to copy data through a disk, but it is obviously inefficient. There must be a mechanism for developers to easily insert controls in applications of other programmers. This is the motivation for VBA controls, OLE controls, OCX and finally ActiveX controls.
This is where the control and components are confused. To solve the control reusability problem, all these technologies must first solve the more general Component reuse problem. (COM, if you still remember it, it means the Component Object Model ). In the language of software, the term component refers to any reusable object or any code body that can interact with other objects. The invention of subprograms once became the holy grail of Software Engineering for programmers: a unified programming theory, it enables programmers to build large systems from basic build blocks, that is, various components written in the selected language. Every new programming example from the evolution of subroutines to OOP, to DLLs, to COM, and to the. NET Framework represents a different solution that provides reusability. VBX uses the DLLs curing name. COM and IUnknown .. NET Framework uses Microsoft's intermediate language (MSIL) layer and Common Language Runtime (CLR) to provide unified bonding.
Therefore, a widget is a major sample of a widget (and has driven component development in History). A widget is not just a unique component. Components do not need to display any information or user interfaces. The component may implement scientific computing, collect performance data, calculate the number of milliseconds from January 1, 1971 to the present, or read the number of dollars in the Bush campaign coffee. Figure 4 shows examples of non-control components in Visual Studio. NET.
Figure 4 Component
In the. NET Framework, term controls and components assign special significance to. NET. The Component class provides basic implementation for design-level objects such as Windows Forms Designer or Web Forms Designer. A Component is anything that can be dragged to a form. The Component class implements IComponent, ISite, and IContainer. These interfaces are much simpler than the COM cousin from the OLE period. IContainer is a little more complex than the list of components with the Add/Remove Method and component attributes. The component it obtains is a ComponentCollection ).
IComponent is derived from IDisposable and has only one attribute, Site, and gets the ISite interface of the component. Component may or may not have a Site. ISite has four attributes, including Name and DesignMode. It controls whether the component is in. What else can it be? -- Design mode. ISite is derived from another interface, IServiceProvider, which has only one method, GetService. In COM, IServiceProvider is similar to QueryInterface -- it can be used to query the interface of an object through ID, but unlike QueryInterface, this interface is not implemented by the object itself, it only knows where and how to get it. Similarly, in the. NET Framework, IServiceProvider is a service that gets a common method for other interfaces or objects-service-an object that is known without implementation.
The. NET Framework makes it easy to compile reusable components without the need for IDL, Type Definition Language, and effort-consuming design support. Through the reflection magic, CLR knows everything from the Code itself, and all classes are under control. To add design support, you only need to mark your attributes with an additional designer. For example, in hosting C ++:
// in CMyControl [Category(S"Appearance")] [Description(S"Specifies widget foreground color.")] _property Color get_ForeColor() { ... } _property void set_ForeColor(Color value) { ... }
Now the Form Designer lists your ForeColor attributes in the "Appearance" (Appearance) and uses the help Description ). For more information about design-Time Attributes, see "design-Time Attributes of components" in. NET Framework documentation"
Figure 5 levels
Figure 5 shows the class hierarchy in the. NET Framework, which can illustrate the issues discussed above. As you can see
Derived from Component. This is another way to describe controls as components (otherwise ). More specifically, a widget is a component that uses the user interface to draw and interact with users. The Control class is also the base class of all managed window classes-forms, buttons, raster, panel, toolbar, and so on. The Control class defines WndProc and ClientSize and all standard window events such as GotFocus and Click. The Web Control (System. Web. UI. Control) is also a Component, but in a strict sense, it is not derived from System. ComponentModel. Component. (For Web controls, the namespace is System. Web. UI, and Control itself implements IComponent .)
In addition to implementing IComponent, System. ComponentModel. Component also provides column set support for all components, but it is implemented by deriving from MarshalByRefObject. To generate a value column set component, You can derive from MarshalByValueComponent (which implements IComponent, IDisposable, and IServiceProvider ). System. Data. DataColumn, DataSet, and able are examples of value column set components. These objects transmit actual data across machine/process boundaries.
If you are writing other people who can also drag to their forms using the Form Designer reusable widgets, you must derive from Component. If your widget also has a user interface-you can create a window, draw a painting, or interact with the user-you should derive from Control. Do you understand?
Ask Paul a question and comment and send it to cppqa@microsoft.com.
|