Win32 Window creation
By: Jack Hoxley
Written: May 2000
When VB creates us a form it
is basically simplifying the use of the MFC (Microsoft Foundation Classes) which
C++ developers tend to use. This translation can be costly to our application.
In C++ it is taken that the MFC shouldn't always be used in time critical programs;
the same can be applied to us, but with something extra. Because VB translates
everything for us it will take a little while for this translation to take place;
which could be a little extra speed for our program. As well as this translation
we are also using the MFC - effectively two potential slowdowns.
This makes it sound like using
forms is a bad thing. For my argument to be of any use to you you'll have to
be picky. I'm not saying that you'll see any great speed advantages; at best
2-5 fps on some computers. On top of this, you cant use controls so easily,
and you can't add things into the forms events (such as Form_load and Form_Click)
- which can be a real pain for some people. There is one last slight advantage;
VB seems to compile smaller and possibly faster (a negligable increase) EXE.
Especially when you use the method below.
Using no forms will only really
be of advantage to the perfectionist, and those with a little more patience
than the rest of us. Using this method is only really useful when it comes to
using fullscreen graphics (DirectDraw or Direct3D) - as these dont rely heavily
on controls or other items that forms offer us.
My method; and the way that this
tutorial will demonstrate is through an ActiveX-VB DLL. This way we'll have
all the form creation code (which is quite big) seperate of our main program
- which makes the EXE simpler (see above). Putting this code into a DLL has
two main advantages:
- Simplicity. It makes your
master project (the EXE) much easier to look at. You could have 1000 lines
of code in your project for creating a window; or you could link this DLL
to your project and use it with 3-4 lines of code. It also makes your pogram
easier to understand for other people, and yourself when you come back to
it 6 monthes later. Which would you prefer?
- Reusabilty. Once this DLL
has been written, debugged, checked, error corrected and compiled it can be
used again and again. Everytime you want to create a window, just add a reference,
stick a few lines of code in and off you go - nowhere near as complex as searching
your hard drive for an odd module file; attaching it to your project etc...
Also, if it is completely stable and has been thoroughly tested you KNOW it
will work first time and every time.
The sample file included is very basic - it uses very little of the potential
provided by the Win32 Libraries. It is, however, perfectly suitable for use
as a DirectDraw container window. With this you can create a complete DirectDraw
project with one Module in your project. There are three very important things
you must bare in mind first:
- DirectXevent. You can no longer
use the DirectXEvent if you use this library. This isn't down to the way it
is programmed; just a general side effect of using the Win32 Libraries. The
DirectXEvent must be declared in a form object and cant be declared ina module
or a class - therefore, if this DLL replaces the form object we cant declare
- Stability. There were numerous
stability issues when developing this DLL - although it seems to work fine
all the time now I cant garauntee that it will be completely error free. Use
it at your own risk; although if you do you're unlikely to get any errors.
- Using DirectInput. Because
you no longer have access to the Form_KeyDown, Form_MouseDown (etc...) events
you won't be able to stop your program using user input. You must use DirectInput
(or the API equivelent) to get input from your user to close the program.
Having said all those bad things;
try it - you may well find it to be useful. You can download it fromt he top
of the page, or from the downloads page.