winapp.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. // Sample demonstrating interoperability of OpenCV UMat with Direct X surface
  3. // Base class for Windows application
  4. */
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #include <string>
  8. #define WINCLASS "WinAppWnd"
  9. class WinApp
  10. {
  11. public:
  12. WinApp(int width, int height, std::string& window_name)
  13. {
  14. m_width = width;
  15. m_height = height;
  16. m_window_name = window_name;
  17. m_hInstance = ::GetModuleHandle(NULL);
  18. m_hWnd = 0;
  19. }
  20. virtual ~WinApp() {}
  21. virtual int create()
  22. {
  23. WNDCLASSEX wcex;
  24. wcex.cbSize = sizeof(WNDCLASSEX);
  25. wcex.style = CS_HREDRAW | CS_VREDRAW;
  26. wcex.lpfnWndProc = &WinApp::StaticWndProc;
  27. wcex.cbClsExtra = 0;
  28. wcex.cbWndExtra = 0;
  29. wcex.hInstance = m_hInstance;
  30. wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
  31. wcex.hCursor = LoadCursor(0, IDC_ARROW);
  32. wcex.hbrBackground = 0;
  33. wcex.lpszMenuName = 0L;
  34. wcex.lpszClassName = WINCLASS;
  35. wcex.hIconSm = 0;
  36. ATOM wc = ::RegisterClassEx(&wcex);
  37. if (!wc)
  38. return -1;
  39. RECT rc = { 0, 0, m_width, m_height };
  40. if(!::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false))
  41. return -1;
  42. m_hWnd = ::CreateWindow(
  43. (LPCTSTR)wc, m_window_name.c_str(),
  44. WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  45. rc.right - rc.left, rc.bottom - rc.top,
  46. NULL, NULL, m_hInstance, (void*)this);
  47. if (!m_hWnd)
  48. return -1;
  49. ::ShowWindow(m_hWnd, SW_SHOW);
  50. ::UpdateWindow(m_hWnd);
  51. ::SetFocus(m_hWnd);
  52. return 0;
  53. } // create()
  54. int run()
  55. {
  56. MSG msg;
  57. ::ZeroMemory(&msg, sizeof(msg));
  58. while (msg.message != WM_QUIT)
  59. {
  60. if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  61. {
  62. ::TranslateMessage(&msg);
  63. ::DispatchMessage(&msg);
  64. }
  65. else
  66. {
  67. idle();
  68. }
  69. }
  70. return static_cast<int>(msg.wParam);
  71. } // run()
  72. virtual int cleanup()
  73. {
  74. ::DestroyWindow(m_hWnd);
  75. ::UnregisterClass(WINCLASS, m_hInstance);
  76. return 0;
  77. } // cleanup()
  78. protected:
  79. // dispatch message handling to method of class
  80. static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  81. {
  82. WinApp* pWnd;
  83. if (message == WM_NCCREATE)
  84. {
  85. LPCREATESTRUCT pCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
  86. pWnd = static_cast<WinApp*>(pCreateStruct->lpCreateParams);
  87. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
  88. }
  89. pWnd = GetObjectFromWindow(hWnd);
  90. if (pWnd)
  91. return pWnd->WndProc(hWnd, message, wParam, lParam);
  92. else
  93. return ::DefWindowProc(hWnd, message, wParam, lParam);
  94. } // StaticWndProc()
  95. inline static WinApp* GetObjectFromWindow(HWND hWnd) { return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA); }
  96. // actual wnd message handling
  97. virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
  98. // idle processing
  99. virtual int idle() = 0;
  100. HINSTANCE m_hInstance;
  101. HWND m_hWnd;
  102. int m_width;
  103. int m_height;
  104. std::string m_window_name;
  105. };