网页上的3D蓝图

This item was filled under [ 新闻News ]

不需要Flash,不需要Silverlight,浏览器本身就能做出性能不差的3D应用,甚至是像魔兽世界那样的大型在线3D游戏?在现在看来,这只是梦想,不过,WebGL正在拉近梦想与现实间的距离……

Web图形渲染的革命

8月初,图形行业标准协会Khronos集团透露:Web页面调用OpenGL的标准——WebGL即将完成,并将于2010年初正式发布。这条貌似不起眼的、充满了技术化背景的新闻,很快引起了业界的巨大反响,因为它将给浏览器带来巨大的用户体验方面的影响。

Web应用不断扩大的市场份额显示着它在未来的重要性,而图形元素则是任何一张有吸引力的Web页面都不可或缺的。随着Web应用的进一步复杂化,普通的平面静态图形已不能满足应用的需要。于是,用于Web页面的交互式三维动画应运而生。

但早期的技术非常不成熟,比如Java Applet实现的简单交互式三维动画,不仅需要下载一个巨大的支持环境,而且画面非常粗糙,性能也很差。究其原因,就在于早期的这些交互式三维动画在做 图形渲染时,并没有直接利用到图形硬件本身所带的加速功能,也就是,即使安装了性能很高的显卡,对于Web页面的交互式三维动画的显示也起不了什么作用。

网页上的3D蓝图

现在主流的Web交互式动画由Flash等浏览器插件调用DirectX、OpenGL等操作系统图形接口实现图形渲染,调用了硬件加速。但缺点是必须下载插件

网页上的3D蓝图

WebGL直接以OpenGL接口实现HTML5的canvas标签调用,以统一的OpenGL标准,从Web脚本生成利用硬件加速功能的Web交互式3D动画的图形渲染

后来,Adobe的Flash Player浏览器插件几乎在一夜之间,占据了Web交互式三维动画的半壁江山。和Java Applet不同的是,它直接利用操作系统提供的图形应用程序接口,来调用图形硬件的加速功能,实现了高性能的图形渲染。这么一来,交互式三维动画几乎可 以瞬间加载完成,而且对于相当复杂的交互和三维动画而言,它都表现不俗。

可是,这样的解决方案也有不少的问题。首先,它是通过浏览器插件来实现的,这就意味着对于每种不同的操作系统和浏览器的组合,都需要下载特定版 本的插件,对于手持设备上运行的比较特殊的操作系统或浏览器,就可能没有对应版本的插件;其次,对于操作系统上的图形应用程序接口的调用,它并不是遵循一 个公开的标准。比如,在Windows平台上,Flash是通过调用DirectX来实现,而在Mac OS X上则是调用AGL。

这两点不足,很大程度上限制了Web交互式三维动画的应用范围。微软推出的Silverlight,本质上也无法解决这个问题。因此,开发一种 无需浏览器插件支持、通过统一的图形应用程序接口调用来实现Web交互式三维动画图形渲染的技术,就变得很有必要,而这正是WebGL的革命意义所在。

尽管HTML5中引入了canvas标记,这个标记本身支持Web交互式三维动画的制作,但是,如果没有WebGL的支持,它并不能调用图形硬 件加速功能。因此,此前虽然很多浏览器支持canvas标记,但由于性能不够好,无法得到广泛的应用。可是,一旦加入了WebGL的支持,它的面貌就焕然 一新了。

WebGL完美地解决了现有的Web交互式三维动画的两个问题:第一,它通过HTML脚本本身实现Web交互式三维动画的制作,无需任何浏览器插件支持;第二,它利用底层的图形硬件加速功能进行的图形渲染,是通过统一的、标准的、跨平台的OpenGL接口实现的。

这意味着,以后不需要再通过任何浏览器插件,仅仅用HTML和Javascript,就可以制作出性能丝毫不亚于现在用Flash、Silverlight等做出来的Web交互式三维动画,而且在任何平台上都能以同样的方式运作,这是多么巨大的改进!

浏览器巨头反应不一

率先宣布在浏览器引擎中加入WebGL支持的,是作为苹果Safari和谷歌Chrome浏览器基础的WebKit引擎。谷歌Chrome近期 发布的最新版本3中,就包含了这个引擎。而Firefox也不甘示弱,紧跟其后在自己的Gecko引擎中加入了对WebGL的支持,虽然还没有正式发布, 但 Firefox已经在产品页面中大力宣传了这个新特性。

