Well start our exploration of custom activex controls with ‘a simple example. The first control we’ll develop in this chapter is the FlEXLabel control which is an . enhanced Label control and is shown in Figure 16.1.It provides all the members of the Label control plus a few highly desirable hew features such as the ability to align the text in all possible ways on the control as well as in three-dimensional type.
To implement the FLEXLabel control here’s what we must do:
1.Include all the member of the standard Label control.
2. Add the custom members and provide the code to implement ‘the custom members.
3.Test the control on a standard Form
The FLEXLabeIcontrol is an
enhanced Label control
that can display 3-D text
and align it vertical and . V’ I
horizontally OIJ ~ control
At this point, you’re probably thinking about the code that aligns the text and renders it as carved or raised. A good idea is to start with a standard project, which displays a string on a Form and aligns it in all possible ways. A control is an application packaged in a way that allows-it to be displayed on a Form, instead of on the Desktop. As far as the functionality is concerned, in most cases it can be implemented on a regular Form. Figure 16.2 shows the F Label project’s Form which does exactly what the FLEX Label control will do. You’ll find the project in this chapter’s folder on .” the CD you can open it to see how the code works.
The FLabel project displays
a string in 3-D type and
aligns it vertically and horizontally.
like the FLEX LabeI
control.
I’m not suggesting you st~rtyour cu~tom control~ as Standard EXE applications but for your first few projects this approach will probably help you. Once you get the code that handles the control’s visible interface out of the way you can concentrate 011 the procedures that are specific to the development of ActiveX controls. There’s another good reason I’ve chosen this approach namely to juxtapose the similarities and differences in designing standard applications and custom ActiveX .controls
Start a new Standard EXE project and add. Ute code that aligns a string vertically and horizontally renders a string with 3-D type on. a Form. The code is .lengthy but straightforward. To align the string you must calculate the width and height of the.string in the. current font set the current point on the Form accordingly and then display the string with the Print method. To align a string ” .vertically, you must find out the difference between the Form’s height and the string’s height and then split this difference above and below the string.
To achieve the 3-D effect you must display the same string twice first in white and the in black on top of the while The two strings must be displaced slightly and the direction of the displacement determines the effect (whether the text will appear as raised or carved). The amount of displacement determines the depth of the effect. Use a displacement of 1 pixel for a light effect a displacement of 2 pixels for a moderate effect and a displacement of 3 pixels for a heavy effect. The following code displays the string on the Form, taking into consideration the values of the Form variables m Caption (the string) Alignment(the alignment of the text on the Form), and m_Effect (the special effect that will be applied on the text). The meanings of the possible settings of these variables are listed in the code as comments. Here’s the code that duplicates the functionality of the FLEX Label control on aForm.
Implementing a More Flexible Label Control
Sub DrawCaptionO ”
CaptionWidth’ As Long’,’CaptionHeight As
Dim urrX;As long :CurrY As Lpng
Dim oiciForeColor As OLE_COLOR”
Long
. Capt;onHeight – Me.TextHeight(m_Caption)
CaptionWidth – Me.TextWidth(m_Caption)
Select Case m_TextAlignment
Case 0: ‘ Top-left
CurrX – 30
CurrY 0
Case 1.Top-center
CurrX (Me.ScaleWidth – CaptionWidth) / 2
I CurrY 0 .
Case 2:Top-right
CurrX – Me.ScaleWidth – CaptionWidth – 30
CurrY 0 ‘
Case 3.’Middle-left
CutrX 30
CurrY” (Me.ScaleHeight – (aptionHeight) / 2
Case 4: ‘ Middle-center
CurrX (Me.ScaleWidth -‘CaptionWidth) / 2
CurrY’· (Me.ScaleHeight – CaptionHeight) / 2
Case 5: ” Middle-left ‘
CurrX Me.ScaleWidth – CaptionWidth – 30
CurrY – (Me.ScaleHeight – CaptionHeight) / 2
Case 6: I Bottm-right
CurrX – 30
CurrY – Me.ScaleHeight – CaptionHeight – 45
Case 7: I bottom-center
CurrX ··(Me.ScaleWidth CaptionWidth) / 2
CurrY – Me.ScaleHeight – CaptionHeight – 45
Case 8: I B9ttom-right
CurrX – Me.ScaleWidth – CaptionWidth – 30 .
CurrY – Me.ScaleHeight – CaptionHeight – 45
End Select
oldForeColor – Me.ForeColor
Select Case m_Effect
Case 0: I Plain text
Me.Cls
Me.CurrentX – CurrX
Me.CurrentY – CurrY
Me.Print m_Caption
Case 1: I carved light
Me.Cls
I displace by 15 twi~s (1 pixel) .
Me.CurrentX – CurrX + 15
Me.CurrentY – CurrY + 15
Me;ForeColor – RGB(255, 255, 255)
I and print caption in white
Me.Print m_Caption
I restore original ‘coordinates for current point
Me.CurrentX – CurrX
Me.CurrentY • CurrY
Me.ForeColor – oldForeColor
I and print caption in black
Me.Print.m_Caption .
Case 2: .I Carved medium
Me.Cls
Me.CurrentX – CurrX + 30
Me.CurrentY • CurrY + 30
Me.ForeColor – RGB(255, 255, 255)
Me.Print m_Caption
Me.CurrentX – CurrX
Me.C~rrentY – CurrY
Me.ForeColor – oldForeColor
Me.Print m_Caption
Case 3: I Carved heavy
Me.Cls
Me.CurrentX – CurrX + 45
Me.CurrentY Q CurrY + 45
Me.ForeColor = RGB(255 255 255)
.Me .Pr’int m.:.Caption
~l6 Chapter 16· Building ActiveX Controls
Me.CurrentX – CurrX + 30
Me.Current~ – CurrY +·30
Me.ForeColor RGB(255 255 255)
Me. Pri nt m_Caption
” Me.CurrentX CurrX + 15
Me.CurrentY – CurrY + 15
Me.ForeColor RGB(255 255 255)
Me.Print m_Caption
Me.CurrentX a CurrX
Me.CurrentY – CurrY
Me.ForeColor – oldForeColor’
Me.Print m_Caption
Case 4: I Raised light
Me.Cls
Me.CurrentX CurrX – 15
Me.CurrentY CurrY – 15
Me.ForeColor RGB(255 255 255)
.:Me. Print m_Capti on .
Me.CurrentX CurrX
Me.CurrentY – CurrY
Me.For·eColor oldForeColor
Me.Print m~Caption
Case 5: I Raised.medium
Me.Cls
Me.CurrentX CurrX – 30
Me.CurrentY CurrY – 30
Me.ForeColor RGB(255. 255. 255)
Me.Print m_Caption
Me.CurrentX – CurrX
Me.CurrentY – CurrY
Me.ForeColor ~ oldForeColor
Me.Print m_Caption
Case 6: I Ra1sed heav~
Me.Cls
Me.CurrentX – CurrX – 45
Me.CurrentY – CurrY – 45
Me.ForeColor – RGB(255. 255, 255)
Me.Print m_Caption
Building the FLEXLabelControl &t
Me.CurrentX – CurrX – 30
Me.CurrentY – CurrY – 30
Me!For9lor – RGB(255 255
Me.Print m_Caption
255)
1 Me.CurrentX ~ Cu~rX – 15
Me.CurrentY – CurrY – 15
Me.ForeColor -RGB(255 .255
.Me.Print m_Caption .r
Me..CurrentX – .:CurrX
Me.CurrentY CurrY
Me.ForeColor – oldForeColor “‘ Me.Print·m_Caption
255)
End Select r’
End Sub
The DrawCaption subroutine draws the string, taking into consideration the values of the variables that determine the alignment and special effect. The Draw Cappon() subroutine called from within the Display Caption button’s code:
Private”Sub’ Conrnandl_Click ..
m_Caption – ‘Mastering Visual Basic” .
IlLTex~lignment~ 0 –
IlLEffect – 1
DrawCaption .
End:Sub
This code. sets the values of the Form variables m_Caption, m Text Alignment and m_Effect and then calls the. Draw Caption subroutine to display the caption on the Form ..The Draw caption) subroutine is the core of the FLEX Label control and as you will soon-see we’ll.use.it as is in the control’s code. You might want to .
open the F Label project now and familiarize yourself with the code before pro cascading to build the control.