[DUG] Using mdi forms in dunit test

karlreynolds at xtra.co.nz karlreynolds at xtra.co.nz
Mon May 21 17:05:34 NZST 2007


Interesting. It appears (in D2006 anyway) MDI forms are only supported if
the *main form* of the application is an MDI parent form:

procedure TCustomForm.CreateWindowHandle(const Params: TCreateParams);
var
  CreateStruct: TMDICreateStruct;
  NewParams: TCreateParams;
begin
  if (FormStyle = fsMDIChild) and not (csDesigning in ComponentState) then
  begin
    if (Application.MainForm = nil) or
      (Application.MainForm.ClientHandle = 0) then
      raise EInvalidOperation.Create(SNoMDIForm);

Why that is, I don't know. Having said that I was able to hack around it by
making the child form's owner be the parent mdi form and overriding
CreateWindowHandle for the child in a test app:

procedure TForm1.Button1Click(Sender: TObject);
var LForm: TForm;
begin
  LForm := TMDIParentForm2.Create(Application);
  LForm.Show;
  TMDIChildForm.Create(LForm).Parent := LForm;
end;

TMDIParentForm.FormStyle is fsMDIForm;
TMDIChildForm.FormStyle is fsMDIChild, and has the following additions:

  private
    procedure TMDIChildForm.SetLayeredAttribs; { For CreateWindowHandle,
copied directly from Forms.pas, but using AlphaBlend instead of FAlphaBlend
etc.}
  protected
    procedure TMDIChildForm.CreateWindowHandle(const Params: TCreateParams);
override;
  public
    constructor TMDIChildForm.Create(AMDIOwner: TForm); override;

var MOwner: TForm; { Need to hold this for CreateWindowHandle to access }

constructor TMDIChildForm.Create(AMDIOwner: TForm);
begin
  MOwner := AMDIOwner;
  inherited Create(AMDIOwner);
end;

procedure TMDIChildForm.CreateWindowHandle(const Params: TCreateParams);
var
  CreateStruct: TMDICreateStruct;
begin
  if (FormStyle = fsMDIChild) and not (csDesigning in ComponentState) then
  begin
    if (Application.MainForm = nil) or
      (MOwner.ClientHandle = 0) then
      raise EInvalidOperation.Create(SNoMDIForm);
{ The middle bit is copied from TForm.CreateWindowHandle, but replacing
Application.MainForm with MOwner }
  else inherited CreateWindowHandle(Params);
end;

Obviously the forms can't be auto-created in the project to do this. Anyway,
that seemed to look right to me - it may be what you want, not sure. I can
zip up the project for you if you want.

Cheers,
Carl

-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
Behalf Of Sean Cross
Sent: Monday, May 21, 2007 3:00 PM
To: toddm at kol.co.nz; NZ Borland Developers Group - DelphiList
Subject: RE: [DUG] Using mdi forms in dunit test

Yes.  I am doing this

procedure TestTfrmOrders.SetUp;
begin
...

  FmdiParent:= TformMdiParent.Create(Application);
  FmdiParent.Show;
  Application.ProcessMessages;

  FfrmOrders := TfrmOrders.Create(Application);
end;


Regards

Sean Cross
IT Systems Development Manager

Catalyst Risk Management
PO Box 230
50 Dalton St
Napier 4140
DDI: 06-8340362
mobile: 021 270 3466
Visit us at http://www.catalystrisk.co.nz/

Offices in Auckland, Napier, Wellington & Christchurch

Disclaimer:
"The information contained in this document is confidential to the
addressee(s) and may be legally privileged. Any view or opinions expressed
are those of the author and may not be those of Catalyst Risk Management. No
guarantee or representation is made that this communication is free of
errors, viruses or interference. If you have received this e-mail message in
error please delete it and notify me. Thank you."


> -----Original Message-----
> From: delphi-bounces at delphi.org.nz [mailto:delphi-
> bounces at delphi.org.nz] On Behalf Of Todd Martin
> Sent: Monday, 21 May 2007 2:01 p.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: Re: [DUG] Using mdi forms in dunit test
>
> Did you create a parent MDI form first?
>
> On Mon, 21 May 2007 11:47, Sean Cross wrote:
> > Has anyone managed to use mdi forms in a unit test?   I keep getting
> the
> > error "Cannot create form.  NO MDI forms are currently active".
> >
> > I have tried creating and showing an mdi form first without any luck.
> I
> > have also tried creating an inherited child form with a formstyle of
> > fsnormal but that didn't work either.
> >
> > Any ideas?
> >
> > Regards
> >
> > Sean Cross
> > IT Systems Development Manager
> >
> > Catalyst Risk Management
> > PO Box 230
> > 50 Dalton St
> > Napier 4140
> > DDI: 06-8340362
> > mobile: 021 270 3466
> > Visit us at http://www.catalystrisk.co.nz/
> >
> > Offices in Auckland, Napier, Wellington & Christchurch
> >
> > Disclaimer:
> > "The information contained in this document is confidential to the
> > addressee(s) and may be legally privileged. Any view or opinions
> expressed
> > are those of the author and may not be those of Catalyst Risk
> Management.
> > No guarantee or representation is made that this communication is
> free of
> > errors, viruses or interference. If you have received this e-mail
> message
> > in error please delete it and notify me. Thank you."
> >
> >
> >
> > _______________________________________________
> > NZ Borland Developers Group - Delphi mailing list
> > Post: delphi at delphi.org.nz
> > Admin: http://delphi.org.nz/mailman/listinfo/delphi
> > Unsubscribe: send an email to delphi-request at delphi.org.nz with
> Subject:
> > unsubscribe
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with
> Subject: unsubscribe

_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
unsubscribe




More information about the Delphi mailing list