ActiveX control related knowledge ~

xiaoxiao2021-03-06  74

October 9, 2004 10:37:56

What is the control do? Http://www.seven-color.com/problem/problem5.htm Control The original function is a function that can perform a certain function. Function [Function] -> OCX [OLE Automata] -> ActriveX [Active Component] function because of universal Sex, depending on person, transplantation. Ole follows a certain agreement. Obser, but the OCX written in various languages ​​is still less compatible .ActiveX is currently a more complete program. In OOP [Object-Oriented Coding], The control is that a set of classes encapsulated, and some attributes and methods can be seen in each class [a collection of functions.

Creation of ActiveX controls: Gu Dai Hung http://vbeden.xg88.com/vbtech/activex/page_1/file4.htm1) Overview ActiveX is a Microsoft term that refers to a group including controls, DLL and ActiveX document components It is usually existed in the form of a dynamic link library, so it must be run in a separate execution software of a container. Such containers include Authorware, Delphi, Visual Basic, Visual C , Internet Explorer, and Access, and more. ActiveX control data input and function functions must pass through the container, so ActiveX controls and containers must support some specific interface protocols. According to Microsoft's corresponding specifications, the ActiveX control should have the following performance mechanisms. Properties and Methods: The ActiveX control must provide the name of the attribute, the name and parameters of the method, and the property parameters of the ActiveX control can be accessed and changed through this mechanism container. Event: ActiveX control is notified by this mechanism to events that occur in the ActiveX control, such as changes in the attribute parameters, and the user presses the left mouse button. Store: Container is notified by this mechanism to the ActiveX control store and extract information data. ActiveX Controls As long as you register in the Windows Registry database, you can play a respective features like other Windows applications. The ActiveX control is a modular flexible object. If an application or page requires an additional special feature, it is not necessary to rewrite the entire program, as long as you flexibly insert an ActiveX control with this feature. The advantage of ActiveX is also its dynamic interactivity, users can dynamically implement their own special requirements by changing its properties and parameters during use. Perhaps readers will ask: There are currently a variety of ready-made ActiveX controls on the market, and there is no need to create an ActiveX control yourself? About this problem is easy to explain, there are many types of existing controls, the function is also complete, and the problem is to encounter this situation: Although the existing control can complete the task, some of the features (attributes, Events or methods) The control is not provided, but also it needs to be implemented by hand-written code, and these features are often used in their own application systems. In order to avoid a lot of repetitive labor, one-time, one-time, one is to create their own ActiveX control. Create an ActiveX control using Visual Basic or other developments. Regardless of which standards, Visual Basic is the most successful computer history (also the most popular) programming language, the most exciting feature is to create users' own controls and can be applied to support ActiveX like other controls. In the application of control technology. By following a specific example, how to perform an ActiveX control under Visual Basic 6.0 (Chinese Enterprise Edition). 2) Steps to create an ActiveX control Using the Visual Basic programming language, you must be very familiar with controls such as TextBox, Label, and Data. To use these controls, they can be drawn in the form, and their behavior is controlled by attributes, methods, and events. When the user creates its own ActiveX control, the user is creating a similar object in addition to the identity, methods, and events. Once your own ActiveX control is created, you can use it in other Visual Basic projects, just like using the TextBox control.

You can use your own controls in any application or development tool that can use the ActiveX control (including other Visual Basic Engineering, Authorware, or Microsoft Internet Explorer). Creating an ActiveX control in Visual Basic is different from creating a Standard EXE application. So when you create a new control, the steps that should generally follow are: (1) Determine the functionality to provide. Because the ActiveX control is similar to a separate object, you need to clarify this object, I hope is what kind of appearance on the screen? What properties, methods, and events do you need to use for application? (2) The appearance of the design control. (3) The interface of the design control, that is, attributes, events, and methods. (4) Creating an engineering group consisting of control engineering and testing projects. (5) The appearance of the control is implemented by adding controls and or code to the UserControl object. (6) Implement the interface and function of the control. (7) Compile control components (.ocx file). Below, follow the steps below to create a DataListView control that can directly display the contents of the database. 3) Specific implementation method (1) Determine the functional standard of the DataListView control There are still some shortcomings when displaying database records. If you must write a lot of code, DataListView makes up for ListView by adding partial features on the ListView. Insufficient, therefore, in addition to its own inherent attributes, methods, and events, add the following members: ◆ DataServerName Properties - Determine the database server. ◆ DatabaseName Properties - Determine the database operated. ◆ AdministratorName Properties - Determine the administrator name of the operation database. ◆ PasswordName Properties - Determine the administrator password for the operational database. ◆ ShowDatabase method - Displays the database operation results executed by the select command. ◆ ERRORDATABASE event - This event is triggered when you encounter a database operation. (2) Design Controls DATALISTVIEW Due to only one ListView control, its appearance does not have much problem that needs to be considered. If the control to be created is a plurality of controls, or the new control does not include any existing control, the appearance is a very important issue. (3) The interface of the design control, that is, the improvements made by the event, events, and methods to the ListView controls are to allow the ListView control to support the content display of the database to use in all applications that can support the ActiveX control. This feature can be implemented by adding user's own properties DataServerName, DatabaseName, AdministratorName, PassERDName, and Method ShowDatabase, etc. Other properties, events, and methods of DataListView are the same as standard ListView. (4) Creating an engineering group consisting of control engineering and testing projects ◆ Start a new ActiveX control project. ◆ Press the Ctrl T key button or select [Project] | [Parts] menu option, display the [Parts] dialog box, select Microsoft Windows Common Controls 6.0 in the [Control] selection box. ◆ Select the [Project] | [Reference] Menu option, display the [Reference] dialog box, select Microsoft ActiveX Data Objects 2.0 Library in the [Available reference] selection box.

