hDC Property

See AlsoKZZ7GK                 ExamplePEXHDC>Low

Applies To

Form14TJ2LN, common dialog1H1HYJI, picture box31MYIWX, Printer objectCBQUDQ.


Specifies a handle164OMS7 provided by the Windows environment  to the device context64FX9YH of an object; not available at design time and read-only at run time.


{ [form.] [commondialog. | picturebox. ] | Printer.}hDC


This property is a Microsoft Windows environment device context handle.  The Windows environment manages the system display by assigning a device context for the Printer object and for each form and picture box in your application.  You can refer to the handle for an object's device context with the hDC property, which provides a value to pass to Windows APIDEFAPI calls.

With a common dialog control, this property returns a device context for the printer selected in the Print dialog box when the PD_RETURNDC flag is set, or an information context when the PD_RETURNIC flag is set.


Note   The value of the hDC property can change while a program is running.  Therefore, you should not store the value in a variable; instead, use the hDC property each time you need it.

The AutoRedraw1UY7ZM3 property can cause hDC to change.  If AutoRedraw for a form or picture box is set to True, hDC acts as a handle to the device context of the persistent bitmap41CTZFK (equivalent to the Image6AEZWAR property).  When AutoRedraw is False, hDC is the actual hDC value of the Form window and the picture box window.  Note that hDC may change while the program is running regardless of the AutoRedraw setting.


Data Type


See Also


hWnd Property1CIW7KQ

Icon Property2S1DMPP


Programmer's Guide:

Chapter 22, "Calling Procedures in DLLs"

hDC Property Example

The example draws a triangle, and then uses a Windows function to fill it with color.  Be sure that the declaration appears on one line with no break or wordwrap in the form module.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

' Declaration of Windows routine.
Declare Sub FloodFill Lib "GDI" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal Color As Long)
Sub Form_Click ()
  ScaleMode = 3                          ' Windows draws in pixels.
  ForeColor = 0                          ' Set draw line to black.
  Line (100, 50)-(300, 50)               ' Draw a triangle.
  Line -(200, 200)
  Line -(100, 50)
  FillStyle = 0                          ' Set FillStyle to solid.
  FillColor = RGB(128, 128, 255)         ' Set FillColor.
  FloodFill hDC, 200, 100, ForeColor     ' Call Windows API to fill.
End Sub