而微软对此则反应较为冷淡,微软的有关人员曾提到“浏览器插件的模式将长期存在,微软的Silverlight产品的最新版本已经可以在各种主 流平台,包括非微软操作系统的浏览器平台,以及各种主流手持设备上使用”。事实上,由于WebGL和OpenGL都是开源组织的产品,这也就注定了微软对 它的反应会比较谨慎。

微软的Internet Explorer浏览器采用的是Trident引擎(又称MSHTML),已经有很多年历史,最近刚刚经历了数次重写以支持新的Internet Explorer 7和8以及在开发过程中的9。以微软自家产品大一统的思想,对于这种别的行业协会标准,反应从来都是比较落后的。然而,Windows Mobile最近的销售遇到了较大问题,这和它封闭式的浏览器体验不能不说没有关系。

而采用了独立式的Presto引擎的浏览器厂商Opera,表示自己会“非常关注”WebGL,但并未明确表示会在什么时候加入对它的支持。

WebGL带来的新未来

由于WebGL是一个开放式的标准,它的出现将打破现有的浏览器图形插件的市场格局。原先下载Flash和Silverlight似乎是并不需 要理由的事,可是现在根本不需要它们也可以一样实现高性能的Web交互式三维动画,那么,是否要下载、下载哪些插件就成了一种选择。

可以预见,大量带有Web交互式三维动画的网页会推出“无插件版本”,而对于那些在单位上网,没有管理员权限以安装插件的用户,或是使用的设备 比较特别,没有对应插件版本的用户来说,WebGL是个福音,现在他们可以和所有人一样,打开浏览器就能享受Web交互式三维动画提供的丰富Web应用 了。

对于Web开发人员来说,这意味着页面内容开发成本的进一步降低,他们可以很容易地做到同一段代码在跨平台体验上的一致性。大量的基于WebGL的库的出现,也会把在桌面应用中的开发经验很好地迁移到Web应用的开发中去。

最重要的是,由于Web交互式三维动画成了页面的一部分,而不是作为单独的一个插件而存在,它与页面上其他内容的交互性就会更强。对于网络社区和大型在线游戏来说,这是必不可少的。

相信在不久的将来,利用WebGL,我们在浏览器里就可以玩到像《魔兽世界》这样的大型在线游戏,而且它是跨平台、跨

Silverlight 3 多点触摸功能在Windows 7 中的应用

This item was filled under [ 新闻News ]

伴随着计算机变得越来越智能化,越来越无所不能,触摸屏的普及只是时间问题了。 虽然鼠标和键盘不 会很快就离开人们的视野,毕竟人们使用鼠标跟键盘已经成为一种习惯,但是处理信息或者说操作计算机的其他方法也层出不穷——比如触控技术。从硬件技术的角 度来讲,触控技术是可行的,随着最新一代的触摸屏技术,接触技术现在已经存在,如今Web开发人员可以利用Silverlight 3提供多点触摸功能。可惜的是,只有Windows 7同时支持Silverlight和多点触摸能力。这个因素大大制约了这部分功能的运用和推广,不过如果多点触摸继续流行开来的话,情况会有所改变的,不 过现在Windows 7为触摸屏技术提供了发挥的软件空间,Windows 7让屏幕触控技术成为可能。

借助 Windows 7 和触摸感应屏幕,您只需使用手指即可在电脑上翻阅在线报纸,翻阅相册,拖拽文件和文件夹。 多年来在 Windows 中早已开始采用触控技术。Windows 7 进一步将其扩展到电脑的每个角落。“开始”菜单和任务栏现在都采用了加大显示、易于手指触摸的图标。常用的 Windows 7 程序也都可以执行触摸操作。您甚至可以在“画图”中使用手指来画图!

Windows 触控功能还可以识别多点触控(使用适当的监视器)。是否需要缩小图片将手指捏在一起即可。是否要用鼠标右键单击某项内容?用一个手指触及该内容,并用第二 个手指点击屏幕即可。Windows 触控功能仅在 Windows 7 的家庭高级版、专业版和旗舰版中提供。

