How to make Rounded Corners of Controls and Components in Delphi XE2?

How to make Rounded Corners of Controls and Components in Delphi XE2?

We will show here how can you make rounded corners of various delphi components and controls. We will take an example of delphi panel component. We will drag 4 delphi panels on a delphi form. We have to make the corners of these delphi panels rounded.

Just call a function RoundTheCorners on the OnShow event of the delphi form. RoundTheCorners function again calls MakeRounded function by passing the name of components (panels here) as an argument. MakeRounded  does all the calculations of rounding the corners of delphi components.

procedure TMyForm.RoundTheCorners;
begin
  MakeRounded(Panel1);
  MakeRounded(Panel2);
  MakeRounded(Panel3);
  MakeRounded(Panel4);
end;

procedure TMyForm.MakeRounded(Control: TWinControl);
var
  R: TRect;
  Rgn: HRGN;
begin
  with Control do
  begin
    R := ClientRect;
    rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 20, 20);
    Perform(EM_GETRECT, 0, lParam(@r));
    InflateRect(r, - 5, - 5);
    Perform(EM_SETRECTNP, 0, lParam(@r));
    SetWindowRgn(Handle, rgn, True);
    Invalidate;
  end;
end;