每日程炼-触摸屏编程OnGesture

Delphi 2010 最抢眼的新功能可能就是支持”触摸屏”了, 它包括一个 可触控的软键盘 和识别不同的触屏手势.

因为手势同时支持鼠标, 所以没有触摸屏的我也可以尝试一下其大多数的功能.


首次尝试的步骤:

1、加 TGestureManager 控件如窗体: GestureManager1;

2、设置窗体属性 Touch.GestureManager := GestureManager1; {下面程序是在设计时指定的属性}

3、添加窗体的 OnGesture 事件, 随便写点什么;

4、然后运行程序, 用鼠标随便在窗体上 “划” 几下… 第一个测试程序完成了!

5、添加窗体的panle1 OnCreate 事件, 写: Touch.StandardGestures := [sgLeft, sgRight];

 

6、给窗体添加并关联 TGestureManager, 还要添加 TActionList;

7、给动作关联 Action:

8、给 Action 写代码.


现在程序可以 “感知手势” 了, 怎么 “识别手势” 呢?

Delphi 把可以识别的手势分成了 3 类: 标准手势、自定义手势、交互手势(InteractiveGestures).

其中的交互手势用鼠标不好模拟, 可能只能用于触摸屏;

Delphi 预定义了 34 种标准手势, 并定义成 TStandardGesture 枚举类型:


TStandardGesture = (
  sgLeft            = sgiLeft,
  sgRight           = sgiRight,
  sgUp              = sgiUp,
  sgDown            = sgiDown,
  sgUpLeft          = sgiUpLeft,
  sgUpRight         = sgiUpRight,
  sgDownLeft        = sgiDownLeft,
  sgDownRight       = sgiDownRight,
  sgLeftUp          = sgiLeftUp,
  sgLeftDown        = sgiLeftDown,
  sgRightUp         = sgiRightUp,
  sgRightDown       = sgiRightDown,
  sgUpDown          = sgiUpDown,
  sgDownUp          = sgiDownUp,
  sgLeftRight       = sgiLeftRight,
  sgRightLeft       = sgiRightLeft,
  sgUpLeftLong      = sgiUpLeftLong,
  sgUpRightLong     = sgiUpRightLong,
  sgDownLeftLong    = sgiDownLeftLong,
  sgDownRightLong   = sgiDownRightLong,
  sgScratchout      = sgiScratchout,
  sgTriangle        = sgiTriangle,
  sgSquare          = sgiSquare,
  sgCheck           = sgiCheck,
  sgCurlicue        = sgiCurlicue,
  sgDoubleCurlicue  = sgiDoubleCurlicue,
  sgCircle          = sgiCircle,
  sgDoubleCircle    = sgiDoubleCircle,
  sgSemiCircleLeft  = sgiSemiCircleLeft,
  sgSemiCircleRight = sgiSemiCircleRight,
  sgChevronUp       = sgiChevronUp,
  sgChevronDown     = sgiChevronDown,
  sgChevronLeft     = sgiChevronLeft,
  sgChevronRight    = sgiChevronRight);

 
 

注意: 每个枚举项都对应了一个常数值(譬如: 枚举项 sgLeft 对应 sgiLeft, sgiLeft 是之前定义好的常数);

应记下常数的命名规律, 后面会经常用到它们, 以区别触发的是哪个手势, 譬如:

if EventInfo.GestureID = sgiLeft then ...

 
 

下面是从 docwiki.embarcadero.com/RADStudio/en/TStandardGesture_Enum 拷过来的标准手势的图示:

EnumSymbol
sgLeftimage:64GestLeft.gif
sgRightimage:64GestRight.gif
sgUpimage:64GestUp.gif
sgDownimage:64GestDown.gif
sgUpLeftimage:64GestUpLeft.gif
sgUpRightimage:64GestUpRight.gif
sgDownLeftimage:64GestDownLeft.gif
sgDownRightimage:64GestDownRight.gif
sgLeftUpimage:64GestLeftUp.gif
sgLeftDownimage:64GestLeftDown.gif
sgRightUpimage:64GestRightUp.gif
sgRightDownimage:64GestRightDown.gif
sgUpDownimage:64GestUpDown.gif
sgDownUpimage:64GestDownUp.gif
sgLeftRightimage:64GestLeftRight.gif
sgRightLeftimage:64GestRightLeft.gif
sgUpLeftLongimage:64GestUpLeftLong.gif
sgUpRightLongimage:64GestUpRightLong.gif
sgDownLeftLongimage:64GestDownLeftLong.gif
sgDownRightLongimage:64GestDownRightLong.gif
sgScratchoutimage:64GestScratchOut.gif
sgTriangleimage:64GestTriangle.gif
sgSquareimage:64GestSquare.gif
sgCheckimage:64GestCheck.gif
sgCurlicueimage:64GestCurlicue.gif
sgDoubleCurlicueimage:64GestDoubleCurlicue.gif
sgCircleimage:64GestCircle.gif
sgDoubleCircleimage:64GestDoubleCircle.gif
sgSemiCircleLeftimage:64GestSemiCircleLeft.gif
sgSemiCircleRightimage:64GestSemiCircleRight.gif
sgChevronUpimage:64GestChevronUp.gif
sgChevronDownimage:64GestChevronDown.gif
sgChevronLeftimage:64GestChevronLeft.gif
sgChevronRightimage:64GestChevronRight.gif

 

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, StdCtrls, ExtCtrls, ActnList;