什么是触控操作呢?触 控指 Windows 允许你使用手指直接与计算机进行交互的方式。与使用鼠标、键盘或手写笔相比,触摸更加方便、自然、具有吸引力。也更符合人们日常的交流习惯。而 Windows 7中引入了全新的多点触摸的概念。多点触摸又称多点触控,简而言之可以理解为一个屏幕多点操作。多点触摸不但是两个点或者几个点同时应用到屏幕上这么一点 点便利,由于是多点触摸,所以他能感应到手指滑动的快慢以及力度(力度用触摸点的多少转换来实现),从而操作系统应 用起来更加人性化。传统的触控屏幕一次只能判断一个触控点,若同时有两个以上的点被触碰,就不能做出正确反应,或者说反应混乱了。多重触控的任务可以分解 为两个方面的工作,一是同时采集多点信号,二是对每路信号的意义进行判断,也就是所谓的手势识别。最早在Apple公司的iPhone上应用。多点触摸技 术是一项划时代的输入方式。可以设想随着全息投影的发展,完全有可能实现屏幕在空中投影,而用户直接在投影中触控电脑,科幻电影中的场景将会变成现实。

Windows 7触控界面

许多触摸交互都通过手势(gesture)和划动(flick)来完成的。手势是指手指在屏幕上的快速移动,计算机会将其解释为命令而非鼠标移动、书写或绘图。

划动:“划动”是一个简单的手势,用于执行导航或编辑命令。屏幕上方的轻弹屏幕可以回翻一页,而在屏幕下方的轻弹可以向下翻一页。屏幕左右的轻弹分别可以控制页面导航向前向后。编辑划动手势包括复制、粘贴、撤销和删除。

图1  编辑划动手势

轻击与双击:点击和点击释放是最基本的两种触控操作方式。双击也可以打开文件和文件夹。这比鼠标操作宽容方便了许多,方便用户随处使用。

拖动:在屏幕上点击并拖动,这就好比用鼠标拖拽文件到某个文件夹,或者左右滑动选中某些文字。

图2  拖拽文件夹

卷动:向上或向下拖动滚动窗口的内容。这是最常用的功能,但不是使用滚动条。

图3  滚动条功能

放大: 用两个手指同时触摸一个文件然后两手指拆开,从而使图片或者文档放大。在使用一个笔记本电脑看照片或阅读文件时这个功能非常实用。

缩放: 用两个手指同时触摸一个文件或者图片,然后缩紧达到缩放效果。

旋转:用手指轻触一幅图片,像旋转一幅真实图片一样旋转它就达到旋转效果。

长触暂停:这相当于用鼠标右键单击。 用手指触摸屏幕,直到一个圆圈出现之后,松开手指。对于需要精准移动或悬停的任务,Windows 7提供了“触摸指示器”,一个看起来像鼠标的浮动屏幕指示器。因为触摸指示器并不易于用作直接输入,因此可触摸程序应当避免对触摸指示器的依赖。

异步长触: 用一根手指轻触屏幕并用第2指轻击,如同用鼠标选中某个文件并点击鼠标右键。

画笔:你可以使用手指当画笔在画布上作画,无拘无束,再也不用忍受鼠标作画的苦恼了。

图4  画图功能

Windows 7为触控技术做了特别的优化:

程序的交互控件足够大以使用户能够容易触摸——至少 23×23 像素(13×13 对话框单位)。

程序对相关的如划动、多点触摸操纵及拖放等系统手势的功能都可以正常使用。

所有控件都使用 Microsoft Active Accessibility (MSAA) 来提供用于辅助技术用户界面的可编程访问。

任务是为方便触摸而设计的,将最常操作的命令直接放在用户界面或内容上,而不是放在下拉菜单内。

该程序特别设计为具有沉浸式的触摸体验,在细节方面具有真实世界的物理特性反馈,如动量或摩擦。

任务是宽容的,允许用户方便地纠正其错误并处理不精确的触摸和拖放。

任务是为避免或减少文本输入或选择而设计的。

多点触控代表了输入方式的未来。随着Windows 7的普及,相信越来越多的厂商将会加入多点触控的行列。

开启多点触摸功能

首先,需要一台支持触摸特性的PC,目前市面上可选的产品主要有:

*HP TouchSmart All-in-One PCs (IQ500 series & IQ800 series)

* HP TouchSmart tx2 Tablet PC

* Dell Latitude XT Tablet PC

其次,需要安装最新的多点触摸驱动以使得屏幕能够识别触摸指示。不过这些驱动目前都是Beta阶段,而在正式发布之前都必须通过WHQL测试,我们目前无法保证所有预装Windows 7的机器都装有WHQL驱动。

* HP TouchSmart All-in-One PCs :这款机器的驱动只需通过Windows Update即可完成

* Dell Latitude XT and HP TouchSmart tx2 Tablet PCs:可以在N-Trig’s website上下载

