This section
discusses some Windows related specific that we need to know to
create Hide To Desktop. These are namely some essentials, so necessary
for any at least a bit advanced graphic operations, that you must
be very concentrated and try to remember everything:
Each window
is assigned a window handle when
it is initially created and this handle is used by Windows to
refer to this window any time some kind of operation has to be
performed over it. This handle is a Long
VB type number, unique for the system. To get the handle of your
form (window), you use the Hwnd property.
Each window
outlook is represented by a Device Context
(DC) attached to it on its creation. Any drawing operations
on a given window are performed using its DC as identifier. The
DC is also a Long VB type number.
You may retrieve it either by the VB property hDC
or by using the API function GetDC
specifying the window handle as a parameter.
The DC of
each window is actually holding the image displayed on the screen
at the position of this window. If your window is partly overlapped
by another one, the DC of your window will contain also the part
of the other window. Also, if your window is partly or completely
hidden off the screen, these hidden parts are represented on the
DC with pure black color.
The Windows
desktop is nothing else, but a window. And as the DC contains
everything that's over it, the DC of the Desktop window is actually
a snapshot of your screen, updated 50-100 times per second. So,
you can count on it to get whatever part of the screen you need.
There is such
thing called a compatible (sometimes
called memory) DC. This is a virtual
DC created in memory and often used as an auxiliary drawing area.
By the way, it's much faster to draw on such DC and then transfer
its contents at once onto a visual DC, than just drawing on this
visual DC. A compatible DC is called "compatible", because
it must be compatible with some existing visual DC. And again,
"compatible" means it will have the same color setting
and other attributes as the visual DC. And you don't need to care
much about these attributes.
To create
a compatible DC, you call CreateCompatibleDC
API function and specify the visual DC as a parameter.
A surprise!
- a DC is not just a canvas to draw on. It is actually a set of
objects needed for drawing. These objects include a pen, a brush,
a bitmap and some more. It is actually the bitmap you draw on
when using the DC. And if you draw a circle on a DC, you will
draw it on the DC's bitmap, using the DC's pen as a drawing tool
specifying the color and width of the circle's stroke. You will
only need the bitmap object for this example, as there is no drawing
in it.
Now, if you
create a DC using CreateCompatibleDC, it will have no bitmap loaded
into it. So, you cannot use it. And it means you need to load
a bitmap in it. But where to get the bitmap from?
Ok, there
is such thing called compatible (memory) bitmap. It is, as the
compatible DC is, a virtual representation of a bitmap. And "compatible"
means the same as above. To create a compatible bitmap, you call
CreateCompatibleBitmap API and specify
a DC that the bitmap will be compatible with. You should avoid
using a compatible DC as a parameter, instead, use the DC you
specified as a parameter when creating the compatible DC.
When you create
a compatible bitmap, it is not yet loaded into any DC. To do so,
you call SelectObject API (also used
for loading other objects, like pens, into a DC). You specify
the DC as a first parameter and the handle of the object as second.
Because it
takes some time for a window to be redrawn (Windows does not keep
a copy of the old desktop contents in memory, instead it just
sends a message to the programs covering it that they should redraw
their windows where needed), we need to call RedrawWindow
API function to make sure the desktop area we need will be redraw.
This is actually the weakest point of this sample - if you run
it over Photoshop picture, the window will desappear, then Photoshop
will take some time to draw its picture and then the sample window
will appear again and "fade out".
And lastly,
you use BitBlt function to copy a
given part of a DC onto another DC. It is maybe the most used
function for drawing and if you are not familiar with it yet,
you will surely be soon. Ok, it is a fast and easy function.
Sorry for
the above paragraph isn't the last (I lied to you). After you
have finished using a given compatible DC, you should free the
resources it uses by calling DeleteDC
API function. It takes just the DC handle as a parameter. The
DC of your window is automatically deleted by VB itself.