GetWindowDC (user32)
Last changed: Senthil Kumar-220.227.31.66

.
Summary
The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area. GetWindowDC assigns default attributes to the window device context each time it retrieves the device context. Previous attributes are lost.

C# Signature:

[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);

VB Signature:

Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

VB.NET Signature:

<DllImport("User32.dll", EntryPoint:="GetWindowDC", _
  CallingConvention:=CallingConvention.StdCall, _
  CharSet:=CharSet.Auto, exactspelling:=True)> _
Public Shared Function GetDesktopWindow() As Int32
End Function

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

  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
  Declare Function GetWindowDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr

  ' Get an image of the form plus its decoration
  ' (borders, title bar, etc).
  Private Function GetDecoratedFormImage() As Bitmap
    ' Get this form's Graphics object.
    Dim MyGrph As Graphics = Me.CreateGraphics

    ' Make a Bitmap to hold the image.
    Dim TempBMP As New Bitmap(Me.Width, Me.Height, MyGrph)
    Dim MyGrphBmp As Graphics = MyGrph.FromImage(TempBMP)
    Dim MyGrphBmpHdc As IntPtr = MyGrphBmp.GetHdc

    ' Get the form's hDC. We must do this after
    ' creating the new Bitmap, which uses me_gr.
    Dim MyGrphHdc As IntPtr = GetWindowDC(Me.Handle)

    ' BitBlt the form's image onto the Bitmap.
    BitBlt(MyGrphBmpHdc, 0, 0, Me.Width, Me.Height, MyGrphHdc, 0, 0, SRCCOPY)
    MyGrphBmp.ReleaseHdc(MyGrphBmpHdc)

    ' Return the result.
    Return TempBMP
  End Function

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
GetWindowDC on MSDN