其实在Windows 7Beta中,很多特性都是与多点触摸相关的。

比如画图中的油漆刷就可以用手指来控制了,不过支持的手指数目取决于机器对多点触摸的支持,有些只支持2个,有些可以达到4个设置更多。

Windows 7画笔

还有IE8中,用户可以用手指在任意地方向上或向下拖拽滚动网页,还可以用手指向左/右甩以完成前进/后退操作。另外,任务栏也是触摸优化过的,触摸的话用手指拖拽就可以打开Jumplists了。

Jumplists

而如果想要放大屏幕的话,开始-搜索display,在打开的控制面板中选择即可。

图5  打开Jumplists程序
Tagged with: [ , ]

微软今发布Windows7首个重要安全更新

This item was filled under [ 新闻News ]

微软于上周四宣布了将在本周二(也就是今天)通过Windows自动升级程序发布一个补丁程序,该程序中包含了13个安全补丁,能够解决多达34个 受新漏洞影响的应用,包括Windows系统,IE浏览器,Office,Silverlight,Forefront,Developer Tools,以及 SQL Server等。

其实这是很平常的微软每月安全公告,然而这次受人关注的情况是因为,本月的安全公告中包括了5个Windows 7系统的安全补丁, 需要特别指出的是其中还包括了一个“紧急”和一个“重要”级别的安全补丁。紧急漏洞补丁是用于修复IE8浏览器的,微软的安全项目经理Jerry Bryant强调了此次更新的重要性,并说道:“一般情况下我们是不会详细预先通告安全更新的,但我们认为此次的更新对于广大用户来说具有重要的指导意 义,用户可以提前进行相应的规划和部署尽快下载这些更新。”

当然,这也是Windows 7操作系统发布以来第一个重要的安全补丁包,Windows XP和Vista系统将下载到数量更多的安全补丁。目前Windows 7正式版已经即将要发布,国内预售也正在火热进行中,Windows 7中文版以全球最低预售价吸引了大量用户的眼球,不少网友早已在自己的电脑上安装了Windows 7 RTM版,如果你就是其中之一那么请注意下载升级及时更新。

微软安全中心列出的此次更新的13个安全补丁有:

Bulletin 1: Critical (Remote Code Execution), Windows
Bulletin 2: Critical (Remote Code Execution), Windows
Bulletin 3: Critical (Remote Code Execution), Windows
Bulletin 4: Critical (Remote Code Execution), Windows
Bulletin 5: Critical (Remote Code Execution), Windows, Internet Explorer
Bulletin 6: Critical (Remote Code Execution), Windows
Bulletin 7: Important (Spoofing), Windows
Bulletin 8: Important (Remote Code Execution), Windows
Bulletin 9: Important (Elevation of Privilege), Windows
Bulletin 10: Important (Denial of Service), Windows
Bulletin 11: Critical (Remote Code Execution), Office
Bulletin 12: Critical (Remote Code Execution), Windows, Silverlight
Bulletin 13: Critical (Remote Code Execution), Windows, Office, SQL Server, Developer Tools, Forefront■

MultiTouch Capabilities in Windows 7

This item was filled under [ 新闻News ]
This article discusses:

  • Multitouch Programming Models
  • Gestures
  • Raw Touch Messages
This article uses the following technologies:
Windows 7
This article is based on a prerelease version of Windows 7. Details are subject to change.
This is the third article in a series of articles about Windows 7. The series focuses on new user experiences that developers can tap into to make their applications shine on Windows 7. Part 1 covered Libraries. Part 2 covered Taskbar APIs. Part 3 covers multitouch capabilities in Windows 7. Download Windows 7 Release Candidate now to help you get the most out of this article.
Introduction to Windows 7 Multitouch
In Windows 7 we have enriched the Windows experience with touch, making touch a first-class citizen as another way to interact with your PC alongside the mouse and keyboard. In recent years, we have witnessed a wide range of multitouch devices that have generated an extremely positive user experience. Therefore, it is only natural for Windows to introduce such multitouch support in Windows 7 as a core capability.
With the Windows 7 Multitouch Platform, you have the freedom to directly interact with your computer. For example, you are able to reach out and slowly scroll through your pictures directly from Windows Explorer, or flick and move through them quickly. It is important to understand that we didn’t create a special Windows 7 Multitouch Shell. There is no special Windows Explorer that is available only on multitouch devices. The simplest example is the Windows 7 Taskbar Jump Lists. When you use the mouse to right-click on any icon on the Taskbar, you see its corresponding Jump List. For example, right-clicking on the Windows Live Messenger icon shows Live Messenger’s Jump List. But how can you right-click using multitouch? Simply touch the Live Messenger icon and drag out with your finger, as shown in Figure 1.
fig01.gif

