Given only a handle to a device context, it's not generally possible to determine the dimensions of a TCanvas
object associated with it. Descendants of TGraphicControl
all share the DC of their parent control because only windowed controls can have device contexts, and TGraphicControl
objects aren't windowed controls.
When a TGraphicControl
descendant (including TPaintBox
) needs a canvas, TControlCanvas
calls back to the control's GetDeviceContext
method. That method returns the DC handle of the parent control, but before returning, it modifies the DC a little:
Result := Parent.GetDeviceContext(WindowHandle);
SetViewportOrgEx(Result, Left, Top, nil);
IntersectClipRect(Result, 0, 0, Width, Height);
That is, it shifts the origin to match the upper left corner of the current control, and it updates the clipping region to exclude anything outside the current control's borders.
In some circumstances, you might be able to determine the canvas's dimensions by inspecting the DC's clipping region (with GetClipRgn
), but that's only if the region hasn't been adjusted in any other ways. The clipping region might be smaller than the control's dimensions. (It will never be larger because of the call to IntersectClipRect
shown above.)
As you can see, you need more than just the DC handle to get the information you want. Ideally, that will include a TControl
reference; then you can just read its Height
and Width
properties to know the control's and the canvas's dimensions.