Creating ActiveX Controls
Creating ActiveX Controls
ActiveX controls are reusable UI components packaged as .ocx files. They can be used in VB6 forms, Internet Explorer web pages, Office documents, and any ActiveX container.
Creating a Custom Progress Bar
- Start a new ActiveX Control project in VB6.
- The project creates a
UserControl. This is the design surface for your control. - Add a
PictureBoxnamedpicBar(the filled portion) and set itsBackColortovbBlue.
' UserControl code: FlameProgressBar
Option Explicit
Private m_lngValue As Long
Private m_lngMax As Long
Public Property Get Value() As Long
Value = m_lngValue
End Property
Public Property Let Value(ByVal lngNew As Long)
If lngNew < 0 Then lngNew = 0
If lngNew > m_lngMax Then lngNew = m_lngMax
m_lngValue = lngNew
RedrawBar
PropertyChanged "Value"
End Property
Public Property Get Max() As Long
Max = m_lngMax
End Property
Public Property Let Max(ByVal lngNew As Long)
If lngNew < 1 Then lngNew = 1
m_lngMax = lngNew
RedrawBar
PropertyChanged "Max"
End Property
Private Sub UserControl_Initialize()
m_lngMax = 100
m_lngValue = 0
End Sub
Private Sub UserControl_Resize()
RedrawBar
End Sub
Private Sub RedrawBar()
If m_lngMax = 0 Then Exit Sub
Dim pct As Double
pct = m_lngValue / m_lngMax
picBar.Move 0, 0, UserControl.ScaleWidth * pct, UserControl.ScaleHeight
End Sub
' Persistence
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_lngMax = PropBag.ReadProperty("Max", 100)
m_lngValue = PropBag.ReadProperty("Value", 0)
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Max", m_lngMax, 100
PropBag.WriteProperty "Value", m_lngValue, 0
End Sub
Compiling and Using
- Go to File → Make .ocx. This compiles and registers the control.
- In a Standard EXE project, go to Project → Components and check your control.
- It appears in the Toolbox. Drag it onto a form and set
ValueandMaxproperties.
Internet Explorer Usage
ActiveX controls can be embedded in web pages using the <OBJECT> tag. The control must be signed with Authenticode and marked safe for scripting.