◆ Add a listView control in the UserControl window, and the top left corner of the ListView control is 0,0. ◆ Set ActiveX Engineering and UserControl control attribute values, as shown in the following table. Enterprise Settings Engineering Type ActiveX Control Engineering Name Datalv Engineering Description Through ADO, the ListView control can directly act as a database UserControl's Name Property DatalistView UserControl's public property true ◆ ​​Save this project. ◆ Select [File] | [Add Project] menu option. Then add a standard EXE project. The purpose of establishing an engineering is to continuously test when creating an ActiveX control. At this point, an engineering group consisting of control engineering and test engineering has been established, and the DataListView control can be officially started. (5) Implementation of the appearance of the control determines the appearance of the control depends on the creation model of the current control. If you want to create a user drawing control, you must write code in the userControl_paint event process to complete all drawing work, but also need to determine when the control is called to call the UserControl's Refresh method to generate a PAINT event. If it is improved an existing control, simply put the constituent control on UserControl if you correctly correctly. Since the DataListView control only contains a constituted control, just simply draw a standard ListView control on UserControl1, the control is named default ListView1, you can complete the appearance. In order to match the ListView control and your own space when using the control, the UserControl's Resize event process must be established. Code Resize event procedure is as follows: Private Sub UserControl_Resize () ListView1.Left = 0 ListView1.Top = 0 ListView1.Width = UserControl.Width ListView1.Height = UserControl.HeightEnd Sub Resize event procedure only four lines of code is a simple user control All required code of the interface is to make the ListView control and the UserControl objects have the same size. (6) Implementing the interface and function of the control This is the most core, most important and most complex step throughout the creation process. Each property, events, and methods for creating controls are required one by one. ◆ Creating a DataServerName property To create a DataServerName property value, first you need to add a local variable that is internally stored attribute values. To do this, create this variable in the Genaral Declarations generic declaration statement of the UserControl object. As shown below: DIM m_dataservername as String then needs to create new properties called DataServerName, you can create this new property by manually entering the GET and LET, or select [Tool] | [Add Procedure] | [Type] Create this new property. The code for the DataServerName property is quite easy to understand. When the value of the DataServerName property is set, the Property GET process takes only the storage content of the local variable. When setting the DataServerName property value, the Property LET process will give a valid value for local variables.

The following is the code Property two processes: Public Property Get DataServername () As String DataServername = m_DataServerNameEnd PropertyPublic Property Let DataServername (ByVal New_DataServerName As String) m_DataServerName = New_DataServerName PropertyChanged "DataServerName" End Property Note that the Property Let procedure, there A PropertyChanged method, its function is to notify the container (which can be understood as a unit that stores all attributes) The attribute value has been changed, and a WriteProperties event is required to save the new attribute value. In fact, not only in the Property Let process needs to call the PropertyChanged method, and when you change a property value of the ActiveX control in the code module of UserControl, you need to call the process to save the properties. Note Usage of the Property DataServerName method, this method is used with ReadProperties and WriteProperties events. At this point, you need to specify the initial value of this property using the user control: private sub usercontrol_initproperties () m_dataservername = m_def_dataservernameEnd Sub Even if the user does not set the initial value, these code ensures an initial value. As for DatabaseName, AdministratorName, the creation process of the PassERDName property is exactly the same as the DataServerName property, which is no longer repeated here. ◆ Using the PropertyBag object users need to create code for WriteProperties and Readproperties events to protect DataServerName, DatabaseName, AdministratorName, the attribute value at the design phase. Both events use the Propertybag object to save and retrieve the value of DataServerName, DatabaseName, AdministratorName, and passordname properties. The Propertybag object can keep DataServerName, DatabaseName, the design value of the PassERDName.