Figure 1 Using Multitouch on Live Messenger’s Jump List
Performing that drag gesture shows Live Messenger’s Jump Lists. As you can see in Figure 2, the touch-triggered Jump List displays the same content as in the standard right-click Jump List. The right-side image shows the Live Messenger’s Jump List using touch. The spacing between each item in the multitouch-enabled Jump List is larger than the spacing between each item in the left image, which is the default right-click Jump List.
fig01.gif

Figure 2 Multitouch and Standard Views of Jump Lists
The Windows Live Messenger is just one example where Windows 7 doesn’t create a new set of UIs just for touch scenarios, but instead makes it blend into the existing infrastructure. The Taskbar is only one example of many multitouch optimized experiences that ship with Windows 7, like the XPS viewer, Windows Photo Viewer, and IE8.
Windows 7 Multitouch Platform Programming Models
To provide well-rounded Windows Touch solutions for all kind of applications, the Windows Touch Platform provides various levels of support. There are several scenarios by which you can enhance applications using the Windows Touch Platform features. Before you adopt a specific approach, you should consider what exactly you want to do with your application.
Legacy Support Let’s assume you already have an existing application with a large install base. You might ask yourself, what will be my users multitouch experience when running the application on a Windows 7 multitouch-enabled computer? The good news is that the Windows 7 Multitouch Platform provides a free out-of-the-box experience for applications that are touch-unaware and were not designed to support multitouch. Specifically, it provides free, out-of-the-box support for a few basic gestures. In other words, you can expect a few basic gestures to work and to have the desired effect in your application. Basic gestures include single finger or two fingers panning, two fingers zoom, and flick gestures that were introduced in the Windows Vista time frame.
Adding Basic Multitouch Support Here we focus on adding direct gestures support, as well as other behavior, and user interface changes to make applications more touch-friendly beyond simple gesture support.
One example that we’ve already reviewed near the beginning of this article is the touch- optimized Taskbar Jump Lists. By using the getMessageExtraInfo method, the Taskbar can trace the origin of the input message and determine whether it is a touch message and then respond accordingly.
In addition, you can use gestures to enhance your application and provide better multitouch support. Applications that directly respond to gestures have full control over how they behave when a user is touching the touch-enabled device. For example, Windows 7 ships with the Windows Photo Viewer. In the Photo Viewer application, the viewer receives specific information about the location that the zoom gesture originates from. That is, the zoom gesture contains information about the center point–specific X and Y coordinates–of the zoom gesture, and then the Photo Viewer can focus around the center of the gesture. The Windows Photo Viewer application also uses panning and rotation gestures to provide a very good image viewing experience, with relatively little effort.
With gestures, you can also override the default panning behavior. For example, the default touch scrolling is designed to work in text-centric windows that scroll primarily vertically, like Web pages or documents; dragging horizontally does text selection rather than scrolling. In most applications, this works just fine. But what if your application actually needs to support horizontal scrolling? Also, for some applications the default scroll can appear chunky, going too fast or too slow. With gestures support, you can override the default panning behavior and optimize it for your application’s needs.
Experience Optimized for Multitouch The best-case scenario is when applications are designed from the ground up to support multitouch. These applications build on top of the Windows Messages Touch message, WM_TOUCH. This message provides raw touch data to the application, and you can consume these messages and handle multiple touch points. Most gestures that we mentioned previously are two-finger gestures, where with WM_TOUCH messages you can receive as many simultaneous touch points as your underlying touch sensitive hardware supports.
The Windows 7 Multitouch Platform also provides manipulation and inertia processors to help you interpret the touch messages. Think of manipulation as a black-box that receives as input the object that is being touched and all the related touch messages. The result is a 2D affine transform matrix representing the transformation that happened as a result of the finger movement. For instance, if you were writing a photo-editing application, you could grab two photos at the same time using however many fingers you wanted to rotate, resize, and translate the photos, and the manipulation process provides the changes you need to reflect on the object.
Inertia provides a very basic physics model for applications and provides you with an easy way to continue the smooth transition of an object even after you picked up your fingers from the touch- sensitive device, to create a simple transition effect rather than stopping the object on the spot.
Working with Gestures
Whenever the user touches a touch-sensitive Windows 7 enabled device, the Windows 7 Multitouch Platform sends gesture messages, WM_GESTURE, to your application by default. This is the free out-of-the-box behavior, and you will need to opt-out if you wish to stop receiving such messages.
Gestures are considered as one or two fingers touch inputs that translate into some kind of predefined action (gesture) that the user performs. Once detected (the OS is doing all the work for you), the OS will send a gesture message to your application. This message contains all the information needed for decoding and making it work. Windows 7 support the following gestures:

  • Zoom
  • Single finger and two fingers pan
  • Rotate
  • Two fingers tap
  • Press and tap
