Given only the device context descriptor, it is generally not possible to determine the dimensions of the associated TCanvas object. The descendants of TGraphicControl all share the DC of their parent control, because only window controls can have device contexts, and TGraphicControl objects TGraphicControl not window controls.
When a TGraphicControl child (including TPaintBox ) needs a canvas, TControlCanvas calls on the GetDeviceContext control GetDeviceContext . This method returns the DC handle of the parent control, but before returning it changes the DC a bit:
Result := Parent.GetDeviceContext(WindowHandle); SetViewportOrgEx(Result, Left, Top, nil); IntersectClipRect(Result, 0, 0, Width, Height);
That is, it shifts the origin in accordance with the upper left corner of the current control and updates the clipping region to exclude anything outside the borders of the current control.
In some cases, you can determine the size of the canvas by checking the DC clipping region ( GetClipRgn ), but this is only if the region has not been configured in other ways. The clipping region may be smaller than the control dimensions. (It will never be again due to the IntersectClipRect call shown above.)
As you can see, you need more than just a DC knob to get the information you need. Ideally, this would include a TControl link; then you can simply read its Height and Width properties to find out the dimensions of the control and the canvas.
source share