Magnifying The Desktop With Visual Basic

Adding code to the timer

Double click on the timer control. Add the following lines of code to its Timer() event:

Dim cp As POINTAPI Dim dsDC As Long Dim lpPT As POINTAPI Dim hr As Long Dim vr As Long Dim percent As Double Dim lengthX As Long Dim lengthY As Long Dim offsetX As Long Dim offsetY As Long Dim blitAreaX As Long Dim blitAreaY As Long Dim ret As Long Dim dsHWND As Variant

We start off by declaring multiple variables that will be passed both to and from several of the API calls that we talked about on the last page.

GetCursorPos cp StatusBar1.SimpleText = "X = " & cp.X & ", Y = " & cp.y

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.

dsHWND = GetDesktopWindow()

percent = VScroll1.Value / 100 lengthX = (Form1.ScaleWidth - VScroll1.Width) * percent lengthY = (Form1.ScaleHeight - StatusBar1.Height) * percent

'Center image around the mouse offsetX = lengthX \ 2 offsetY = lengthY \ 2

'Actual area to blit to blitAreaX = Form1.ScaleWidth - VScroll1.Width blitAreaY = Form1.ScaleHeight - StatusBar1.Height

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

ret = StretchBlt(Form1.hdc, 0, 0, blitAreaX, blitAreaY, dsDC, cp.X - offsetX, cp.y - offsetY, lengthX, lengthY, SRCCOPY)

End If

End If

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

- nrcHeight: 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.

You might also like...

Comments

About the author

Mitchell Harper Australia

Visit http://www.devarticles.com for more articles and free programming eBooks, or visit Socket6.com for your dose o...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger