Forms and Components | Online Documentation for Altium Products

Contents
  • Overview of Graphical Components
  • Designing Script Forms
  • Creating a New Script Form
  • Displaying a Script Form
  • Accepting Input from the User
  • Responding to Events
  • Writing Event Handlers
  • Component's Properties
  • Component Events
  • Component Methods
  • Dropping Components on a Script Form
  • Creating Components in a Script
  • Customizing Script Forms
  • Refreshing Script Forms and Components

Parent page: DelphiScript

Overview of Graphical Components

The scripting system handles two types of components: Visual and Non-visual components.

Visual components are used to build the user interface and the non-visual components are used for different tasks, such as the Timer, OpenDialog and MainMenu components.

  • The Timer non-visual component can be used to activate specific code at scheduled intervals, and it is never seen by the user.
  • The Button, Edit and Memo components are visual components.

Both types of components appear at design time but non visual components are not visible at runtime. Components from the Tool Palette panel are object oriented and have the three following items:

  • Properties
  • Events
  • Methods

A Property is a characteristic of an object that influences either the visible behavior or the operations of this object. For example, the Visible property determines whether this object can be seen or not on a script form.

An Event is an action or occurrence detected by the script. In a script, the programmer writes code for each event handler designed to capture a specific event such as a mouse click.

A Method is a procedure that is always associated with an object and defines the behavior of an object.

All script forms have one or more components. Components usually display information or allow the user to perform an action. For example, a Label is used to display static text, an Edit box is used to allow user to input some data, a Button can be used to initiate actions.

Any combination of components can be placed on a form, and while the script is running, a user can interact with any component on a form. It is the programmer's task to decide what happens when a user clicks a button or changes text in an Edit box.

The Scripting system supplies a number of components that can be used to create complex user interfaces for scripts. To place a component on a form, locate its icon on the Tool Palette panel and double-click it. This action places a component on the active form. The visual representation of most components is set with their properties. A component is initially placed in a default position on the form, but can be repositioned (dragged) and resized (stretched) as required. You can also change the size and position later using the Object Inspector.

When a component is dropped onto a form, the Scripting system automatically generates code necessary to use the component, and updates the script form. Only the properties need to be set and event handler code implemented to use the desired methods to get the component on the form working.

Designing Script Forms

A script form is designed to interact with the user within the environment. Designing script forms is the core of visual development.

In practice, all components placed on a script form and each property that is set is stored in a file describing the form (a *.DFM file) which has a relationship with the associated script code (the *.PAS file). For every script form, there is the .PAS file and its corresponding .DFM file.

When working with a script form and its components, all of the element properties can be inspected and modified using the Object Inspector panel. You can select more than one component by shift-clicking on the components, or by dragging a selection rectangle around the components on the form. A script form has a title (the Caption property on the Object Inspector panel).

Creating a New Script Form

With a script project open, right click on a project in the Projects panel, click on the Add New to Project item in the pop-up context menu and choose a Delphi Script Form item. A new script form will open with the default name of EditScript1.pas.

Displaying a Script Form

A script needs to have a procedure that displays the form when the script form is executed. Within this procedure the ShowModal method can be invoked for the form. The Visible property of the form needs to be false if the ShowModal method of the script form is to work correctly.

ShowModal example:

view sourceprint?
1 Procedure RunDialog;
2 Begin
3   DialogForm.ShowModal;
4 End;

The ShowModal example is a very simple approach to displaying the script form when the RunDialog procedure is invoked. Note that values can be assigned to the components of the DialogForm object before the DialogForm.ShowModal method is invoked.

The ModalResult property example shown below is a bit more complex. The following methods in the script are used for buttons on a script form. The methods cause the dialog to terminate when the user clicks either the OK or Cancel button, which return mrOk or mrCancel from the ShowModal method respectively.

ModalResult Example:

