Home | History | Annotate | Download | only in directx
      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 
      9 
     10 #define WINCLASS "WinAppWnd"
     11 
     12 class WinApp
     13 {
     14 public:
     15     WinApp(int width, int height, std::string& window_name)
     16     {
     17         m_width       = width;
     18         m_height      = height;
     19         m_window_name = window_name;
     20         m_hInstance   = ::GetModuleHandle(NULL);
     21         m_hWnd        = 0;
     22     }
     23 
     24     virtual ~WinApp() {}
     25 
     26 
     27     virtual int create()
     28     {
     29         WNDCLASSEX wcex;
     30 
     31         wcex.cbSize        = sizeof(WNDCLASSEX);
     32         wcex.style         = CS_HREDRAW | CS_VREDRAW;
     33         wcex.lpfnWndProc   = &WinApp::StaticWndProc;
     34         wcex.cbClsExtra    = 0;
     35         wcex.cbWndExtra    = 0;
     36         wcex.hInstance     = m_hInstance;
     37         wcex.hIcon         = LoadIcon(0, IDI_APPLICATION);
     38         wcex.hCursor       = LoadCursor(0, IDC_ARROW);
     39         wcex.hbrBackground = 0;
     40         wcex.lpszMenuName  = 0L;
     41         wcex.lpszClassName = WINCLASS;
     42         wcex.hIconSm       = 0;
     43 
     44         ATOM wc = ::RegisterClassEx(&wcex);
     45         if (!wc)
     46             return -1;
     47 
     48         RECT rc = { 0, 0, m_width, m_height };
     49         if(!::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false))
     50             return -1;
     51 
     52         m_hWnd = ::CreateWindow(
     53                      (LPCTSTR)wc, m_window_name.c_str(),
     54                      WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
     55                      rc.right - rc.left, rc.bottom - rc.top,
     56                      NULL, NULL, m_hInstance, (void*)this);
     57 
     58         if (!m_hWnd)
     59             return -1;
     60 
     61         ::ShowWindow(m_hWnd, SW_SHOW);
     62         ::UpdateWindow(m_hWnd);
     63         ::SetFocus(m_hWnd);
     64 
     65         return 0;
     66     } // create()
     67 
     68 
     69     int run()
     70     {
     71         MSG msg;
     72 
     73         ::ZeroMemory(&msg, sizeof(msg));
     74 
     75         while (msg.message != WM_QUIT)
     76         {
     77             if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
     78             {
     79                 ::TranslateMessage(&msg);
     80                 ::DispatchMessage(&msg);
     81             }
     82             else
     83             {
     84                 idle();
     85             }
     86         }
     87 
     88         return static_cast<int>(msg.wParam);
     89     } // run()
     90 
     91 
     92     virtual int cleanup()
     93     {
     94         ::DestroyWindow(m_hWnd);
     95         ::UnregisterClass(WINCLASS, m_hInstance);
     96         return 0;
     97     } // cleanup()
     98 
     99 protected:
    100     // dispatch message handling to method of class
    101     static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    102     {
    103         WinApp* pWnd;
    104 
    105         if (message == WM_NCCREATE)
    106         {
    107             LPCREATESTRUCT pCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
    108             pWnd = static_cast<WinApp*>(pCreateStruct->lpCreateParams);
    109             ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
    110         }
    111 
    112         pWnd = GetObjectFromWindow(hWnd);
    113 
    114         if (pWnd)
    115             return pWnd->WndProc(hWnd, message, wParam, lParam);
    116         else
    117             return ::DefWindowProc(hWnd, message, wParam, lParam);
    118     } // StaticWndProc()
    119 
    120     inline static WinApp* GetObjectFromWindow(HWND hWnd) { return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA); }
    121 
    122     // actual wnd message handling
    123     virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
    124     // idle processing
    125     virtual int idle() = 0;
    126 
    127     HINSTANCE   m_hInstance;
    128     HWND        m_hWnd;
    129     int         m_width;
    130     int         m_height;
    131     std::string m_window_name;
    132 };
    133