Handling WM_Gesture Messages In order to work with gestures, you will need to handle the WM_GESTURE messages that are sent to your application. If you are a Win32 programmer, you can check for WM_GESTURE messages in your application’s WndProc function.
The WM_GESTURE is the generic message used for all gestures. Therefore, in order to determine which gesture you need to handle, first you need to decode the gesture message. The information about gestures is found in the lParam parameter, and you need to use a special function, GetGestureInfo, to decode the gesture message, as shown in the following code snippet.

GESTUREINFO gi;
ZeroMemory(&gi, sizeof(GESTUREINFO));
gi.cbSize = sizeof(gi);
BOOL bResult = GetGestureInfo((HGESTUREINFO)lParam, &gi);

After obtaining a GESTUREINFO structure, you can check the dwID to identify which gesture was performed. The GESTUREINFO structure contains several other important members:

  • cbSize – the size of the structure, in bytes
  • ptsLocation – A POINTS structure containing the coordinates associated with the gesture. These coordinates are always relative to the origin of the screen
  • dwFlags – the state of the gesture such as begin, inertia and end
  • ullArguments – a 64-bit unsigned integer that contains the arguments for gestures that fit into eight bytes. This is the extra information that is unique for each gesture type
With this knowledge, we can now move forward and write the complete switch-case method that handles all gestures, as shown in Figure 3.

Figure 3 Switch-Case Method

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);
}

Please note that at the end of the function we call the CloseGestureInfoHandle function that closes resources associated with the gesture information handler. If you handle the WM_GESTURE message, it is your responsibility for closing the handle using this function. Failure to do so may result in memory leaks.
Handling gesture messages has a fixed flow that includes configuration, decoding the gesture message, and handling the specific gestures according to your application needs. As you can see in the preceding code, it is not that difficult to do so.
Now let’s review in detail the Zoom gestures, which will give you an idea of what all other gestures might look like.
Use the Zoom Gesture to Scale an Object

Zoom gesture is usually recognized by users as a “pinch” movement between two touch points, where you move your fingers closer to each other to zoom out, and move your fingers farther apart to zoom in and enlarge the content. The zoom gesture allows you to scale the size of your objects. Figure 4 illustrates how the zoom gesture works.

fig01.gif

Figure 4 Zoom Gesture
Now, let’s see what code you need to implement in your GID_ZOOM switch to achieve the desired zooming effect.
The gesture info-structure includes the dwFlags member that is used to determine the state of the gesture and can include any of the following values:

  • GF_BEGIN – indicates that the gesture is starting, received in the first WM_Gesture message
  • GF_INERTIA – indicates that the gesture has triggered inertia
  • GF_END – indicates that the gesture has finished
  • The default of the switch – indicates the rest of the gesture message and is usually referred to as the delta
We will use the GF_BEGIN flag to save the initial start coordinates of the touch point into variables as a reference for the following steps. We save the ptsLocation into the _ptFirst variable. For zoom gesture, the ptsLocation indicates the center of the zoom.
The following zoom message that arrives is handled by the default case. We save the coordinates into the _ptSecond variable. Next, we calculate the zoom center point, the zoom, and last we update rectangular (our graphic object) to reflect the zoom center point and zooming ratio. Figure 5 showcases these arguments.

Figure 5 GID_ZOOM Switch

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;

