The Skeleton of the AdiveX Control

Before adding the custom code, you must understand what the wizard did for you. Switch to the Project Explorer window and double-click the name of the control to open it in design mode. Then, double-click the User Control to open the code pane  and see the lines inserted by the wizard

The ActiveX Control

Private m_Caption As String
Pr~vate m_Effect As Integer
Private m_TextAlignment As Integer
‘Default Property Values:
Const m_def_caption ‘3D Label’
Const mLdef_Effect – 2
Const mLdef_ Text Alignment – 4
‘Property Variables:
‘Event Declarations:
Event Dbl Click O
Event Click O

Private Sub UserControl_DblClick()
RaiseEvent DblClick
End Sub.
Pr~vate Sub UserControl_Click()
RaiseEvent Click
~nd Sub
Public Property Get Enabled() As Boolean
Enabled – UserControl.Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
UserControl.Enabled() – New_Enabled
Property(hanged “Enabled”
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor – UserControl.ForeColor
End Property
Public Property Let ForeColor(ByVal New_For-eColor As OLE_COLOR)
UserControl.ForeColor() – New_ForeColor
PropertyChanged “ForeColor”
End Property

Public Property Get hOC() As long
hOC – UserControl.hDC
End Property
Public Property Get hWnd() As Long
hWnd – UserControl.hWnd
End Property
Private Sub UserC~trol_KeyUp(KeyCode As Integer Shift As Integer)
RaiseEvent ~eyUp(KeyCode Shift)
End Sub
Private Sub UserContro’_KeyPress(KeyAscii As Integer)
RaiseEvent KeyPress(KeyAScii)
.End Sub
Private Sub User Control_KeyOown(KeyCode As Integer Shift As Integer)
Raise Event Key Down(KeyCode, Shift)
End–Sub

Public Property Let TextAlignmentCByVal New_TextAlignment As Integer)
m_TextAlignment – New_TextAlignment
PropertyChanged “TextAlignment”
End Property
‘Initialize Properties for User Control
Private Sub Us~rControl_InitPropertiesC)
Set Font – Ambient.Font
m_caption – m_def_Caption
m_Effect – mLdef_Effect
m_Text Alignment – m_def_Text Alignment
User ContMOl.Border Style – 1
User ContrQl.Back Style – 1
End’Sub

Load property values from storage
Private Sub User Control_Read Properties C Prop Bag As Property Bag)

Set Font· Prop Bag.ReadPropertyC”Font” Ambient.Font)
UserControl.BorderStyle – PropBag.ReadProperty(“BorderStyle” 0)
UserCont,rol.BackStyle ‘. PropBag. ReadPropertyC “BackStyl e”  1)
UserControl.Enabled – PropBag.ReadPropertyC”Enabled” True)
UserControl.For~olor – PropBag.ReadPropertyC”ForeColor” _
&ij&OOOO012) UserControl.MousePointer – PropBag. ReadPropertyC “MousePointer”  0)
UserContr.ol.OLEDropMode PropBag.ReadPropertyC”OLEDrOpMode” 0) .
Set Picture  Prop bag.ReadPropertyC”Picture” Nothing)
m_Caption  Prop Bag.ReadPropertyC”Caption’, m_def_Caption)
m_Effect· Prop Bag.Read PropertyC “Effect” , m_def_Effect)
mLTex tAlignrnent – Prop Bag.ReadPropertyC”TextAlignment” _
m_def_TextAlignment)
UserControl.BackColor PropBag.ReadPropertYC”BackColor” _
&H8000000F)
End Sub

