42 #include <visp/vpConfig.h>
44 #include <visp/vpWin32API.h>
46 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
48 #define MAX_SEM_COUNT 2147483647
50 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 #include <visp/vpWin32Window.h>
56 # define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
60 # define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
65 _declspec(thread) vpWin32Window * window;
67 bool vpWin32Window::registered = false;
71 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
76 if (!window->isInitialized())
78 window->initialized =
true;
79 vpReleaseSemaphore(window->semaInit,1,NULL);
87 InvalidateRect(window->getHWnd(), NULL, TRUE);
88 UpdateWindow(window->getHWnd());
91 case vpWM_DISPLAY_ROI:
94 typedef struct _half_rect_t{
95 unsigned short left_top;
96 unsigned short right_bottom;
102 hr1 = *((half_rect_t*)(&wParam));
103 hr2 = *((half_rect_t*)(&lParam));
105 rect.left = hr1.left_top;
106 rect.right = hr1.right_bottom;
108 rect.top = hr2.left_top;
109 rect.bottom = hr2.right_bottom;
111 InvalidateRect(window->getHWnd(), &rect, TRUE);
112 UpdateWindow(window->getHWnd());
118 window->clickX = GET_X_LPARAM(lParam);
119 window->clickY = GET_Y_LPARAM(lParam);
122 vpReleaseSemaphore(window->semaClick,1,NULL);
128 window->clickX = GET_X_LPARAM(lParam);
129 window->clickY = GET_Y_LPARAM(lParam);
132 vpReleaseSemaphore(window->semaClick,1,NULL);
138 window->clickX = GET_X_LPARAM(lParam);
139 window->clickY = GET_Y_LPARAM(lParam);
142 vpReleaseSemaphore(window->semaClick,1,NULL);
148 window->clickXUp = GET_X_LPARAM(lParam);
149 window->clickYUp = GET_Y_LPARAM(lParam);
152 vpReleaseSemaphore(window->semaClickUp,1,NULL);
158 window->clickXUp = GET_X_LPARAM(lParam);
159 window->clickYUp = GET_Y_LPARAM(lParam);
162 vpReleaseSemaphore(window->semaClickUp,1,NULL);
168 window->clickXUp = GET_X_LPARAM(lParam);
169 window->clickYUp = GET_Y_LPARAM(lParam);
172 vpReleaseSemaphore(window->semaClickUp,1,NULL);
177 window->coordX = GET_X_LPARAM(lParam);
178 window->coordY = GET_Y_LPARAM(lParam);
179 vpReleaseSemaphore(window->semaMove,1,NULL);
188 GetKeyNameText((LONG)lParam, window->lpString, 10);
190 vpReleaseSemaphore(window->semaKey,1,NULL);
205 window->renderer->render();
208 case vpWM_CLOSEDISPLAY:
218 return DefWindowProc(hWnd, message, wParam, lParam);
226 vpWin32Window::vpWin32Window(vpWin32Renderer * rend): initialized(false)
233 semaInit = CreateSemaphore(NULL,0,1,NULL);
234 semaClick = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
235 semaClickUp = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
236 semaKey = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
237 semaMove = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
244 vpWin32Window::~vpWin32Window()
247 CloseHandle(semaInit);
248 CloseHandle(semaClick);
249 CloseHandle(semaClickUp);
250 CloseHandle(semaKey);
251 CloseHandle(semaMove);
265 void vpWin32Window::initWindow(
const char* title,
int posx,
int posy,
unsigned int w,
unsigned int h)
268 DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
269 const char g_szClassName[] =
"ViSPWindowClass";
273 rect.right=
static_cast<int>(w);
275 rect.bottom=
static_cast<int>(h);
280 wcex.cbSize =
sizeof(WNDCLASSEX);
282 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
283 wcex.lpfnWndProc = (WNDPROC)WndProc;
286 wcex.hInstance = hInst;
287 wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
288 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
289 wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
290 wcex.lpszMenuName = NULL;
291 wcex.lpszClassName = g_szClassName;
292 wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
294 RegisterClassEx(&wcex);
296 AdjustWindowRectEx(&rect, style,
false, style);
300 hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
301 posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
304 DWORD err= GetLastError();
305 std::cout <<
"err CreateWindowEx=" << err << std::endl;
307 "Can't create the window!");
309 SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
315 renderer->init(hWnd, w, h);
318 ShowWindow(hWnd, SW_SHOWDEFAULT);
327 BOOL val = GetMessage(&msg, NULL, 0, 0);
329 std::cout <<
"GetMessage error:" << GetLastError() << std::endl;
334 if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
336 TranslateMessage(&msg);
337 DispatchMessage(&msg);
Error that can be emited by the vpDisplay class and its derivates.