Delphi 2010 新增功能之: 手势编程[4] - 关于 OnGesture 事件过程的参数


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

 
 
 
 
 

 

 
  

你可能感兴趣的:(Delphi 2010 新增功能之: 手势编程[4] - 关于 OnGesture 事件过程的参数)