這是一個很簡單的範例, 首先, 產生一個MFC的Dialog, 當有touch發生時, 視窗的畫面會出現message box, 部分程式碼如下所示:
// Step 1: 宣告這個視窗接收 WM_TOUCH訊息
BEGIN_MESSAGE_MAP(CMFCTouchDlg, CDialog)
ON_MESSAGE(WM_TOUCH, OnTouch)
END_MESSAGE_MAP()
BOOL CMFCTouchDlg::OnInitDialog()
{
CDialog::OnInitDialog();
RegisterTouchWindow(this->GetSafeHwnd(), 0);
... ... ...
}
// Step 3: 加入實作 (當視窗收到 WM_TOUCH 指令時, 呼叫下面 method)
LRESULT CMFCTouchDlg::OnTouch(WPARAM wParam,LPARAM lParam)
{
::AfxMessageBox(L"Touch!!",0,0);
return 0;
}
執行畫面如下所示:
另一個範例則是當有手勢作用時, MFC dialog則會顯示手勢類型的message, 程式碼如下所示:
// Step 1: 宣告這個視窗接收 WM_GESTURE訊息
BEGIN_MESSAGE_MAP(CMFCTouchDlg, CDialog)
ON_MESSAGE(WM_GESTURE, OnGesture)
END_MESSAGE_MAP()
// Step 2: 開啟Rotate手勢
BOOL CMFCTouchDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Enable rotate gesture
GESTURECONFIG config = { 0 };
config.dwWant = GC_ROTATE;
config.dwID = GID_ROTATE;
config.dwBlock = 0;
BOOL result = SetGestureConfig(
this->GetSafeHwnd(),
0,
1,
&config,
sizeof(GESTURECONFIG));
... ... ...
}
// Step 3: 加入實作 (當視窗收到 WM_GESTURE 指令時, 呼叫下面 method)
LRESULT CMFCTouchDlg::OnGesture(WPARAM wParam,LPARAM lParam)
{
GESTUREINFO gi;
ZeroMemory(&gi,sizeof(GESTUREINFO));
gi.cbSize = sizeof(GESTUREINFO);
BOOL result = GetGestureInfo((HGESTUREINFO)lParam, &gi);
if(result)
{
switch(gi.dwID)
{
case GID_PAN:
::AfxMessageBox(L"Pan",0,0);
break;
case GID_ZOOM:
::AfxMessageBox(L"Zoom",0,0);
break;
case GID_ROTATE:
::AfxMessageBox(L"ROTATE",0,0);
break;
case GID_TWOFINGERTAP:
::AfxMessageBox(L"Two Finger Tap",0,0);
break;
case GID_PRESSANDTAP:
::AfxMessageBox(L"Press and Tap",0,0);
break;
}
}
return 0;
}
執行畫面如下所示: