Multitouch WMTouch with MFCNative.docx
- 文档编号:24099239
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:17
- 大小:649.84KB
Multitouch WMTouch with MFCNative.docx
《Multitouch WMTouch with MFCNative.docx》由会员分享,可在线阅读,更多相关《Multitouch WMTouch with MFCNative.docx(17页珍藏版)》请在冰豆网上搜索。
MultitouchWMTouchwithMFCNative
Hands-OnLab
MultitouchWMTouchwithMFC-Native
Labversion:
1.0.0
Lastupdated:
9/25/2018
CONTENTS
Overview3
Exercise1:
BuildaMultitouchApplication5
Task1–PreparetheApplication5
Task2–AddTouchSupporttotheapplication8
Task3–AddtheStrokeSourceandHeaderFilestotheProject,andDrawLineswithyourFingers11
Summary16
Overview
Windows7givesuserstheabilitytomanageapplicationswiththetouchoftheirfingers,usingnointermediatedevice.Thisexpandsthestylus-basedcapabilitiesoftabletPCs.Unlikeotherpointingdevices,thisnewcapabilityallowsmultipleinputeventsatthesametimefromdifferentpointinglocations,anditenablescomplexscenarios,suchasmanagingapplicationswithtenfingersorwithmultiplesimultaneoususers.However,topullthisoff,wehavetoadaptourapplication'suserinterfaceandbehaviortosupportthisnewinputmodel.
MFCinVisualStudio2010hasaddedsupportforcheckingMultitouchhardwarereadinessandsimplifiedtheprocessofreceivingtouchevents.
Objectives
InthisHands-OnLab,youwilllearnhowtomanageMultitouchevents,including:
ProcessinginputfromWindowsTouch
Understandingtheimplicationsofmanipulatingmultipletoucheventssimultaneously
CheckingforMultitouchhardwareexistenceandreadiness
SystemRequirements
Youmusthavethefollowingitemstocompletethislab:
Windows7
MicrosoftVisualStudio2010Beta2(orlater)
AMultitouchhardwaredevice
Introduction
TocreateaMultitouchdrivenapplicationyoucanchooseoneofthreeapproaches:
Good,Better,andBest.
The“Good”approachistheeasiestofthethree.Youshoulddesignyourapplicationuserinterfacewithtouchabilityinmind.UselargeandcleanWin32basedcontrolsthatmakeanaturalinterfaceforbetteruserexperience.TouchabilitiessuchasscrollingcomefromtheWin32controls.Thereisnoneedforextrawork.Forexample,trytoscrollthedocumentthatyouarereadingnowwithyourfingers!
Thisisthe“Good”approach.
The"Better"approachletsthesystemreceivevariouslow-leveltoucheventsandpassestheresultoftheheuristicsthatthesystemperformswiththeseeventstoyourapplicationas“gestures”.Forexample,iftheusermakesarotationmovementonthescreen,thesystemwillissuearotationgestureeventwiththerotationangle.Althoughthe"Better"approachiseasytouse,ithasitslimitations.UsinggesturesonecannotgetRotate,Translate,andScalesimultaneously.Alsoyoucannothandlemanydifferenttouch-basedactionsatthesametime.ForexampletwousersthatoperatedifferentareasoftheWindow.
The“Best”approachistoreadthelow-leveltoucheventsastheinputtotheapplication.Applicationslike“Piano”orcomplexcontrolslikemultipleslidersthatcanbeoperatedsimultaneouslyaregoodexamples.Forexample,runMSPaint,selectadrawingtoolfromthegalleryanddrawwithfourofyourfingers.
InthisHands-OnLabyouwillmimicthenewMSPaintMultitouchpaintingfeatureusingthe“Best”approach.Wewillreadandusetherawtouchevents.
AbouttheMultitouchScratchpadApplication
TheMultitouchScratchpadapplicationpresentsasimplewindowthatallowssimultaneouslydrawingofcontinuouslineswithyourfingers.WhilethefolderSource\MFC_WMTouchSource\Startercontainsfilesneededfortheexercise,Source\MFC_WMTouchSource\Finalcontainsthecompletedsolution.
Exercise1:
BuildaMultitouchApplication
Task1–PreparetheApplication
1.StartVisualStudio2010
2.CreateanewMFCapplicationprojectandgiveitthenameScratchPad:
3.IntheApplicationType,selectSingleDocument.Tokeeptheapplicationsimple,unselecttheotheroptionsinthedialogsimilartothescreen-shotsbelow:
4.ContinueclickingNextuntilyoufinallyhitFinish:
Task2–AddTouchSupporttotheapplication
5.Theapplicationthatwearebuildingrequirestouch-enabledhardware,soweneedtomakethischeckintheapplication.
6.InScratchpad.cpp,addthefollowingcheckattheendofCScratchPadApp:
:
InitInstance():
C++
BYTEdigitizerStatus=(BYTE)GetSystemMetrics(SM_DIGITIZER);
if((digitizerStatus&(0x80+0x40))==0)//StackReady+MultiTouch
{
AfxMessageBox(L"Notouchinputiscurrentlyavailable.");
returnFALSE;
}
BYTEnInputs=(BYTE)GetSystemMetrics(SM_MAXIMUMTOUCHES);
CStringstr;
str.Format(L"Touchinputavailablewith%dtouchpoints.",nInputs);
AfxMessageBox(str);
returnTRUE;
7.Youcanseethatbesidescheckingfortouchavailabilityandreadinesswealsofindoutthenumberoftouchinputsthatthehardwaresupports.
8.Compileandrun.
9.Dependingonthenumberoftouchinputsyouhaveonyourmachine,youshouldseeoutputsimilartothis:
10.InordertoregistertheapplicationclientviewwindowtoreceiveTouchmessages,weneedtocalltheMFCfunctionCWnd:
:
RegisterTouchWindow().We’lldosoaftertheviewhasbeencreated;i.e.intheOnCreate()eventhandler.
SwitchtotheClassViewandSelecttheCChildViewclass.
InthePropertiespage,gototheMessagepropertysheetandnavigatetoWM_CREATE,thenaddtheOnCreate()messagehandlerfromthedropdownbox:
11.InsidetheCChildView:
:
OnCreate()handlerappendthefollowingcodetoregistertouchinputfortheviewwindow:
C++
if(!
RegisterTouchWindow())
{
ASSERT(FALSE);
}
Note:
CallingCWnd:
:
RegisterTouchWindow()registers(andunregisters)awindowastouchcapable,allowingittoreceivelow-levelWM_TOUCHmessages.
12.Sincewe’veregisteredtheviewtoreceivetouchinput,wemustoverridethehandlerreceivingeachofthetouchmessages:
CWnd:
:
OnTouchInput().
ThishandlerreceivesasingleinputfromWindowsTouchandshouldreturnTRUEiftheapplicationprocessesthismessage;otherwiseFALSE.
13.InChildView.h,addthismethoddeclaration:
C++
//Overrides
protected:
virtualBOOLOnTouchInput(CPointpt,intnInputNumber,intnInputsCount,PTOUCHINPUTpInput);
14.AndinChildView.cpp,providethecorrespondingimplementation:
C++
BOOLCChildView:
:
OnTouchInput(CPointpt,intnInputNumber,intnInputsCount,PTOUCHINPUTpInput)
{
//TODO:
HandleTocuhinputmessageshere
returnFALSE;
}
Task3–AddtheStrokeSourceandHeaderFilestotheProject,andDrawLineswithyourFingers
Wewouldliketouseourfingersasmultipleinputdevices.Wewanttodrawalineforeachfingerthattouchesthescreen.Todothatwearegoingtousetwostrokecollections.Onecollectionholdsthefinishedstrokes(lines)andanothercollectionholdstheongoing,currentlypaintinglines.Eachfingerthattouchesthescreenaddspointstoastrokeinthem_StrkColDrawingcollection.Whenweraisethefingerfromthescreen,wemovethefinger'sstrokefromthem_StrkColDrawingtothem_StrkColFinishedcollection.Inadditionwewantstrokestohavedifferentcolorsifauserisusingtwoormorefingersonamultitouchmonitor.
15.IntheStarterfolderyouwillfindtwofiles:
Stroke.handStroke.cpp.Copythemtotheprojectfolderanduse“AddExistingitem…”toaddthemtotheproject.
16.Similarly,addStrokeCollection.handStrokeCollection.cpptotheproject.
17.Include"Stroke.h"and"StrokeCollection.h"attheendofStdAfx.hheaderfile.
C++
#include"Stroke.h"
#include"StrokeCollection.h"
18.AddtheseprivatemembervariabledefinitionstoChildView.h:
C++
private:
intm_iCurrColor;//Thecurrentstrokecolor
CStrokeCollectionm_StrkColFinished;//Theuserfinishedenteringstrokes
//afteruserliftedhisorherfinger.
CStrokeCollectionm_StrkColDrawing;//TheStrokescollectiontheuseris
//currentlydrawing.
19.Important:
wehavetoinitializethecurrentcolor.We’lldosointheCChildViewconstructorinChildView.cpp:
C++
CChildView:
:
CChildView():
m_iCurrColor(0)
{
}
20.Todrawthefinishedcollection,weaddthefollowingcalltotheendoftheCChildView:
:
OnPaint()handler.Itwilldrawallfinishedstrokes.
C++
m_StrkColFinished.Draw(&dc);
21.WeneedtoprocesseachTouchinputmessagereceived,sowehandleeachoftheeventswe’reinterestedin:
touchinputdown,moveandup.
22.InCChildView.h,declarethefollowingmethods,whichwe’llusetohandledifferenttouchinputevents:
C++
protected:
//Handlersfordifferenttouchinputevents
BOOLOnTouchInputDown(CPointpt,PTOUCHINPUTpInput);
BOOLOnTouchInputMove(CPointpt,PTOUCHINPUTpInput);
BOOLOnTouchInputUp(CPointpt,PTOUCHINPUTpInput);
23.InCChildView.cpp,addtheimplementationofeachofthetouchinputhandlers:
C++
BOOLCChildView:
:
OnTouchInputDown(CPointpt,PTOUCHINPUTpInput)
{
//Createnewstrokeandaddpointtoit.
COLORREFstrokeColor=GetTouchColor((pInput->dwFlags&TOUCHEVENTF_PRIMARY)!
=0);
CStroke*pStrkNew=newCStroke(pInput->dwID,strokeColor);
pStrkNew->Add(pt);
//Addnewstroketothecollectionofstrokesindrawing.
m_StrkColDrawing.Add(pStrkNew);
returnTRUE;
}
BOOLCChildView:
:
OnTouchInputMove(CPointpt,PTOUCHINPUTpInput)
{
//Findthestrokeinthecollectionofthestrokesindrawing.
intstrokeIndex=m_StrkColDrawing.FindStrokeById(pInput->dwID);
if(strokeIndex>=0)
{
CStroke*pStrk=m_StrkColDrawing[strokeIndex];
//Addcontactpointtothestroke
pStrk->Add(pt);
//Drawthelaststroke
pStrk->Draw(GetDC());
}
returnTRUE;
}
BOOLCChildView:
:
OnTouchInputUp(CPointpt,PTOUCHINPUTpInput)
{
//Findthestrokeinthecollectionofthestrokesindrawing.
intstrokeIndex=m_StrkColDrawing.FindStrokeById(pInput->dwID);
if(strokeIndex>=0)
{
CStroke*pStrkCopy=m_StrkColDrawing[strokeIndex];
//Removethisstrokefromthecollectionofstrokesindrawing.
m_StrkColDrawing.RemoveAt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Multitouch WMTouch with MFC Native