Library tutorials & articles
Magnifying The Desktop With Visual Basic
- Introduction
- Our magnifier app
- Adding the code
- Adding code to the timer
- Conclusion
Adding the code
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.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
i am solving similar kind of problem...
But i doing that in c++. well i found something in msdn. Using GetCursorInfo() one can get the cursor info. and then one can use the drawIcon() to draw. but i am not sure how one can get the icon from the cursor handle...
If you found any solutions, pls reply to this msg.
thanks
vinod
plz help me how to capture screen include cursor picture
Great but plz help me how to capture screen include cursor picture
I learn't alot about using API by trying this code.
Pretty cool. works great!
This thread is for discussions of Magnifying The Desktop With Visual Basic.