继续操作并编写用于处理所有手势的完整 switch-case 方法
[cpp] view plain copy print ? void CMTTestDlg::DecodeGesture(WPARAM wParam, LPARAM lParam) { GESTUREINFO gi; ZeroMemory(&gi, sizeof(GESTUREINFO)); GetGestureInfo((HGESTUREINFO)lParam, &gi); switch (gi.dwID){ case GID_ZOOM: // Code for zooming goes here break; case GID_PAN: break; case GID_ROTATE: break; case GID_TWOFINGERTAP: break; case GID_PRESSANDTAP: break; default: // You have encountered an unknown gesture break; CloseGestureInfoHandle((HGESTUREINFO)lParam); } void CMTTestDlg::DecodeGesture(WPARAM wParam, LPARAM lParam) { GESTUREINFO gi; ZeroMemory(&gi, sizeof(GESTUREINFO)); GetGestureInfo((HGESTUREINFO)lParam, &gi); switch (gi.dwID){ case GID_ZOOM: // Code for zooming goes here break; case GID_PAN: break; case GID_ROTATE: break; case GID_TWOFINGERTAP: break; case GID_PRESSANDTAP: break; default: // You have encountered an unknown gesture break; CloseGestureInfoHandle((HGESTUREINFO)lParam); }
请注意,在函数的末尾,我们调用了 CloseGestureInfoHandle 函数,用于关闭与手势信息处理程序相关联的资源。如果处理 WM_GESTURE 消息,则您要确保使用此函数来关闭句柄。不这么做可能会导致内存泄漏。 处理手势消息具有一个固定流程,包括配置、解码手势消息以及根据应用程序的需要处理特定手势。正如您在以上代码中看到的那样,执行这个流程并不很难。
现在,让我们来详细了解缩放手势,通过这种手势,您也可以大致了解所有其他手势的工作原理。 使用缩放手势缩放对象 缩放手势通常被用户视为两个接触点之间的“挤压”运动,您可以将手指相互靠近以缩小内容显示,或者将手指分开以放大内容显示。使用缩放手势,您可以缩放对象的大小。 图 说明了缩放手势的使用方式
现在,我们将了解需要在 GID_ZOOM switch 中实现什么代码才能达到所需的缩放效果。 手势信息结构包括 dwFlags 成员,该成员用于确定手势的状态,而且可以包括以下任何值: GF_BEGIN - 指示手势即将开始,在第一个 WM_Gesture 消息中收到GF_INERTIA - 指示手势已经触发了延时GF_END - 指示手势已经完成switch 的默认值 - 指示手势消息的剩余部分,通常称为变化量 我们将使用 GF_BEGIN 标志将接触点的初始开始坐标保存在变量中,并将其作为以下步骤的引用。我们将 ptsLocation 保存在 _ptFirst 变量中。对于缩放手势, ptsLocation 表示缩放的中心。 收到的以下缩放消息由 default case 进行处理。我们将坐标保存在 _ptSecond 变量中。接下来,我们将计算缩放中心点和缩放比例。最后,我们还将更新矩形(我们的图形对象)以反映缩放中心点和缩放比例。 图 显示了这些参数。
GID_ZOOM Switch:
[cpp] view plain copy print ? case GID_ZOOM: switch(gi.dwFlags) { case GF_BEGIN: _dwArguments = LODWORD(gi.ullArguments); _ptFirst.x = gi.ptsLocation.x; _ptFirst.y = gi.ptsLocation.y; ScreenToClient(hWnd,&_ptFirst); break; default: // We read here the second point of the gesture. This is middle point between fingers. _ptSecond.x = gi.ptsLocation.x; _ptSecond.y = gi.ptsLocation.y; ScreenToClient(hWnd,&_ptSecond); // We have to calculate zoom center point ptZoomCenter.x = (_ptFirst.x + _ptSecond.x)/2; ptZoomCenter.y = (_ptFirst.y + _ptSecond.y)/2; // The zoom factor is the ratio between the new and the old distance. k = (double)(LODWORD(gi.ullArguments))/(double)(_dwArguments); // Now we process zooming in/out of the object ProcessZoom(k,ptZoomCenter.x,ptZoomCenter.y); InvalidateRect(hWnd,NULL,TRUE); // Now we have to store new information as a starting information for the next step _ptFirst = _ptSecond; _dwArguments = LODWORD(gi.ullArguments); break; } break; case GID_ZOOM: switch(gi.dwFlags) { case GF_BEGIN: _dwArguments = LODWORD(gi.ullArguments); _ptFirst.x = gi.ptsLocation.x; _ptFirst.y = gi.ptsLocation.y; ScreenToClient(hWnd,&_ptFirst); break; default: // We read here the second point of the gesture. This is middle point between fingers. _ptSecond.x = gi.ptsLocation.x; _ptSecond.y = gi.ptsLocation.y; ScreenToClient(hWnd,&_ptSecond); // We have to calculate zoom center point ptZoomCenter.x = (_ptFirst.x + _ptSecond.x)/2; ptZoomCenter.y = (_ptFirst.y + _ptSecond.y)/2; // The zoom factor is the ratio between the new and the old distance. k = (double)(LODWORD(gi.ullArguments))/(double)(_dwArguments); // Now we process zooming in/out of the object ProcessZoom(k,ptZoomCenter.x,ptZoomCenter.y); InvalidateRect(hWnd,NULL,TRUE); // Now we have to store new information as a starting information for the next step _ptFirst = _ptSecond; _dwArguments = LODWORD(gi.ullArguments); break; } break; 在默认的 case 处理程序中,我们保存手势的位置,从两组点(表示当前接触点和前一个接触点)计算缩放中心位置,并将其存储在 ptZoomCenter 中。我们还通过计算两个点之间的比例来计算出缩放系数。调用 ProcessZoom 帮助函数可以更新新的坐标,以反映缩放系数和中心点。 处理其他 Windows 7 默认手势与以上所述的特定缩放手势处理非常相似。所有手势都遵循相同的流程,只是在每个使用案例场景中,每个手势的内部逻辑实现有所不同。转载于:https://www.cnblogs.com/Dennis-mi/p/3414703.html
相关资源:垃圾分类数据集及代码