Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetDesktopWindow (user32)
 
.
Summary
The GetDesktopWindow function returns a handle to the desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which other windows are painted.

C# Signature:

[DllImport("user32.dll", SetLastError = false)]
static extern IntPtr GetDesktopWindow();

VB.NET Signature:

<DllImport("user32.dll", SetLastError:=False)> _
Private Shared Function GetDesktopWindow() As IntPtr
End Function

VB Signature:

Private Declare Auto Function GetDesktopWindow Lib "user32.dll" () As IntPtr

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Here are some functions that you can use to get an image of the destop:

    Private Declare Auto Function GetDesktopWindow Lib "user32.dll" () As IntPtr

    Private Declare Auto Function GetWindowDC Lib "user32.dll" (ByVal windowHandle As IntPtr) As IntPtr

    Private Declare Auto Function GetWindowDC Lib "user32.dll" (ByVal _
    windowHandle As IntPtr) As IntPtr
    Private Declare Auto Function ReleaseDC Lib "user32.dll" (ByVal _
    windowHandle As IntPtr, ByVal dc As IntPtr) As Integer

    Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal _
    hdcDest As IntPtr, ByVal nXDest As Integer, ByVal _
    nYDest As Integer, ByVal nWidth As Integer, ByVal _
    nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc _
    As Integer, ByVal nYSrc As Integer, ByVal dwRop As _
    System.Int32) As Boolean

    Private Const SRCCOPY As Integer = &HCC0020

    Public Function GetScreenshot(ByVal windowHandle As IntPtr, _
    ByVal location As Point, ByVal size As Size) As Image

    Dim myImage As Image = New Bitmap(size.Width, size.Height)
    Dim g As Graphics = Graphics.FromImage(myImage)

    Dim destDeviceContext As IntPtr = g.GetHdc
    Dim srcDeviceContext As IntPtr = GetWindowDC(windowHandle)

    BitBlt(destDeviceContext, 0, 0, size.Width, size.Height, _
           srcDeviceContext, location.X, location.Y, SRCCOPY)
    srcDeviceContext, location.X, location.Y, SRCCOPY)
    ReleaseDC(windowHandle, srcDeviceContext)

    g.ReleaseHdc(destDeviceContext)
    g.Dispose

    Return myImage
    End Function

    'Alternately
    Public Function GetDesktopImage(ByVal loc As System.Drawing.Point, _
                    ByVal area As System.Drawing.Size) _
                    As System.Drawing.Image

      Dim bmp As New Bitmap(area.Width, area.Height)
      Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
      Dim ptScr As System.Drawing.Point = Me.PointToScreen(loc)

      g.CopyFromScreen(loc, New Point(0, 0), area, CopyPixelOperation.SourceCopy)

      'housekeeping
      g.Dispose()

      Return bmp
      'housekeeping
      g.Dispose()
      bmp.Dispose()
    End Function

Alternative Managed API:

Do you know one? Please contribute it!

For Screenshots, you can use Graphics.CopyFromScreen

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions