[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVclTForm

(C++ VCL) TForm

The Component you put all others on. Without any others on it, it is an empty window. Can be found in Forms.hpp.

TForm related functions

Multiple TForms

There are multiple options when you want to develop an application with multiple forms. You can use Auto-created Forms (default) or create them dynamically yourself. Also you can choose between using normal Forms or use MDI Parents and MDI Childs. MDI is an abbreviation for 'Multiple Document Interface'.

An Auto-created Form showing another auto-created Form

To have multiple (non-parent) TForms in your program, do (using default names):
  • Create an VCL Application (called Project1 with Unit1 and Form1)
  • Create a new Form (called Unit2 and Form2)
  • Save all in the same directory under their default names
  • Select Form1
  • Select 'Project | Add to Project | Unit2.cpp'
  • Select 'File | Include Unit Hdr | Unit2'
Now you can type, e.g. under an OnClick event:
  Form2->Show();


An Auto-created MDI Form showing another auto-created MDI Child

  • Start a new VCL Application
  • Set the Form Name to FormParent.
  • Set FormParent's FormStyle to fsMDIForm.
  • Save this Form as UnitFormParent.cpp.
  • Create a new Form called FormChild
  • Set FormChild's FormStyle to fsMDIChild
  • Save this Form as UnitFormChild.cpp
  • Select FormParent
  • Select 'Project | Add to Project | UnitFormChild.cpp'
  • Select 'File | Include Unit Hdr | UnitFormChild'
Now you can type, e.g. under an OnClick event:
  FormChild->Show();


Note that this is not the standard use of an MDI application, as we only have one single Child, which only has one instance. But the Child DOES have different behaviour compared to standard Forms.

An Auto-created MDI Form showing multiple dynamically-created MDI Childs

  • Start a new VCL Application
  • Set the Form Name to FormParent.
  • Set FormParent's FormStyle to fsMDIForm.
  • Save this Form as UnitFormParent.cpp.
  • Create a new Form called FormChild
  • Set FormChild's FormStyle to fsMDIChild
  • Save this Form as UnitFormChild.cpp
  • Select FormParent
  • Select 'Project | Add to Project | UnitFormChild.cpp'
  • Select 'File | Include Unit Hdr | UnitFormChild'
  • Do 'Project | Options | (tab) Forms', then remove 'FormChild' from the Auto-create list
Creating an instance of a Child can be done like this:
  //Somewhere in code of FormParent, e.g. under an OnClick Event
  TFormChild * child = new TFormChild(this);
  //Code
  delete child;


Or using a auto_ptr:

  //Headers
      1. include <memory>
//Somewhere in code of FormParent, e.g. under an OnClick Event std::auto_ptr<TFormChild> child(new TFormChild(this)); //Code


But this simple code is not enough, as you probably want to keep the Child Forms on the Parent Form. So you could use a vector storing these child pointers. I use the shared_ptr for this:

//UnitFormParent.h
//Add these headers
  1. include <vector>
  2. include <boost/shared_ptr>
//Add to the private section of the class declaration: std::vector<boost::shared_ptr<TFormChild> > mChilds;


Then add to FormParent's definitions:

//A button for creating a new Child
void __fastcall TFormMain::ButtonNewClick()
{
  mChilds.push_back(boost::shared_ptr<TFormChild>(new TFormChild(this)));
  //Optionally: (you'll need to add a Label to the Child)
  mChilds.back()->Label1->Caption = "Child #" + IntToStr(Tag);
  ++Tag;
}
//---------------------------------------------------------------------------
//A button for removing a Child
void __fastcall TFormMain::ButtonDeleteClick()
{
  if (!mChilds.empty()) mChilds.pop_back();
}
//---------------------------------------------------------------------------


Transparency

Making a Form transparent is easy. First set the AlphaBlend property to true. Then the AlphaBlendValue determines the transparency. An AlphaBlendValue of 0 denotes total transparence (you won't see it anymore), an AlphaBlendValue of 255 denotes the standard non-transparency.

Custom shape

Here's a simple example showing to change your Form's shape:

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  HRGN h1 =CreateEllipticRgn(0,0,100,100);
  HRGN h2 =CreateEllipticRgn(90,0,190,100);
  HRGN h3 =CreateEllipticRgn(180,0,280,100);
  CombineRgn(h1,h1,h2,RGN_OR);
  CombineRgn(h1,h1,h3,RGN_OR);
  SetWindowRgn(Handle,h1,true);
  DeleteObject(h1);
  DeleteObject(h2);
  DeleteObject(h3);
}


'TForm' Links

Code links



last edited (December 23, 2006) by bilderbikkel, Number of views: 3271, Current Rev: 10 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.