ManipulationHelper.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "pch.h"
  2. #include <windows.h>
  3. #include <assert.h>
  4. #define ASSERT assert
  5. #include <stdio.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include "ManipulationEventSink.h"
  9. #include "ManipulationHelper.h"
  10. namespace ManipulationHelper
  11. {
  12. ManipulationHelper::ManipulationHelper(HWND wndHandle)
  13. {
  14. //开启控制台
  15. //AllocConsole();
  16. //FILE* stream;
  17. //freopen_s(&stream, "CONOUT$", "w", stdout);
  18. m_wndHandle = wndHandle;
  19. //初始化当前线程的COM库
  20. HRESULT hr = CoInitialize(0);
  21. if (FAILED(hr))
  22. {
  23. ASSERT(SUCCEEDED(hr) && L"Failed to execute CoInitialize");
  24. return;
  25. }
  26. // Perform application initialization
  27. if (!InitInstance())
  28. {
  29. CoUninitialize();
  30. return;
  31. }
  32. //printf("Init OK \n");
  33. InitOkFlag = TRUE;
  34. }
  35. ManipulationHelper::~ManipulationHelper()
  36. {
  37. //关闭控制台
  38. //FreeConsole();
  39. if (InitOkFlag)
  40. {
  41. CoUninitialize();
  42. }
  43. }
  44. int ManipulationHelper::ProcessDown(MANIPULATOR_ID manipulatorId, FLOAT x, FLOAT y)
  45. {
  46. return m_pIManipProc->ProcessDown(manipulatorId, x, y);
  47. }
  48. int ManipulationHelper::ProcessMove(MANIPULATOR_ID manipulatorId, FLOAT x, FLOAT y)
  49. {
  50. return m_pIManipProc->ProcessMove(manipulatorId, x, y);
  51. }
  52. int ManipulationHelper::ProcessUp(MANIPULATOR_ID manipulatorId, FLOAT x, FLOAT y)
  53. {
  54. return m_pIManipProc->ProcessUp(manipulatorId, x, y);
  55. }
  56. void ManipulationHelper::SetManipulationStartedCallBack(ManipulationStartedCallBack msCallback)
  57. {
  58. m_pManipulationEventSink->SetManipulationStartedCallBack(msCallback);
  59. }
  60. void ManipulationHelper::SetManipulationDeltaCallBack(ManipulationDeltaCallBack mdCallback)
  61. {
  62. m_pManipulationEventSink->SetManipulationDeltaCallBack(mdCallback);
  63. }
  64. void ManipulationHelper::SetManipulationCompletedCallBack(ManipulationCompletedCallBack mcCallback)
  65. {
  66. m_pManipulationEventSink->SetManipulationCompletedCallBack(mcCallback);
  67. }
  68. BOOL ManipulationHelper::InitInstance()
  69. {
  70. // Register application window for receiving multi-touch input. Use default settings.
  71. if (!RegisterTouchWindow(m_wndHandle, 0))
  72. {
  73. MessageBox(m_wndHandle, L"Cannot register application window for multi-touch input", L"Error", MB_OK);
  74. return FALSE;
  75. }
  76. ASSERT(IsTouchWindow(m_wndHandle, NULL));
  77. // Instantiate the ManipulationProcessor object
  78. HRESULT hr = CoCreateInstance(__uuidof(ManipulationProcessor), NULL, CLSCTX_ALL, IID_PPV_ARGS(&m_pIManipProc));
  79. if (FAILED(hr))
  80. {
  81. ASSERT(SUCCEEDED(hr) && L"InitInstance: failed to instantiate the ManipulationProcessor object");
  82. return FALSE;
  83. }
  84. // Instantiate the event sink with the manipulation processor and pointer to the rectangle object
  85. m_pManipulationEventSink = new ManipulationEventSink();
  86. if (m_pManipulationEventSink == NULL)
  87. {
  88. ASSERT(m_pManipulationEventSink && L"InitInstance: failed to instantiate the CManipulationEventSink class");
  89. m_pIManipProc->Release();
  90. m_pIManipProc = NULL;
  91. return FALSE;
  92. }
  93. // Establish the link between ManipulationEventSink and ManipulationProcessor
  94. if (!m_pManipulationEventSink->Connect(m_pIManipProc))
  95. {
  96. ASSERT(FALSE && L"InitInstance: failed to connect ManipulationEventSink and ManipulationProcessor");
  97. m_pIManipProc->Release();
  98. m_pIManipProc = NULL;
  99. m_pManipulationEventSink->Release();
  100. m_pManipulationEventSink = NULL;
  101. return FALSE;
  102. }
  103. return TRUE;
  104. }
  105. }