Ever wondered how to put all of your 500-line code into your own control?
Well now there’s a way.
Using the Visual Basic 5 Control Creation Edition or Visual Basic 5 (although the CCE is
free), you can create your own controls. This has been very beneficial to companies such
as Data-Dynamics and Sheridan. They have made a lot of money with their own controls (see
my previous article on custom controls).
Well, in the next two weeks I will show you a simple demonstration of how to make two
different types of controls.
The first will make your program stay ‘on top’. Make a new ActiveX control and add a .bas
module (Project, Add Module). This first piece of code sets some constants and uses an API
call on the ‘user32’ library. Add this code to the module:
Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1 Public Const SWP_NOACTIVATE = &H10 Public Const SWP_SHOWWINDOW = &H40 Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE Public Declare Function SetWindowPos Lib "user32" _ (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _ ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, _ ByVal wFlags As Long) As Long
By default, the control name is ‘UserControl1’. Change its name to
something relevant like ‘OnTop’. Change the CanGetFocus property to False and the
InvisibleAtRunTime property to True. Give it a nice picture and leave the ToolboxBitmap
property to None, unless you feel the desire to give it a meaningless picture!
Open up the code window for the Control, and go to the General
Declarations procedure. This code adds a method statement, which makes the program
‘normal’:
Public Sub MakeNormal(Handle As Long) SetWindowPos Handle, HWND_NOTOPMOST, 0, 0, 0, 0, _ TOPMOST_FLAGS End Sub
Now add this method statement to put the program on top:
Public Sub MakeTopMost(Handle As Long) SetWindowPos Handle, HWND_TOPMOST, 0, 0, 0, 0, _ TOPMOST_FLAGS End Sub
Right, time to compile the control. Click File, Make OnTop.ocx. Now save
the project (File, Save Project).
Click File, New Project. Select a Standard EXE file. Click Project, Components. Click on
the Browse button. Go to the directory where you made the OCX file and double click on it.
Click OK. Draw the control on to the form. Open up the code window for the form. Go to the
form’s Load procedure. Add this code:
Private Sub Form_Load() OnTop1.MakeTopMost (Me.hwnd) End Sub
Or, if you want it to become normal, use:
Private Sub Form_Load() OnTop1.MakeNormal (Me.hwnd) End Sub
Run the Project (F5).
So, you have made your own control which keeps your program on top, but
now you want people to know that it was you that made it. Your claim to fame!
Well, as last time, load Visual Basic and choose "ActiveX
Control" from the New Project dialog. Open up the code window for the control and go
to the General Declarations procedure. Add this code which declares your name and web site
address as a string:
Private mvarauthor As String Private mvarURL As String
Now add these four pieces of code which store and retrieve the settings:
Public Property Let URL(ByVal vData As String) mvarURL = vData End Property Public Property Get URL() As String URL = mvarURL End Property Public Property Let author(ByVal vData As String) mvarauthor = vData End Property Public Property Get author() As String author = mvarauthor End Property
Now add two labels to the control, and change the caption properties on
each one to your name and web site address. e.g.
Lblname.caption="Sam Huggill" LblURL.caption=http://www.soft.net.uk/domain/
Almost finished. Open the code window for the control and go to the
Controls Initialise procedure and add this code:
Private Sub UserControl_Initialize() Me.author = lblName.Caption Me.URL = lblURL.Caption End Sub
Now click File, Make Project1.ocx. Select a directory for the file. Now,
make a new project and click Project, Components. Click browse, go to the directory where
the file is. Double click on the file and click OK. Draw the control onto the form and
look at the properties. See, it works!
If you would like to discuss any of the points touched upon in this
article then please e-mail me at: shuggill@domain.softnet.co.uk
or check my web site at: http://www.soft.net.uk/domain/.
If you would like to subscribe to my FREE weekly VB newsletter then send an e-mail with
the subject line of ‘subscribe’.
Thanks for reading
Sam Huggill