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.
As mentioned earlier, the GetCursorPos function gets the x-y co-ordinates of the mouse cursor into a POINTAPI type. We capture the co-ordinates into a POINTAPI type named cp, and set the text of the status bar to reflect the mouse's current position on the desktop:
dsDC = GetDC(0)
Next, we use GetDC to get a device context to the current window. We tell the function we want a DC to the current window by passing it the value of zero.
'Get screen width, height
hr = GetDeviceCaps(dsDC, HORZRES)
vr = GetDeviceCaps(dsDC, VERTRES)
We use the GetDeviceCaps function to get the horizontal and vertical resolution of the screen and save them into two variables.
Because we will be copying a part of the desktop image, we get a handle to it using the GetDesktopWindow handle. We also get the available width and height of our form onto which we can bit blit this image. By getting the dimensions of our form dynamically, we can change the size of our form without worrying whether or not the magnified image will display properly.
If cp.X - offsetX >= 0 And cp.X + offsetX < hr Then
If cp.y - offsetY >= 0 And cp.y + offsetY < vr Then
We use several if statements to determine whether or not the cursor is within a valid range of the desktop, and if it is then we use the StrechBlt function to copy an area of the desktop onto our form.
The StretchBlt function takes eleven parameters. StretchBlt's function signature and the details of each of these parameters are shown below:
Declare Function StretchBlt Lib "gdi32.dll" (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 hSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
hdc: The device context of the target object
x: The x co-ordinate to start the image pasting from
y: The y co-ordinate to start the image pasting from
nWidth: The width of the image as it will be pasted into the target window
nHeight: The height of the image as it will be pasted into the target window
xSrc: The x co-ordinate to start the image copying from
ySrc: The y co-ordinate to start the image copying from
nSrcWidth: The width of the image being copied
nSrcHeight: The height of the image being copied
swRop: The type of raster operation to use when copying the image from one window to another. In our example we have used SRCCOPY, which has a value of &HCC0020 and tells windows that we want to copy the image from a source to a target window. Other options include erasing the image and inverting the image to the target window.
We finish off by releasing the memory stored by the device context for the desktop window.
ReleaseDC dsHWND, dsDC
That's all of the code we need to implement our magnifier. You can see how the Windows API saves us from creating hundreds, or even thousands of lines of code to grab parts of the desktop like we have in our example.