[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] | Reference for unit 'Controls' (#lcl)

# TControlCanvas.CreateHandle

Ensures that a handle exists for the class instance.

## Declaration

Source position: controls.pp line 363

`protected procedure TControlCanvas.CreateHandle; override;`

## Description

Calls the inherited method on entry when a value has not been assigned to the Control property.

Otherwise, Control is used to check for an existing device context (HDC) for the class instance. When assigned, its value is copied into the Handle for the control.When not assigned, the HandleNeeded method in TWinControl is called to create the window handle and get its device context. If Control is not derived from TWinControl, its Parent is used to call the HandleNeeded method.

This is done to prevent resource leaks that might occur when directly accessing he window Handle before it has actually been allocated (recursive calls to the method).

If the device context remains unassigned, the GetDeviceContext method in Control is called to get the device context for the window Handle.

## See also

TControlCanvas.Control |  | The Control object for which the Canvas is used.
TWinControl.HandleNeeded |  | Call this method when your code requires a valid Handle for this control.
TWinControl.Handle |  | A reference handle to the widgetset class instance associated with this control.
TControl.GetDeviceContext |  | Returns a device context handle for the control, from Parent.
TCanvas.CreateHandle |  | Creates a new Handle for the Canvas.