Jump to content
Dmitry Onoshko

Implementing Padding in TCustomControl with hybrid GDI/Direct2D painting

Recommended Posts

I work on a TCustomControl descendant that must paint itself with either plain GDI or Direct2D. I got somewhat stuck while implementing the Padding property support which is needed because the control contents might get scrolled, and having its contents drawn at the very edge of its rectangle doesn’t seem like a good thing.

 

Now, looks like custom clipping is not a thing readily available in both TCanvas and TDirect2DCanvas, not even to mention coordinate transformation adjustments to make actual contents painting easier. While I do understand how to implement all the stuff by invoking GDI/Direct2D directly, I’m afraid to have missed some piece of VCL that might have already made the task easier and more straightforward.

 

So, the question is: what is the recommended/supposed way to support Padding property in a TCustomControl?

Share this post


Link to post

Padding is used to position child controls within a parent (your control). It is generally not used to position content (whatever you are drawing) within your control.

 

What Padding does is shrink your control's ClientRect. If you search the source of the Vcl.Controls.pas unit you can see that handling of padding is built into TWinControl. For instance, here's TWinControl.AdjustClientRect:

procedure TWinControl.AdjustClientRect(var Rect: TRect);
begin
  { WM_NCCALCSIZE performs our BorderWidth logic }
  Inc(Rect.Left, Padding.Left);
  Inc(Rect.Top, Padding.Top);
  Dec(Rect.Right, Padding.Right);
  Dec(Rect.Bottom, Padding.Bottom);
end;

 

If you do want to use it to position your content anyway, then you'll need to paint within the control's ClientRect.

 

8 hours ago, Dmitry Onoshko said:

looks like custom clipping is not a thing readily available in both TCanvas and TDirect2DCanvas

I don't know anything about TDirect2DCanvas but the GDI supports both transformation and clipping and TCanvas is just a wrapper around a GDI Device Context.

Use TCanvas.Handle to get a handle to the DC (HDC).

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×