view sourceprint?
01 Procedure TForm.OKButtonClick(Sender: TObject);
02 Begin
03   ModalResult := mrOK;
04 End;
05  
06 Procedure TForm.CancelButtonClick(Sender: TObject);
07 Begin
08   ModalResult := mrCancel;
09 End;
10  
11 Procedure RunShowModalExample;
12 Begin
13   // Form's Visible property must be false for ShowModal to work correctly.
14   If Form.ShowModal = mrOk Then ShowMessage('mrOk');
15   If Form.ShowModal = mrCancel Then ShowMessage('mrCancel');
16 End;

You could also set the ModalResult value to mrOk for the OK button and mrCancel for the Cancel button in their event handlers to accomplish the same result as above.

When the user clicks either button on this script form, the dialog box closes. There is no need to call the Close method, because when the ModalResult method is set, the script engine closes the script form automatically.

Note that if you wish to set the form's ModalResult to Cancel when user presses the Escape key, use the Object Inspector to set the Cancel button's Cancel property to True, or insert Sender.Cancel := True in the form's button CancelButtonClick event handler.

Accepting Input from the User

One of the common components that can accept input from the user is the TEdit component. This component has a field where the user can type in a string of characters. Note that there are other Delphi components such as TMaskEdit, which is an edit component with an input mask stored in a string — this controls or filters the input.

The example below illustrates the process when user clicks on the button after typing something in the edit box. If the user did not type anything in the edit component (blank), the event handler responds with a warning message.

view sourceprint?
1 Procedure TScriptForm.ButtonClick(Sender : TObject);
2 Begin
3   If Edit1.Text = '' Then
4   Begin
5     ShowMessage('Warning - empty input!');
6     Exit;
7   End;
8   // do something else for the input
9 End;

Note that a user can shift the dialog's input focus using the Tab key or by clicking on another control on the form.

Responding to Events

When a button on a form or a component is clicked, the Scripting System responds by receiving an event notification from Altium Designer and calling the appropriate event handler method.

See also
The HelloWorld project from the Scripts\DelphiScript Scripts\General\ folder of the scripts collection.
The ShowModal example script from the Scripts\DelphiScript Scripts\General\ folder of the scripts collection.

Writing Event Handlers

Each component in a form script has a set of event names, and these are used by script event handlers that determine how the script will react to user actions in Altium Designer. For instance, when a user clicks a button on a form, Altium Designer sends a message to the script and the script reacts to this new event. If the OnClick event for a button is specified it is executed.

The code to respond to events is normally contained in DelphiScript event handlers, and all components have a set of events that they can react on. For example, all clickable components have an OnClick event that is fired when a user clicks on a component. All such components also have an event for obtaining and losing the focus. However, if the code for OnEnter and OnExit has not been specified (OnEnter - the control has focus; OnExit - the control has lost focus) the event will be ignored by a script.

In summary, an event is a link between an occurrence in Altium Designer, such as clicking a button, and a piece of code that responds to that occurrence. The responding code is an event handler. This code modifies property values and calls methods.

Component's Properties

To see a list of properties for a component, select a component and activate the Properties tab in the Object Inspector panel.

Component Events

To see a list of events a component can react on, select a component and activate the Events tab in the Object Inspector panel. To create an event handling procedure, select the event you want the component to react to and double click the event name — the scripting system will automatically insert the event handler framework code.

For by way of example, select the TButton component from the Tool Palette panel and drop it on the script form, then double click next to the OnClick event name in the Object Inspector. The scripting system will bring the Code Editor in focus and the skeleton code for the OnClick event will be created.

If a button has a Close method in the CloseClick event handler for example, when the button is clicked the button event handler will capture the OnClick event causing the code inside the event handler to be executed. Consequently, the Close method closes the script form.

In a summary, select a button component either on the form or by using the Object Inspector panel, select the Events page and double click on the right side of the OnClick event and a new event handler will appear in the script. Alternatively, double click on the button itself and the scripting system will add a handler for this OnClick event. Note that other types of components will have different default actions.