In the default case handler, we save the location of the gesture, and from the two sets of points (representing the current touch point and the previous one) we calculate the zoom center location and store it in ptZoomCenter. We also calculate the zoom factor by calculating the ratio between the two points. A call to the ProcessZoom helper function updates the new coordinates to reflect the zoom factor and center point.
Handling the rest of the Windows 7 default gestures is very similar to the specific zoom gesture handling described above. All gestures follow the same flow, and just the internal logical implementation differs per gesture, per use-case scenario. Next, we review the optimal model and dive into the API that allows you to receive and handle raw touch events.
Working with Windows Raw Touch Messages
In order to start receiving raw touch messages, WM_TOUCH, first you need to ask the OS to start sending touch messages to your application and stop sending the default gesture messages. To do so, you need to call the RegisterTouchWindow(HWND hWnd, ULONG uFlags) function. Calling this function registers a single hWnd element (usually a window) as being touch-enabled.
As with gestures, you handle WM_TOUCH messages in your application’s WndProc function. A single WM_TOUCH message can contain several different “touch-points messages” that need to be unpacked into an array of touch input structures. As standard practice, you want to unpack the WM_TOUCH message into array of TOUCHINPUT structures, where each structure in that array represents data from a single touch point. To unpack, you need to call the GetTouchInputInfo(HTOUCHINPUT hTouchInput, UINT cInputs, PTOUCHINPUT pInputs, int cbSize) function and pass it the lParam of the WM_TOUCH message and a newly created touch points array, as shown in Figure 6.

Figure 6 Unpacking WM_TOUCH

case WM_TOUCH:
{
    unsigned int numInputs = (unsigned int) wParam;
    TOUCHINPUT* ti = new TOUCHINPUT[numInputs];
    if(GetTouchInputInfo((HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT)))
    {
        // Handle each contact point
        for(unsigned int i=0; i< numInputs; ++i)
        {
           /* handle ti[i]  */
        }
    }
    CloseTouchInputHandle((HTOUCHINPUT)lParam);
    delete [] ti;
}
break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);

Here you can see how we fill the TOUCHPOINT ti array with the data from each touch point. Next, we iterate through the touch points array, applying our logic to each touch point, the handle ti[i] comment. Last, we need to clean the touch handle by calling CloseTouchInputHandle(HTOUCHINPUT hTouchInput), passing the original WinProc’s lParam. Failing to do so will result in memory leaks.
The preceding code represents the first step in handling WM_TOUCH messages. A single touch input structure, TOUCHINPUT, contains all the necessary information about a single touch point that you will need to work with:

  • dwID – is the touch point identifier that distinguishes a particular touch input from the other
  • dwFlags – is a set of bit flags that specify the state of the touch point
  • X and Y coordinate of the touch point (basically the location of each touch point)
  • dwTime – is the time step for the event, in milliseconds
  • dwMask – a set of bit flags that specify which of the optional fields in the structure contain valid values
It is important to note that the X and Y coordinates are in hundredths of a pixel of physical screen coordinates (i.e., centa-pixel). This extra-fine resolution promotes high precision and accurate handwrite recognition for other applications that may require such fine resolution. But for most scenarios, you need to remember to divide the touch point X and Y coordinates by a hundred to translate the touch point coordinates to usable screen coordinates before you start using these coordinates.
By now you know how to handle touch messages and you have all the information to go and add real logic to our WM_TOUCH handler described above. Let’s use this knowledge and build a multitouch paint application, also known as Scratch Pad.
Tracking Touch Point IDs To create the Scratch Pad application, you need to track each touch point movement and the path that it forms, and then paint a line along that path. To distinguish between the different touch points and to make sure you really handle each touch point correctly, we assign different color to each touch point.
After unpacking the touch message into an array of touch input structures, ti, you need to check each touch point state and apply different logic per touch state. In the Scratch Pad example, new touch points are indentified by the down state, TOUCHEVENTF_DOWN. You register the new touch point ID and assign it a color. Once a touch point is removed, TOUCHEVENTF_UP, you complete the last paint and unregister the touch point ID. In between down and up events, you most likely will get a lot of move messages, TOUCHEVENTF_MOVE. For each move message, you add a new point to the existing line and paint the new segment of the line. Figure 7 shows the entire WM_TOUCH handler that is required for the Scratch Pad application to support multitouch.

Figure 7 WM_TOUCH Handler

case WM_TOUCH:
{
    unsigned int numInputs = (unsigned int) wParam;
    TOUCHINPUT* ti = new TOUCHINPUT[numInputs];
    if(GetTouchInputInfo((HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT)))
    {
        // For each contact, dispatch the message to the appropriate message handler.
        for(unsigned int i=0; i< numInputs; ++i)
        {
            if(ti[i].dwFlags & TOUCHEVENTF_DOWN)
            {
               OnTouchDownHandler(hWnd, ti[i]);
            }
            else if(ti[i].dwFlags & TOUCHEVENTF_MOVE)
            {
               OnTouchMoveHandler(hWnd, ti[i]);
            }
            else if(ti[i].dwFlags & TOUCHEVENTF_UP)
            {
               OnTouchUpHandler(hWnd, ti[i]);
            }
        }
    }
    CloseTouchInputHandle((HTOUCHINPUT)lParam);
    delete [] ti;
}
break;

