在Windows Mobile 6.5中有提供了兩個新的功能,一個是Widgets,另外就是Gestures API了,今天就來測試看看Gestures的功能。由於Gestures是WM 6.5才有的功能,所以必須要有WM 6.5的模擬器來做測試,如果還沒有安裝過的話,可以到下面位置下載、安裝,請切記,要先安裝Windows Mobile 6 SDK,之後再安裝Windows Mobile 6.5 Developer Tool Kit,順序可不能顛倒喔。
- Windows Mobile 6 Professional and Standard Software Development Kits Refresh
- Windows Mobile 6.5 Developer Tool Kit
接下來,下面是一些有關Gestures的相關參考資料
- 小歐的這篇-[Windows Mobile]Gestures API 擷取使用者的手勢
- Windows Mobile Managed Gestures Sample 這邊可以下載到managed code需要的dll以及相關的sample,裡面也有兩個web cast,記得要看
- How Do I: Use Windows Mobiles 6.5 Gestures in Managed Code?
- MSDN-Using Gestures in Windows Mobile 6.5
- How to use the MSDN managed code gesture wrapper in a Windows Mobile 6.5 device project
在一些使用情境中,會有向右或是向左滑動就能夠切換頁面的功能,在以往WM6/6.1的時候,要做到這樣的功能,第一個遇到的問題就是必須要自行去辨識手勢的動作,必須要去掛hook,攔截滑鼠的點擊位置、放開的位置,之後去判斷方向等等,要做的事情滿多的,不是件簡單的事;而現在有了Gestures API之後,像這樣的功能做起來就方便的多了,接下來就來看看今天測試的程式碼了;首先要先加入Gestures dll的參考,加入後會多了像是下圖的name space可以使用
接下來是設計畫面,筆者這邊是用了3個panel來模擬切換畫面的動作,實際在執行的時候會分別去移動panel的位置,設計畫面會長的像是下面這個樣子
接下來是相關的程式碼
Imports Microsoft.WindowsMobile
Public Class Form1
Dim WithEvents g As Gestures.GestureRecognizer
''移動的間隔頻率
Dim MoveFeq As Integer = 10
''儲存每次位移的移動量
Dim offset As Integer
Dim Panels(2) As Panel
''儲存目前位於前景的panel號碼
Dim panelIndex As Integer = 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
g = New Gestures.GestureRecognizer()
g.TargetControl = Me
''設定Panel相關的位置與大小
pCenter.Location = New Point(0, 0)
pCenter.Size = Screen.PrimaryScreen.WorkingArea.Size
pLeft.Left = 1000
pLeft.Top = 0
pLeft.Size = Screen.PrimaryScreen.WorkingArea.Size
pRight.Left = 1000
pRight.Top = 0
pRight.Size = Screen.PrimaryScreen.WorkingArea.Size
''設定panel陣列指向的panel
Panels(0) = pLeft
Panels(1) = pCenter
Panels(2) = pRight
AddHandler g.Scroll, AddressOf Gestrues_Scroll
End Sub
Private Sub Gestrues_Scroll(ByVal sender As Object, ByVal e As Gestures.GestureScrollEventArgs)
Select Case e.ScrollDirection
Case Is = Gestures.GestureScrollDirection.Left
If panelIndex > 0 Then
panelIndex -= 1
Else
Exit Sub
End If
MoveCtl(Panels(panelIndex), 0 - Me.Width, 0, False)
Case Is = Gestures.GestureScrollDirection.Right
If panelIndex < 2 Then
panelIndex += 1
Else
Exit Sub
End If
MoveCtl(Panels(panelIndex), Me.Width, 0, True)
End Select
End Sub
Private Sub MoveCtl(ByRef ctlToMove As Panel, ByVal intStart As Integer, ByVal intEnd As Integer, ByVal Direction As Boolean)
ctlToMove.Left = intStart
ctlToMove.BringToFront()
offset = Math.Abs((intStart - intEnd) \ MoveFeq)
Do
If Direction Then
''右--->左
If ctlToMove.Left <= intEnd Then
ctlToMove.Left = intEnd
Exit Do
End If
ctlToMove.Left -= offset
Else
''左--->右
If ctlToMove.Left >= intEnd Then
ctlToMove.Left = intEnd
Exit Do
End If
ctlToMove.Left += offset
End If
Me.Invalidate()
Application.DoEvents()
Threading.Thread.Sleep(500 \ MoveFeq)
Loop
End Sub
End Class
執行的時候畫面大致會像是下圖這樣是不是很方便使用呢?Gestures API真是個好物啊 ~
註:如果需要範例程式碼的話可以到這邊下載VS2008的範例程式碼