Component Methods

To see a list of methods for a component, see the Component Reference document.

Dropping Components on a Script Form

To use components from the Tool Palette panel in a script, a script form needs to exist before components can be dropped on the form. Normally when components are dropped on a script form, these objects do not need to be created or destroyed — the script form does this automatically.

The scripting system also automatically generates the code necessary to use a component and updates the script form. Then it's just a matter of setting the properties, putting code in event handlers and using methods as necessary to implement a working script form.

Creating Components in a Script

Components can be directly created or destroyed in a script by passing a Nil parameter to the Constructor of a component. Normally you don't need to pass in the handle of the form because the script form automatically takes care of it. For example, you can create and destroy Open and Save Dialogs (TOpenDialog and TSaveDialog classes from the Embarcadero Delphi RTL).

Customizing Script Forms

The essential points for customizing script forms are:

  • To force a form stay on top of other open panels, set the FormStyle property to fsStayOnTop.
  • To define the default behavior of a form, set the FormKind to one of the following values; fkNone, fkNormal, fkServerPanel or fkModal.
  • If fkModal is closed, then the form will be a modal form — that is, waiting for user input before proceeding such as closing the form. If fkServerPanel then the form will be shown as a Server panel. If fkNormal then the form acts as a normal non-modal form.
  • To remove the form's default scroll bars, change the value of the HorzScrollBar and VertScrollBar properties.
  • To make the form a MDI frame or MDI child, use the FormStyle property
  • To change the form's border style, use the BorderIcons and BorderStyle properties. (The results are visible at runtime.)
  • To change the icon for the minimized form, use the Icon property.
  • To specify the initial position of a form in the application window, use the Position property.
  • To specify the initial state of the form, (eg; minimized, maximized or normal) use the WindowState property.
  • To define the working area of the form at runtime, use the ClientHeight and ClientWidth properties. (Note that ClientHeight and ClientWidth represent the area within the form's border; Height and Width represent the entire area of the form.)
  • To specify which control has initial focus in the form at runtime, use the ActiveControl property.
  • To pass all keyboard events to form, regardless of the selected control, use the KeyPreview property.
  • To specify a particular menu, if a form contains more than one menu, use the Menu property.

Refreshing Script Forms and Components

When the surface of a script form becomes out of date, where for example, the controls are not updated or repainted, then the controls can look frozen or corrupted — this might be due to intensive background processing from that script.

The Update method of the script form, and many of script components from the Tool Palette, provide a way to refresh the graphical contents of the form or the specific control(s). The lines containing the Update method have been highlighted in gray in the example below.

StatusBar component and its Update method Example:

view sourceprint?
01 Procedure TConverterForm.loadbuttonClick(Sender: TObject);
02 Begin
03  If OpenPictureDialog1.Execute then
04  Begin
05  XPProgressBar1.Position := 0;
06  XStatusBar1.SimpleText  := '  Loading...';
07  XStatusBar1.Update;
08   
09  // loading a monochrome bitmap only
10  Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
11   
12  // Check if image is monochrome, otherwise prompt a warning
13  If Image1.Picture.Bitmap.PixelFormat <> pf1bit Then
14  Begin
15  ShowWarning('The image is not a monochrome!');
16  Close;
17  End;
18   
19  lImageSize.Caption := IntToStr(Image1.Picture.Width) + ' x ' +
20  IntToStr(Image1.Picture.Height) + ' mils';
21   
22  convertbutton.Enabled  := True;
23  LoadButton.Enabled     := False;
24  XStatusBar1.SimpleText := '  Ready...';
25  XStatusBar1.Update;
26  End;
27 End;

The above code snippet is from the PCB Logo Creator script project, which can be found in the Scripts\Delphiscript Scripts\Pcb\PCB Logo Creator folder of the downloadable scripts collection.

(0)

相关推荐