Home arrow Visual Basic arrow Page 4 - Magnifying The Desktop With Visual Basic
VISUAL BASIC

Magnifying The Desktop With Visual Basic


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.

Author Info:
By: Jason Brimblecombe
Rating: 4 stars4 stars4 stars4 stars4 stars / 54
February 21, 2002
TABLE OF CONTENTS:
  1. · Magnifying The Desktop With Visual Basic
  2. · Our magnifier app
  3. · Adding the code
  4. · Adding code to the timer
  5. · The magnifier in action
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Magnifying The Desktop With Visual Basic - Adding code to the timer
(Page 4 of 6 )

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:

The status bar contains the x-y co-ordinates of the mouse

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
  • 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.
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials