1

I'm trying to create a control, which creates 3 standard TPanel onto itself in design-time and run-time. Everything is okay: control creates panels perfectly. But I struck with one problem: in design-time I want to have an ability to choose one of panels.
I wish to reproduce a standard behaviour of TPageControl: when user clicks on TabSheet on a screen, TabSheet becomes editable via object inspector.

Below attached a code of my control:

unit MyContainer;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  StdCtrls,
  ExtCtrls,
  StrUtils,
  Dialogs;

type
  TMyContainer = class(TCustomControl)
  private
    FPanelA: TPanel;
    FPanelB: TPanel;
    FPanelC: TPanel;

  protected
    procedure Paint; override;

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  procedure register;

implementation

{ TMyContainer }

procedure Register;
begin
  RegisterComponents('MyComps', [TMyContainer]);
end;

constructor TMyContainer.Create(AOwner: TComponent);
begin
  Inherited Create(AOwner);

  Width := 200;
  Height := 200;
  ControlStyle := ControlStyle + [csAcceptsControls];

  FPanelA := TPanel.Create(Self);
  FPanelA.Parent := Self;
  FPanelA.Width := 100;
  FPanelA.Height := 60;
  FPanelA.Left := 10;
  FPanelA.Top := 10;

  FPanelB := TPanel.Create(Self);
  FPanelB.Parent := Self;
  FPanelB.Width := 100;
  FPanelB.Height := 60;
  FPanelB.Left := 10;
  FPanelB.Top := 80;

  FPanelC := TPanel.Create(Self);
  FPanelC.Parent := Self;
  FPanelC.Width := 100;
  FPanelC.Height := 60;
  FPanelC.Left := 10;
  FPanelC.Top := 160;
end;

destructor TMyContainer.Destroy;
begin
  FreeAndNil(FPanelA);
  FreeAndNil(FPanelB);
  FreeAndNil(FPanelC);

  Inherited Destroy;
end;

procedure TMyContainer.Paint;
begin
  Canvas.Brush.Color := clBlue;
  Canvas.FillRect(Canvas.ClipRect);
end;


end.

Is there someone who can show me a way to get a solution for my task?
Thanks in advance.

0

3 Answers 3

3

This can be implemented in many ways, depending on your specific wishes. Because your code does show nothing more than the creation of the panels, I assume you are in need of the very basics before you can even realize what your wishes exactly are, but then the basics may be a little hard for a novice component builder.

First of all: for something to be editable in the object inspector, it has to be a component or be (part of) a published property of a component. Right now your panels are just private fields. So you could try to publish your panels in properties. Or you could add one property for all panels which will be distinguished by a selected index property.

You could also mimic the page control component by adding panels as separate components. In that case you might need to add a component editor for a "New Page" command in its context menu.

Some notes: That control style setting is not needed, unless the component is ment to become parent of other controls by means of the designer. Also, your destructor is superfluous.

Then try asking a very specific component writing question.

4
  • NGLN, thank you for describing some things. It was very useful, because I am not a professional in Delphi. To reach my goal specified in question, I've decided to create a button, which will visible in design-time and not visible in run-time and will create a TPanel by clicking on button. I did it and now I can click this button, because CM_DESIGNHITTEST message handled, as Remy Lebeau notice. When click is passed, TPanel control is created, but I still can't select it in design-time. I also tried to handle CM_DESIGNHITTEST for my descendant of TPanel, but it didn't give me anything. Mar 1, 2016 at 15:07
  • NGLN, while I wrote my preview comment to your post, I remember, that standard TPageControl has a PopupMenu with item 'New page'. I suppose that code from this menu item could shed some light on my problem. Yesterday I have research all TPageControl code, but there are nothing related with PopupMenu in design-time. Do you know, where can I find source code for menu item 'New page'? Mar 1, 2016 at 15:16
  • 1
    For such a context menu command, you must create a component editor, see my edit in the answer. But that involves the need of a great deal of knowledge about component writing, way too much for a comment here. ;-) (It could be a great separate question, if non-existent).
    – NGLN
    Mar 1, 2016 at 17:08
  • NGLN, thanks for link. I will see what can I do with that. Mar 2, 2016 at 9:01
