Basic Gesture Example

Rather than describing the complete VCL gesturing architecture in theory, let us start by building a very simple first example I've called Gesturesoi. The program has a form with a panel and a memo control, plus the GestureManager component. This last component doesn't have specific setting, as its editor let's you add custom gestures to it, something we will focus on later.

The management of gestures takes place in the Touch property of the target control, for example the form. This property is slightly a misnomer, as it handles all touch and gesture related operations, through its sub-properties. If

63 The gestures overhead is about 300KB to 400KB in executable size, which is why it was worth taking additional measures to avoid it when not needed.

64 Performing a gesture over the control means making the proper mouse or finger movement starting over the control. If you move over other controls while performing the gestures, it won't matter. What is relevant is the starting position.

GestureManager Gestures El InteractiveGestureOptk El Interactive Gestures ParentTabletOptions ¡HTabletOptions

B (Touch

El GestureManager »BI Gestures B| Standard

  • Assign GestureManager) pgoPanSingleFingerVertical rigoPa [igPanfigPressAndTap] [7] True
  • toPressAndHold]
  • TTou ch Manager) G estu reM a n a gerì
In tera cti v e G e s tu r e O p ti e InteractiveGestures ParentTabletOptions Table tOptions

ŒI GestureManager

□ ¡Gestures









  • LeftjRightrUpfDown rUpLeftrUpR pgoParentPass through] [igPressAndTap]
  • False
  • TTouch Ma n ager) G estu re M a n a g eri
  • LeftfRightrUprDownrDownLeftrC

□ r you expand the Touch property (which is Btnmti aiasi*?«!")

of type TTouchManager) for a control, you should see something similar to the image on the right, with the Gestures sub-property disabled as the GestureManager sub-property is not assigned.

As soon as you connect a GestureManager control, the interface of the Object Inspector for the property changes considerably, with the collections of available gestures (by default the Standard ones) listed under the Gestures property, as you can see in the second image here on the right.

In the Object Inspector you can also expand the Standard gestures node, and enable or disable individual gestures for the control, as depicted in the third image of the sequence.

Each element of this collection of gestures (a TGestureColl ectionItem ele ment) has a name, a graphical representation, and (optionally) the action to which it is tied. We'll get to using actions for gestures in a second example. For now, we can just enable all of the gestures for the form (and the panel). This is done more easily in the gestures collections editor rather than in the Object Inspector, as you have buttons to select and deselect all standard actions. The gestures collections editor is visible here on the right.

In this editor you can see the gesture name and graphical description, its internal identifier (a constant) and a graphical animated preview of the gesture itself, with the start point in gray and the movement highlighted by a blue

^ Select Gestures - Formi

^ Select Gestures - Formi

^ & sE




Standard Gestures

0 —Left



0 — Right


0 1 Up



0 1 Down


:0 UpLeft


0 r UpRight


0 _iDownLeft


0 L DownRight


0 1__LeftUp


0 I-LeftDown


0_! RightLip


0 i RightDown



. • Start point

point. You can see an enlarged version of this preview in a gesture test window by double clicking on one of the entries:

Also notice that as the editor caption points out, you are picking the gestures for a given target control (in this case Form1), not at the GestureManager component level. The manager has all possible gestures for the entire application, each form or control picks only those it is capable of handling.

Now, having selected all of the standard gestures for the form and the panel, we can write some code for their generic OnGesture event handler. For the panel I

simply log the fact the event was intercepted to the memo, nothing more:

procedure TForm1.Panel1Gesture(Sender: TObject;

const EventInfo: TGestureEventInfo; var Handled: Boolean); begin

I added this event handler to let you figure out how gestures are associated with the controls underneath, and what matters most is the starting position of the gesture as I mentioned in an earlier note.

For gestures performed on the form, instead, I want to list some of the gesture information, notably their name and ID. The name is extracted from the collection of the corresponding component, which might not exist in case there is no match. Here is the code:

procedure TForm1.FormGesture(Sender: TObject;

const Eventlnfo: TGestureEventlnfo; var Handled: Boolean); var nGesture: Integer;

gestureltem: TCustomGestureCollectionltem; begin nGesture := Eventlnfo.GesturelD;

gestureltem := GestureManager1.FindGesture(se1f, nGesture); if Assigned (gestureltem) then

Log (Format ('Gesture: %s [%d]', [gestureltem.Name, nGesture])) else

Log (Format ('Unrecognized gesture [%d]', [nGesture])); Handled := True; end;

The output of the program is a list of gestures performed on the form, with a simple indication for those originating on the panel:

Was this article helpful?

+5 0

Post a comment