A very handy application that's included with the Windows operating system is the magnifier, which zooms in on a portion of the desktop. In this article Jason shows us how to create a cool zoomable magnifier using Visual Basic 6 and the Windows API.
Magnifying The Desktop With Visual Basic - Adding the code (Page 3 of 6 )
Although there isn't a lot of code to add to our project, the code that we will add makes extensive use of the Windows API and bit blitting, which copies an image from one source to another.
We will now step through the code that we need to add to make our magnifier function. Add the following four API declarations to the "General Declarations" section of your form (alternatively, you can download the entire app as part of our support material at the end of the article):
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
A device context (DC) is a resource that you borrow from Windows to paint, draw, or print to a form. GetDC returns the device context of a window or other device when given the object's handle. ReleaseDC frees up system resources used by a call to GetDC.
StretchBlt is used to copy a section of an image from one device to another, and the BitBlt function performs a block transfer of a rectangular portion of an image from one device to another.
All Windows API calls are written in extremely efficient C code, and the "Declare Function" method allows us to create a wrapper function that Visual Basic will map internally to the specified Windows API call.
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
The GetDesktopWindow function returns a handle to the desktop window, which is the area that makes up the actual desktop part of the Windows operating system. We use this handle in combination with a call to the ReleaseDC function to free the resources occupied by the call.
GetCursorPos accepts a POINTAPI type (which we will look at in a minute) and returns the x-y co-ordinates of the mouse relative to the Windows desktop. Lastly, the GetDeviceCaps function can be used to get the value of a particular capability of a device, such as how many colors it can display, whether or not it can draw lines and circles, etc.
Dim frmH As Long
Dim magnify As Integer
Private Const HORZRES = 8
Private Const VERTRES = 10
Private Const RDW_ERASE = &H4
Private Const RDW_INVALIDATE = &H1
Private Const SRCCOPY = &HCC0020
Private Const WM_PAINT = &HF
Private Type POINTAPI
X As Long
y As Long
Next we declare several constants that will be passed to our API calls, as well as one type, POINTAPI, which is passed to the GetCursorPos to retrieve the x-y co-ordinates of the mouse.
That's all the code we need in the general declarations section of our form. The only other place we need to add code is to the timer. Let's do that now.