3

If you want to allow the user to actual click on one of the panels at design-time (or any other child control, for that matter), your main component needs to handle the CM_DESIGNHITTEST message and return a non-zero value for any mouse coordinates that fall within the desired child control. The message contains the mouse coordinates in its lParam field (you can receive the message as a TWMMouse record, which has a Pos field that you can turn into a TPoint using the SmallPointToPoint() function).

5
  • Remy Lebeau, I've tried to handle this message and it is really works. Now i can provide a click for child controls in design-time. But this doesn't help me to get how to manipulate my panels in design-time as well as TPageControl does it. But thank you for your effort in solving my problem)) Mar 1, 2016 at 14:58
  • What exactly do you want the user to manipulate? As for TPageControl, it simply returns 1 if a click is over any tab other than the active tab, so the click will be directed to the underlying Win32 tab itself so it can become active. Mar 1, 2016 at 19:06
  • 1
    I think he might want to interact with the child panel the same way one can click on a TTabSheet (not the Tab) inside a PageControl during design-time. And as far as I can see that happens because the TabSheet is created with the Form as the Owner but with the PageControl as the Parent. So it only appears as if the TabSheet is a control that belongs to the PageControl but in actual fact it is a design-time component all on its own. If that makes any sense :) Mar 2, 2016 at 6:11
  • Blurry Sterk, yes, you understood me right! I just want to reproduce PageControl's behaviour when user select TabSheet (not a Tab) in design-time. By the way, I've tried to get parent form via GetParentForm and specify it as Panel owner and my container as a parent for Panel, but it was not successful. So in accordance with NGLN advice above, I will try to implement my own version of PageControl's design-time PopupMenu to have an ability to add panels. Mar 2, 2016 at 8:58
  • Remy Lebeau, I just want to force my control create some panels in design-time and provide an option to manage them via Object Inspector: change their properties, position, delete them. Mar 2, 2016 at 9:00
0

There is a solution for my question.
We need to use TComponentEditor to get an ability to create panels in design-time similar to TPageControl. Thanks to user NGLN for helpful link.

code below register TComponentEditor for my component (which was described in question).

unit MyEditor;

interface

uses
  Classes,
  SysUtils,
  TypInfo,
  StdCtrls,
  ComCtrls,
  ExtCtrls,
  Dialogs,

  ToolsAPI,
  DesignIntf,
  DesignEditors,
  VCLEditors,

  MyContainer; // our control

type
  {>>>>>>>>>>>>>>>>>>>>>>>>>}
  TMyContainerEditor = class(TComponentEditor)
  private
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerbCount: Integer; override;
    function GetVerb(Index: Integer): string; override;
    procedure Edit; override;
  end;
  {<<<<<<<<<<<<<<<<<<<<<<<<<}

procedure Register;

implementation


{ TMyContainerEditor}

procedure Register;
begin
  RegisterComponentEditor(TMyContainer, TMyContainerEditor )
end;

procedure TMyContainerEditor.Edit;
begin
  ShowMessage('TMyContainerEditor editor');
end;

procedure TMyContainerEditor.ExecuteVerb(Index: Integer);
var
  Panel: TPanel;
begin
  Inherited ExecuteVerb(Index);
  case Index of
    0:  
      ShowMessage('Design editor');  
    1:                                    
    begin
      Panel:= TPanel.Create(Designer.Root);
      Panel.Parent := Designer.Root;
      Panel.Name := Designer.UniqueName('Panel');
      Designer.SelectComponent(Panel);
      Designer.Modified;
    end;
  end;
end;

function TMyContainerEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := 'Show info...';
    1: Result := 'Add page';
  end;
end;

function TMyContainerEditor.GetVerbCount: Integer;
begin
  Result := 2;
end;

end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.