Results 1 to 3 of 3

Thread: I found the coding to display the desktop, but there's an error somewhere?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177

    Talking

    I found the coding to display the desktop, but there's an error somewhere? Can anyone figure out what's wrong

    URL:http://support.microsoft.com/support/kb/articles/q140/8/85.asp

    Coding:Step-by-Step Example
    Start Visual Basic for Windows (VB.EXE). Form1 is created by default.


    Add a picture box (Picture1) to Form1.


    Set the following properties:
    Control Property Value
    ------------------------------
    Picture1 AutoRedraw True
    Picture1 Visible False




    Add the following code to your .BAS module:
    Type lrect
    left As Integer
    top As Integer
    right As Integer
    bottom As Integer
    End Type

    Private Declare Function GetDesktopWindow Lib "user" () As Integer
    Private Declare Function GetDC Lib "user" (ByVal hWnd%) As Integer

    ' Enter the following Declare on one, single line:

    Private Declare Function BitBlt Lib "GDI" (ByVal hDestDC%, ByVal X%, ByVal Y%,
    ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%,
    ByVal YSrc%, ByVal dwRop&) As Integer
    ' Enter the following Declare on one, single line:

    Private Declare Function ReleaseDC Lib "User"(ByVal hWnd As Integer, ByVal

    hDC As Integer) As Integer

    Private Declare Sub GetWindowRect Lib "User" (ByVal hWnd%, lpRect As lrect)
    Global TwipsPerPixel As Single



    Add the following code to the Form1 Click event procedure:
    Private Sub Form_Click ()
    Call GrabScreen
    End Sub

    Public Sub GrabScreen ()

    Dim winSize As lrect

    ' Assign information of the source bitmap.
    ' Note that BitBlt requires coordinates in pixels.
    hwndSrc% = GetDesktopWindow()
    hSrcDC% = GetDC(hwndSrc%)
    XSrc% = 0: YSrc% = 0
    Call GetWindowRect(hwndSrc%, winSize)
    nWidth% = winSize.right ' Units in pixels.
    nHeight% = winSize.bottom ' Units in pixels.

    ' Assign informate of the destination bitmap.
    hDestDC% = Form1.Picture1.hDC
    x% = 0: Y% = 0

    ' Set global variable TwipsPerPixel and use to set
    ' picture box to same size as screen being grabbed.
    ' If picture box not the same size as picture being
    ' BitBlt'ed to it, it will chop off all that does not
    ' fit in the picture box.
    GetTwipsPerPixel
    Form1.Picture1.Top = 0
    Form1.Picture1.Left = 0
    Form1.Picture1.Width = (nWidth% + 1) * TwipsPerPixel
    Form1.Picture1.Height = (nHeight% + 1) * TwipsPerPixel

    ' Assign the value of the constant SRCOPYY to the Raster operation.
    dwRop& = &HCC0020

    ' NOTE: function call must be on one line:
    Suc% = BitBlt(hDestDC%, x%, Y%, nWidth%, nHeight%,
    hSrcDC%, XSrc%, YSrc%, dwRop&amp

    ' Release the DeskTopWindow's hDC to Windows.
    ' Windows may hang if this is not done.
    Dmy% = ReleaseDC(hwndSrc%, hSrcDC%)

    'Make the picture box visible.
    Form1.Picture1.Visible = True
    End Sub

    Public Sub GetTwipsPerPixel ()
    ' Set a global variable with the Twips to Pixel ratio.
    Form1.ScaleMode = 3
    NumPix = Form1.ScaleHeight
    Form1.ScaleMode = 1
    TwipsPerPixel = Form1.ScaleHeight / NumPix
    End Sub



    Run the program. Click Form1.


    Using the mouse, change the size of the form to see more of the picture box. With a little work, you can use this as a screen saver program.


    OK: When I tried the example I got a whole bunch of undefined variables. When I defined the variables I got an Overflow error Run-time error '6'

    Does anyone know what the hell is going on?
    212 will lead you to the truth

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    By looking at this Microsoft's example I assume you're trying to get an image of the desktop. If that's the case, then it is a lot easier then they're showing. Add a picturebox to the form and copy this code:
    Code:
    Option Explicit
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd 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
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Const SRCCOPY = &HCC0020
    
    Public Sub GetScreenImage(pPicture As PictureBox)
        Dim recSize As RECT
        Dim lngHwndSource As Long
        Dim lngDCHwnd As Long
        Dim intHeight As Integer
        Dim intWidth As Integer
        Dim intY As Integer
        Dim intX As Integer
        
        pPicture.Visible = False
        
        lngHwndSource = GetDesktopWindow()
        lngDCHwnd = GetDC(lngHwndSource)
        
        Call GetWindowRect(lngHwndSource, recSize)
        intWidth = recSize.Right
        intHeight = recSize.Bottom
        
        pPicture.Top = 0
        pPicture.Left = 0
        pPicture.Width = ScaleWidth
        pPicture.Height = ScaleHeight
        
        Call BitBlt(pPicture.hdc, 0, 0, intWidth, intHeight, lngDCHwnd, 0, 0, SRCCOPY)
        Call ReleaseDC(lngHwndSource, lngDCHwnd)
        pPicture.Visible = True
    End Sub
    
    Private Sub Form_Click()
        GetScreenImage Picture1
    End Sub

  3. #3
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Cool

    Serge is right, but for an explanation -

    Your code was old code for 16bit platforms.

    - Your Lib "user" etc should be "user32" if your using win 98 and above.

    - most of your vars are integers ex. hwndSrc% whereas for this to work they should be declared As Long -
    hwnd var types (handles) are always long in win32.

    Also:
    - If you declare your API functions in a separate module,
    as you have, you can only access them from the form's code
    if they are declared as Public and not private as in your
    code.




    C/C++,Delphi,VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
    I love deadlines. I like the whooshing sound they make as they fly by.
    —Douglas Adams

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width