Specific codes are as follows: Private Sub UserControl_ReadProperties (PropBag As PropertyBag) m_DataServerName = PropBag.ReadProperty ( "DataServerName", m_def_DataServerName) m_DataBaseName = PropBag.ReadProperty ( "DataBaseName", m_def_DataBaseName) m_AdministratorName = PropBag.ReadProperty ( "AdministratorName", m_def_AdministratorName) m_PasswordName = PropBag.ReadProperty ( "PasswordName", m_def_PasswordName) End SubPrivate Sub UserControl_WriteProperties (PropBag As PropertyBag) Call PropBag.WriteProperty ( "DataServerName", m_DataServerName, m_def_DataServerName) Call PropBag.WriteProperty ( "DataBaseName", m_DataBaseName, m_def_DataBaseName) Call PropBag.WriteProperty ( "AdministratorName", m_AdministratorName, m_def_AdministratorName) Call PropBag.WriteProperty ( "PasswordName", m_PasswordName, m_def_PasswordName) End Sub Because of these two processes is for the "container" object, so save and read through all the property values ​​of the two The process is implemented, rather than each attribute requires a separate process. Among them, PropertyBag is the name of the "container object". The WriteProperty method has three parameters: the first string identifier requires saved properties, the second parameter is the value that needs to be saved, and the last parameter is the default value of the attribute. The ReadProperty method requires two parameters: a string is used to save the name of the property, and the other is the default value. When drawing the ActiveX control on the form, you will start executing the code of the ActiveX control. In the control design process, the default value DataServername, DataBaseName, AdministratorName, PasseordName property to: Const m_def_DataServerName = "lyc" Const m_def_DataBaseName = "pubs" Const m_def_AdministratorName = "sa" Const m_def_PasswordName = "" Of course, also possible to program Modify it multiple times when running. The normal behavior of the control is to restore its default values ​​when the program terminates, which increases the requirements of the two independent states of the main attribute. In short, if you change a property value during the design phase, the control must get this new value, not the default value.

Conversely, if the property value is changed at runtime, the control must retrieve this property value when the design status is returned. The PropertyBag object allows the ActiveX control to store its own properties, so that it can perform this action. The PropertyChanged method will notify the user that it has changed a property. By understanding the status of the program and whether the PropertyChanged method is called, VB can inspire WriteProperties and Readproperties events. ◆ Write the code showdatabase method for the showdatabase method to display the database operation results executed by the select command in the ListView control. A data access method of ADO (ActiveX Data Objects) is used during its specific implementation. The main feature of ADO is more easier, the access speed is faster, and the requirements for disk and storage capacity are smaller, and ADO supports building a variety of client / server patterns and web-based applications, with remote data service RDS (Remote Data Service The characteristics, through RDS to transfer server-side data to the client's application or web page, and update the server-side data immediately after the client is processed. The OLE DB technology based on ADO can access all kinds of data such as email, text file, data table, by unified interface API interface, which is a major development direction of remote data access.

ShowDataBase method has a string parameter, without any return value, the specific code as follows: Public Sub showdatabase (ssql As String) Dim AdoDatabase As New ADODB.ConnectionDim AdoTable As New ADODB.RecordsetDim scnn As StringDim response As StringDim I As IntegerOn Error GoTo errorhandlescnn = "Provider = SQLOLEDB; Data Source =" & Trim (m_DataServerName) & "; Initial Catalog =" & Trim (m_DataBaseName) & "; User Id =" & Trim (m_AdministratorName) & "; Password =" & Trim (m_PasswordName) & ";" AdoDatabase.Open scnnAdoTable.CursorType = adOpenKeysetAdoTable.LockType = adLockOptimisticAdoTable.CursorLocation = adUseClientAdoTable.Open ssql, AdoDatabase,,, adCmdTextIf AdoTable.BOF And AdoTable.EOF Thenresponse = MsgBox ( "no record Found! ! ", vbOKOnly vbInformation," database control ") AdoTable.CloseSet Adotable = NothingAdoDatabase.CloseSet AdoDataBase = NothingExit SubEnd IfListView1.ColumnHeaders.ClearListView1.ListItems.ClearDim clmX As ColumnHeaderFor I = 0 To AdoTable.Fields.Count - 1Set clmX = ListView1 .Columnheaders.add () with clmx.text = adotable.fields (i) .nameend with NextDim itmX As ListItemAdoTable.MoveFirstDo While Not AdoTable.EOFSet itmX = ListView1.ListItems.Add () With itmXIf IsNull (AdoTable.Fields (0) .Value) Then.Text = "NULL" Else.Text = AdoTable.Fields (0) .Valueend ifend withfor i = 1 to adotable.fields.count - 1IF isnull (adotable.fields (i) .value) THENITMX.SUBITEMS (I) = "null" elseitmx.subitems (i) = adotable.fields (i). ValueEnd ifXTadotable.movenextLooplistView1.View = lvwreportadotable.closset adotable = Nothingadodatabase.closset AdoDatabase =

转载请注明原文地址:https://www.9cbs.com/read-109154.html

New Post(0)