type
  TForm1 = class(TForm)
    gstrmngr1: TGestureManager;
    pnl1: TPanel;
    actlst1: TActionList;
    act1: TAction;
    act2: TAction;
    procedure FormCreate(Sender: TObject);
    procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
      var Handled: Boolean);
    procedure act1Execute(Sender: TObject);
    procedure act2Execute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.act1Execute(Sender: TObject);
begin
  ShowMessage(‘Left’);
end;

procedure TForm1.act2Execute(Sender: TObject);
begin
  ShowMessage(‘Right’);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Self.Touch.GestureManager := gstrmngr1;
end;

procedure TForm1.FormGesture(Sender: TObject;
  const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
  ShowMessage(Sender.ClassName + ‘_Gesture’);
//  case EventInfo.GestureID of
//    sgiLeft  : ShowMessage(‘Left’);
//    sgiRight : ShowMessage(‘Right’);
//  end;

end;

end.

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 参数的作用.

添加 TGestureManager 后, 双击其图标即可进入自定义手势编辑;

编辑框中, Sensitivity 用来调整手势响应的敏感度; 取消 Unidirectional 的勾选后反向手势也可识别; 其他都一目了然.

一个或一组手势设计可以保存为一个 dgf 文件.

通过控件属性中的 Touch -> Gestures 旁边的小按钮可查看、选择或再编辑手势.

通过和 TGestureManager 同组的 TGestureListView、TGesturePreview、TGestureRecorder 控件可设计和 Delphi 设计时一样的运行时的手势编辑(官方说: 他们的手势编辑也是使用了这几个控件).

自定义手势的 ID 分别是 -1、-2、-3 … 可在 OnGesture 事件中用于识别, 当然也可以直接给它分配 Action.

Demo下载:Temp code

演示效果:

程序代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GestureMgr, StdCtrls, ExtCtrls, ActnList;

type
  TForm1 = class(TForm)
    gstrmngr1: TGestureManager;
    pnl1: TPanel;
    actlst1: TActionList;
    act1: TAction;
    act2: TAction;
    procedure FormCreate(Sender: TObject);
    procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
      var Handled: Boolean);
    procedure act1Execute(Sender: TObject);
    procedure act2Execute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.act1Execute(Sender: TObject);
begin
  ShowMessage(‘Left’);
end;

procedure TForm1.act2Execute(Sender: TObject);
begin
  ShowMessage(‘Right’);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Self.Touch.GestureManager := gstrmngr1;
end;

procedure TForm1.FormGesture(Sender: TObject;
  const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
  ShowMessage(Sender.ClassName + ‘_Gesture’);
//  case EventInfo.GestureID of
//    sgiLeft  : ShowMessage(‘Left’);
//    sgiRight : ShowMessage(‘Right’);
//  end;

end;

end.

窗体代码:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = #24858#20154#31508#35760′-http://www.foolcode.com’
  ClientHeight = 238
  ClientWidth = 387
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = ‘Tahoma’
  Font.Style = []
  OldCreateOrder = False
  Touch.GestureManager = gstrmngr1
  OnCreate = FormCreate
  OnGesture = FormGesture
  PixelsPerInch = 96
  TextHeight = 13
  object pnl1: TPanel
    Left = 183
    Top = 0
    Width = 204
    Height = 238
    Align = alRight
    TabOrder = 0
    Touch.GestureManager = gstrmngr1
  end
  object gstrmngr1: TGestureManager
    Left = 192
    Top = 40
    GestureData = <
      item
        Control = Owner
        Collection = <
          item
            GestureID = sgiLeft
          end>
      end
      item
        Control = pnl1
        Collection = <
          item
            Action = act1
            GestureID = sgiLeft
          end
          item
            Action = act2
            GestureID = sgiRight
          end>
      end>
  end
  object actlst1: TActionList
    Left = 192
    Top = 96
    object act1: TAction
      Caption = ‘act1′
      OnExecute = act1Execute
    end
    object act2: TAction
      Caption = ‘act2′
      OnExecute = act2Execute
    end
  end
end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值