The key for tracking individual touch points is using the dwID that remains the same through the duration of specific touch stroke. In the OnTouchDownHandler helper function, you assign this ID to a CStroke object that is basically an array of points that represents a line. This line is the path form when you drag your finger across the touch-sensitive device. We are not going to cover the entire code sample that supports the application and actually paints the lines to the screen. Basically, all that you need to do in order to support multitouch can be found in the preceding code sample.
You can view the output of the Scratch Pad application in Figure 8.
fig01.gif

Figure 8 Output of the Scratch Application
In the default case handler, we save the location of the gesture, and from the two sets of points (representing the current touch point and the previous one) we calculate the zoom center location and store it in ptZoomCenter. We also calculate the zoom factor by calculating the ratio between the two points. A call to the ProcessZoom helper function updates the new coordinates to reflect the zoom factor and center point.
Handling the rest of the Windows 7 default gestures is very similar to the specific zoom gesture handling described above. All gestures follow the same flow, and just the internal logical implementation differs per gesture, per use-case scenario. Next, we review the optimal model and dive into the API that allows you to receive and handle raw touch events.
Working with Windows Raw Touch Messages
In order to start receiving raw touch messages, WM_TOUCH, first you need to ask the OS to start sending touch messages to your application and stop sending the default gesture messages. To do so, you need to call the RegisterTouchWindow(HWND hWnd, ULONG uFlags) function. Calling this function registers a single hWnd element (usually a window) as being touch-enabled.
As with gestures, you handle WM_TOUCH messages in your application’s WndProc function. A single WM_TOUCH message can contain several different “touch-points messages” that need to be unpacked into an array of touch input structures. As standard practice, you want to unpack the WM_TOUCH message into array of TOUCHINPUT structures, where each structure in that array represents data from a single touch point. To unpack, you need to call the GetTouchInputInfo(HTOUCHINPUT hTouchInput, UINT cInputs, PTOUCHINPUT pInputs, int cbSize) function and pass it the lParam of the WM_TOUCH message and a newly created touch points array, as shown in Figure 6.
Summary
The Windows 7 Multitouch Platform is a very powerful development platform. From implementing the default gesture support to the more advanced raw touch messages, it gives you a lot of power with relatively simple implementation.
The platform also includes manipulation and inertia processors. Manipulations are similar to gestures in a lot of ways, but they are a lot more powerful. Manipulations are used to simplify transformation operations on any given number of objects. You can perform a combination of specific component gestures such as rotate, zoom, and scale on a specific object at the same time. The manipulation processor yields a two dimensional “transformation matrix” that represents the transformations in X and Y coordinates, the scale change, and the rotation that occurred to the object over time as a result of the performed movement of the touch points. Once the last touch point is pulled up, you may want to apply simple physics to the object so it smoothly comes to a halt, rather than it abruptly stopping on the spot. To support that smooth motion, the Windows 7 multitouch platform provides the Inertia API.
These APIs will be the topic of our next MSDN article.
Yochay Kiriaty is a Technical Evangelist at Microsoft, focusing on Windows 7. He has more than a decade of experience in software development. He has written and taught academic computer science courses and is an active contributor to The Windows Blog.
Tagged with: [ , ]

Silverlight 3把多点触摸带进Web世界

This item was filled under [ 新闻News ]

Web开发人员可以利用Silverlight 3提供多点触摸功能。可惜的是,只有Windows 7同时支持Silverlight和多点触摸能力。这个因素大大制约了这部分功能的运用和推广,不过如果多点触摸继续流行开来的话,情况会有所改变的。

在应用程序中添加多点触摸的功能非常简单。对于基本的操作,只要响应Touch.FrameReported事件就足够了。这个事件会包含一系列触摸点,以及用户触摸,移动或拿开了哪些手指的信息。Kevin Wolf在他的博客上公布了一个快速入门示例

Kevin Wolf还提供了一个多点触摸的游戏Crazy Coins。除了Silverlight 3以外,这个应用程序还使用了SliverSprite框架及Farseer Physics类库。

查看英文原文:Silverlight 3 is Bringing Multi-Touch to the Web