Home arrow Visual Basic arrow Page 3 - 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 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):

Option Explicit

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

End Type


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