OnGesture 事件中的 const EventInfo: TGestureEventInfo; 參數主要用於識別手勢信息;
前面用過 EventInfo.GestureID, 還有 EventInfo.Location 給出了手勢動作的起點坐標, 測試代碼:
procedure TForm1.FormGesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
ShowMessage(Format('X:%d; Y:%d' , [EventInfo.Location.X, EventInfo.Location.Y]));
end;
EventInfo 是個結構, 裡面還有更多信息; 在沒有觸摸屏的情況下, 其它有可能都用不上了.
OnGesture 事件中還有一個 var 參數 Handled: Boolean;
Handled 過來的值是 False, 我們一般可以寫上一句 Handled := True;
如果 Handled = False 這個觸摸動作將繼續向上層控件去尋找相應.
首先應該知道, 現在包括窗體在內的諸多控件都有了 GestureManager 屬性和 OnGesture 事件...
譬如我們在 Panel 和其窗體上同時設置了手勢, 就可以看到 Handled 參數的作用.
測試代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GestureMgr, ExtCtrls;
type
TForm1 = class(TForm)
GestureManager1: TGestureManager;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
var Handled: Boolean);
procedure Panel1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo;
var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Self.Touch.GestureManager := GestureManager1;
Panel1.Touch.GestureManager := GestureManager1;
end;
procedure TForm1.FormGesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
ShowMessage(Sender.ClassName);
end;
procedure TForm1.Panel1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
Handled := True; {可以把這句注釋掉再試}
ShowMessage(Sender.ClassName);
end;
end.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 206
ClIEntWidth = 339
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnGesture = FormGesture
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 128
Top = 16
Width = 185
Height = 121
Caption = 'Panel1'
TabOrder = 0
OnGesture = Panel1Gesture
end
object GestureManager1: TGestureManager
Left = 56
Top = 104
end
end