Advertisement
sglienke

ResizeControlEditor

Oct 18th, 2016
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.48 KB | None | 0 0
  1. unit ResizeControlEditor;
  2.  
  3. interface
  4.  
  5. procedure Register;
  6.  
  7. implementation
  8.  
  9. uses
  10.   Controls,
  11.   DesignEditors,
  12.   DesignIntf,
  13.   Math,
  14.   Types;
  15.  
  16. type
  17.   TControlAutoSizeEditor = class(TDefaultEditor)
  18.   public
  19.     procedure ExecuteVerb(Index: Integer); override;
  20.     function GetVerb(Index: Integer): string; override;
  21.     function GetVerbCount: Integer; override;
  22.   end;
  23.  
  24. procedure Register;
  25. begin
  26.   RegisterComponentEditor(TWinControl, TControlAutoSizeEditor);
  27. end;
  28.  
  29. procedure AdjustSizeToContent(AControl: TWinControl);
  30. const
  31.   MARGIN = 3;
  32. var
  33.   i: Integer;
  34.   LWidth, LHeight: Integer;
  35.   LBounds: TRect;
  36. begin
  37.   LWidth := 0;
  38.   LHeight := 0;
  39.   for i := 0 to AControl.ControlCount - 1 do
  40.   begin
  41.     LBounds := AControl.Controls[i].BoundsRect;
  42.     LWidth := Max(LWidth, LBounds.Right + MARGIN);
  43.     LHeight := Max(LHeight, LBounds.Bottom + MARGIN);
  44.   end;
  45.  
  46.   AControl.ClientWidth := LWidth;
  47.   AControl.ClientHeight := LHeight;
  48. end;
  49.  
  50. { TControlAutoSizeEditor }
  51.  
  52. procedure TControlAutoSizeEditor.ExecuteVerb(Index: Integer);
  53. begin
  54.   case Index of
  55.     0: AdjustSizeToContent(Component as TWinControl);
  56.   end;
  57. end;
  58.  
  59. function TControlAutoSizeEditor.GetVerb(Index: Integer): string;
  60. begin
  61.   case Index of
  62.     0: Result := 'Resize control to fit content';
  63.   end;
  64. end;
  65.  
  66. function TControlAutoSizeEditor.GetVerbCount: Integer;
  67. begin
  68.   if csAcceptsControls in (Component as TWinControl).ControlStyle then
  69.     Result := 1
  70.   else
  71.     Result := 0;
  72. end;
  73.  
  74. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement