Visual Basic
  Home arrow Visual Basic arrow Page 4 - Magnifying The Desktop With Visual Basic
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Magnifying The Desktop With Visual Basic
By: Jason Brimblecombe
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 48
    2002-02-21

    Table of Contents:
  • Magnifying The Desktop With Visual Basic
  • Our magnifier app
  • Adding the code
  • Adding code to the timer
  • The magnifier in action
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic Articles
    More By Jason Brimblecombe


       · Nice article.Hope i can get away with trying it out at college :P.
     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek