C# 範例中的 Windows Touch 手勢 (MTGesturesCS)
本節說明 C# 中的 Windows Touch 手勢範例。
這個 Windows Touch 手勢範例示範如何使用手勢訊息,藉由處理 WM_GESTURE 訊息,來轉譯、旋轉圖形裝置介面 (GDI) 轉譯的方塊。 下列螢幕擷取畫面顯示執行範例時的外觀。
在此範例中,手勢訊息會傳遞至手勢引擎,然後呼叫繪圖物件上的方法,以轉譯、旋轉及縮放具有處理這些命令的方法。 為了在 C# 中達成此動作,會建立特殊表單 TouchableForm 來處理手勢訊息。 此表單接著會使用訊息來變更繪圖物件 DrawingObject,以變更物件在 Paint 方法中的呈現方式。
為了協助示範範例的運作方式,請考慮使用移動流覽命令來翻譯轉譯方塊的步驟。 使用者會執行移動流覽手勢,其會產生具有筆勢識別碼 WM_GESTURE 訊息GID_PAN。 TouchableForm 會處理此訊息,並更新繪圖物件的位置,然後物件會轉譯本身。
下列程式碼示範手勢處理常式如何從 WM_GESTURE 訊息擷取參數,然後透過呼叫繪圖物件的移動方法,在轉譯的方塊上執行翻譯。
switch (gi.dwID)
{
case GID_BEGIN:
case GID_END:
break;
(...)
case GID_PAN:
switch (gi.dwFlags)
{
case GF_BEGIN:
_ptFirst.X = gi.ptsLocation.x;
_ptFirst.Y = gi.ptsLocation.y;
_ptFirst = PointToClient(_ptFirst);
break;
default:
// We read the second point of this gesture. It is a
// middle point between fingers in this new position
_ptSecond.X = gi.ptsLocation.x;
_ptSecond.Y = gi.ptsLocation.y;
_ptSecond = PointToClient(_ptSecond);
// We apply move operation of the object
_dwo.Move(_ptSecond.X - _ptFirst.X, _ptSecond.Y - _ptFirst.Y);
Invalidate();
// We have to copy second point into first one to
// prepare for the next step of this gesture.
_ptFirst = _ptSecond;
break;
}
break;
下列程式碼示範繪圖物件的移動方法如何更新內部位置變數。
public void Move(int deltaX,int deltaY)
{
_ptCenter.X += deltaX;
_ptCenter.Y += deltaY;
}
下列程式碼示範如何在繪圖物件的繪製方法中使用位置。
public void Paint(Graphics graphics)
{
(...)
for (int j = 0; j < 5; j++)
{
int idx = arrPts[j].X;
int idy = arrPts[j].Y;
// rotation
arrPts[j].X = (int)(idx * dCos + idy * dSin);
arrPts[j].Y = (int)(idy * dCos - idx * dSin);
// translation
arrPts[j].X += _ptCenter.X;
arrPts[j].Y += _ptCenter.Y;
}
(...)
}
移動流覽手勢會導致轉譯繪製的方塊。