Desktop Functions:

   Smart Device Functions:

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

GetWindowDC (user32)
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:

static extern IntPtr GetWindowDC(IntPtr hWnd);

VB.NET Signature:

<DllImport("User32.dll")> _
Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
End Function

VB Signature:

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

User-Defined Types:



GetWindowDC is intended for special painting effects within a window's nonclient area. Painting in nonclient areas of any window is not recommended. The GetSystemMetrics function can be used to retrieve the dimensions of various parts of the nonclient area, such as the title bar, menu, and scroll bars. The GetDC function can be used to retrieve a device context for the entire screen.

After painting is complete, the ReleaseDC function must be called to release the device context. Not releasing the window device context has serious effects on painting requested by applications.

Tips & Tricks:

For .NET Compact Framework, change user32.dll to coredll.dll

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)

    ' Return the result.
    Return TempBMP
  End Function

Alternative Managed API:

Do you know one? Please contribute it!

GetWindowDC on MSDN

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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version