...create a parented control in a DLL?

Author: Lüpher Cypher

Category: VCL

// In DLL
//
// This is our windowed control, which also
// contains more windowed controls within itself

type
TMyPanel = class(TPanel)
fSomeControl: tMyWinControl;

constructor Create(aOwner: TComponent); override;
procedure updateControls();
end;...

// Initialize properties here
constructor TMyPanel.Create(aOwner: TComponent);
begin
inherited
Create(aOwner);
...end;

// This method adds this control to its parent
// and initializes and adds its own controls
procedure TMyPanel.updateControls();
begin
// Now, here we have to set ParentWindow,
// because this control is in a DLL.
// Doing it otherwise will cause "Control
// has no parent window" error.
// We do it before we assign Parent (if we)
// assign Parent first, the assignment
// to ParentWindow will have no effect.
// After these two assignments, our
// control is contained within its parent
// so, we setup the parent dependant
// properties now, such as align
Self.ParentWindow := TWinControl(Owner);
Self.Parent := TWinControl(Owner);
Self.Align = alLeft;
...fSomeControl := TMyWinControl.Create(Self);
fSomeControl.ParentWindow := Self.Handle;
fSomeControl.Parent := Self;
fSomeControl.Align := alTop;
// init more properties
...end;


// In application
//

...MyPanel := TMyPanel.Create(Form1);

MyPanel.updateControls();

// Note that if we created the TMyPanel object in
// DLL (e.g. with a call to a function that
// returned the object), we should destroy it
// in DLL, too, to avoid the access violation.

 

printed from
www.swissdelphicenter.ch
developers knowledge base