Delphi 2010 最抢眼的新功能可能就是支持"触摸屏"了, 它包括一个 可触控的软键盘 和识别不同的触屏手势.
因为手势同时支持鼠标, 所以没有触摸屏的我也可以尝试一下其大多数的功能.
首次尝试的步骤:
1、加 TGestureManager 控件如窗体: GestureManager1;
2、设置窗体属性 Touch.GestureManager := GestureManager1; {下面程序是在设计时指定的属性}
3、添加窗体的 OnGesture 事件, 随便写点什么;
4、然后运行程序, 用鼠标随便在窗体上 "划" 几下... 第一个测试程序完成了!
测试代码:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, GestureMgr; type TForm1 = class(TForm) GestureManager1: TGestureManager; procedure FormCreate(Sender: TObject); procedure FormGesture(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; {可在设计时指定} end; procedure TForm1.FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean); begin ShowMessage(Sender.ClassName + '_Gesture'); end; end.
现在程序可以 "感知手势" 了, 怎么 "识别手势" 呢?
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 拷过来的标准手势的图示: