This section
will give a full explanation of each part of the code. A fragment
of the code will precede the explanation:
Dim
CDC As Long, DTWND As Long, DTDC As Long
Dim CBMP As Long, frmWidth As Long, frmHeight As Long
Dim PrevX As Long, PrevY As Long
Dim TempDC As Long, TempBMP As Long
These are
the declaration of some variables used. Here is what each of them
means:
CDC |
compatible
DC to store the background behind our window |
DTWND |
the
handle of the Desktop window |
DTDC |
the
DC of the Desktop Window |
CBMP |
the
compatible bitmap to be loaded into CDC |
frmWidth |
the
width of our form in pixels |
frmHeight |
the
height of our from in pixels |
PrevX |
the
X position of our from in pixels |
PrevY |
the
Y position of our form in pixels |
TempDC |
a
temporary DC to store our form outlook |
TempBMP |
a
temporary bitmap to select into TempDC |
If
Val(Text1.Text) = 0 Then Text1.Text = "3"
If Val(Text2.Text) < 1000 Then Text2.Text = "15000"
Just a simple
textbox contents check.
frmWidth
= Form1.Width \ Screen.TwipsPerPixelX
frmHeight = Form1.Height \ Screen.TwipsPerPixelY
Calculating
the width and height of our form in pixels and storing the values
for a future use in variables.
DTWND
= GetDesktopWindow
DTDC = GetDC(DTWND)
CDC = CreateCompatibleDC(DTDC)
CBMP = CreateCompatibleBitmap(DTDC, frmWidth, frmHeight)
Call SelectObject(CDC, CBMP)
TempDC = CreateCompatibleDC(DTDC)
TempBMP = CreateCompatibleBitmap(DTDC, frmWidth, frmHeight)
SelectObject TempDC, TempBMP
Retrieve the
handle of the Desktop Window and store it into a variable. Then
get its DC and again store it. Next, create the compatible DC,
compatible with the desktop, create a compatible bitmap with the
size of our window and compatible with the Desktop and load it
into the compatible DC. Then create the temporary compatible DC
to hold our form's outlook, create a bitmap for it and load it.
Pic.Top
= 0
Pic.Left = 0
Pic.Width = Me.Width
Pic.Height = Me.Height
PrevX
= Me.Left \ Screen.TwipsPerPixelX
PrevY = Me.Top \ Screen.TwipsPerPixelY
Position the
picture box to cover the whole window and then calculate the current
left and top position in pixels and store into variables.
Me.Move
Me.Left - 100000
TempRect.Left = PrevX
TempRect.Right = PrevX + frmWidth
TempRect.Top = PrevY
TempRect.Bottom = PrevY + frmHeight
RedrawWindow DTWND, TempRect, ByVal 0&, RDW_ERASE
DoEvents
BitBlt CDC, 0&, 0&, frmWidth, frmHeight, DTDC, PrevX,
PrevY, SRCCOPY
Me.Move Me.Left + 100000
Hide our window,
ensure it is hidden by calling DoEvents, redraw the rectangular
part of the desktop behind "us" and then copy the contents
of the desktop at the place of our window into our compatible
DC. Then return the window to its original position.
DoEvents
BitBlt TempDC, 0&, 0&, frmWidth, frmHeight, DTDC, PrevX,
PrevY, SRCCOPY
Ensure the
form has returned to its position and then copy the Desktop contents
on the place of our form into a temp memory DC. This contents
is actually the outlook of our from.
Pic.ZOrder
0
Pic.Visible = True
BitBlt Pic.hdc, 0&, 0&, frmWidth, frmHeight, TempDC,
0&, 0&, SRCCOPY
Pic.Picture = Pic.Image
Put the picture
on the top of all controls, show it and then copy the contents
of the temp memory DC onto it. Then set .Picture property to .Image
property to prevent losing the current contents.
Pic.AutoRedraw
= False
Dim I As Long, tX As Long, tY As Long, SqSize As Long
SqSize = Val(Text1.Text)
Randomize Timer
For I = 1 To Val(Text2.Text)
tX = Rnd(5) * frmWidth
tY = Rnd(7) * frmHeight
BitBlt Pic.hdc, tX, tY, SqSize, SqSize, CDC, tX, tY, SRCCOPY
Next I
Set AutoRedraw
to False to prevent the picture to be reset to its old contents.
Then init a random numbers generation and copy random squares
from the memory DC containing the background behind our window
onto the picture box.
DeleteDC
CDC
DeleteDC TempDC
Unload Me
Free resources
used by CDC and TempDC and end the program.