write property values to storage
privet sub user control _write PropertiesCProPBag As.PropertyBag)
callProPbag Write Property(” Font I Font Ambienr Font)
CallPropB~.WritePropertyC’BordeStyle” UserContro1.BorderStyle
CallPropBag.WritePropertYC”BagkStylel UserCOntrol.BackStyle
Call PrOpBag.WritePropertyC” Enabl end”  UserControl.Enabled, True)
Call ‘PropBag ~Wri tePropertyC I ForeCo lor ” UserContro 1 ForeCo lor
&H800000U)
Pr4PBag.W~itepropertYC”MousePointer
userControl.MousePointer 0) ‘
Call PropBag.WriteProperty(‘OLEDropMode’ _
UserControl.OLEDropMode 0)
Call PropBag.WriteProperty(‘Picture’ Picture ~othing)
Call PropBag.Writeproperty(‘Caption’ m_Caption ~
m_def_Caption)
Call PropBag.WriteProperty(‘Effect” m_Effect, _
m_def_Effect)
Call PropBag.WriteProperty(‘TextAlignment’ _
m_TextA 1ignment m_.def_TextA 1ignment)
Call PropBag.WriteProperty(‘BackColor’ _
UserControl.BackColor &H8000000F)
End Sub

WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES
‘MappingInfo-UserControlUserControl-l BackColor
Public Property Get BackColor() As OLE_COLOR
BackColor – UserControl.BackColor
End Property
_ ‘Chapter 16. Building ActiveX Controls
Call PropBag.WriteProperty(‘OLEDropMode’ _
UserControl.OLEDropMode, 0)
Call PropBag.WriteProperty(‘Picture’ Picture ~othing)
Call PropBag.Writeproperty(‘Caption’ m_Caption ~
m_def_Caption)
Call PropBag.WriteProperty(‘Effect” m_Effect _
m_def_Effect)
Call PropBag.WriteProperty(‘TextAlignment’_
m_Text Alignment m_.def_TextA 1ignment)
Call Prop Bag.Write Property(‘BackColor’ _
UserControl.BackColor &H8000000F)
End Sub
Public Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
UserControl.BackColor() – New_BackColor
PropertyC:Ii’anged’BackColor’
End Property

It’s quite lengthy but not.as complicated as it appears. Let’s look at each section of the code in detail, starting with the declaration section:

Option Explicit
Default Property Values:
Private m_Caption As String
Private m_Effect As Integer
Private m_TextAlignment As Integer

These variables will hold the values of the control’s custom properties. The control’s properties are mapped to private variables in the control’s code because that’s  what they are. As is the-case with an ActiveX component, what the applications perceive and access as properties from the outside are actually plain variables in the control. Later you’ll see how the control gets the values entered by the user in the Properties window (or the code at run time) and assigns them to these private variables. (Do you remember how you were manipulating the properties of your own  OLE server in the previous chapter with the Property Let and Property Get procedures? The same approach works with ActiveX controls. But more on this later.)

Following are the definitions of a few constants that correspond to the values we specified in the set Attributes window of the Wizard. These constants will be used later in the code as initial values for various properties. Notice that you don’t have to run the WlZal’d to change these values. You can easily edit the control’s code. Notice also that the names of the constants are based ‘on the actual property names and that you can easily edit the code.

‘Default Property Values:
Const m_def_Caption – ‘3D label’
Const m_def_Effect  2
Const m_def_Text Alignment – 4

The event declarations follow. These are the events we specified in the first two windows of the Wizard and we mapped them to the User control object. Clicking on·the custom control generates a Click event which is reported to the application . as if it were generated by the ActiveX control. There are more event definitions in . the listing but I need not repeat them here

Event Dec1arati. ens
Event DblClickO .
Event ClickO
Event.KeyUp(Key codeAs Integer Shift As Integer) .’
Event K~yP~ess(~eyAscii AsrI~teger)
Event K~YoOWnc’KeyCode As Inte.ger Shift As Intege.r) .
Event MouseUp(Button.As Integer, Shift As Integer _
X As Single Y As Single)
Event Mouse Nove(Button As Integer Shift As Integer _
X As Single. Y As Single)

NOTE

If the Click event weren’t mapped to the User Control object only the User Control .would .see the click  event. If you want to perform a special action when the control is clicked.you program the Click event in the custom controls Code window. The user won’t be able to program the Click event unless you raise the Click. event . in the control’s code.

In our custom control we don’t have any exclusive use for the Click event (and the other common and keywords}board events) so we are exposing them to the application that uses the ActiveX control.

Scroll to Top