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 拷過來的標准手勢的圖示: