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))
66 vpWin32Window * window;
68 _declspec(thread) vpWin32Window * window;
71 bool vpWin32Window::registered =
false;
75 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
80 if (!window->isInitialized())
82 window->initialized =
true;
83 vpReleaseSemaphore(window->semaInit,1,NULL);
91 InvalidateRect(window->getHWnd(), NULL, TRUE);
92 UpdateWindow(window->getHWnd());
95 case vpWM_DISPLAY_ROI:
98 typedef struct _half_rect_t{
99 unsigned short left_top;
100 unsigned short right_bottom;
106 hr1 = *((half_rect_t*)(&wParam));
107 hr2 = *((half_rect_t*)(&lParam));
109 rect.left = hr1.left_top;
110 rect.right = hr1.right_bottom;
112 rect.top = hr2.left_top;
113 rect.bottom = hr2.right_bottom;
115 InvalidateRect(window->getHWnd(), &rect, TRUE);
116 UpdateWindow(window->getHWnd());
122 window->clickX = GET_X_LPARAM(lParam);
123 window->clickY = GET_Y_LPARAM(lParam);
126 vpReleaseSemaphore(window->semaClick,1,NULL);
132 window->clickX = GET_X_LPARAM(lParam);
133 window->clickY = GET_Y_LPARAM(lParam);
136 vpReleaseSemaphore(window->semaClick,1,NULL);
142 window->clickX = GET_X_LPARAM(lParam);
143 window->clickY = GET_Y_LPARAM(lParam);
146 vpReleaseSemaphore(window->semaClick,1,NULL);
152 window->clickXUp = GET_X_LPARAM(lParam);
153 window->clickYUp = GET_Y_LPARAM(lParam);
156 vpReleaseSemaphore(window->semaClickUp,1,NULL);
162 window->clickXUp = GET_X_LPARAM(lParam);
163 window->clickYUp = GET_Y_LPARAM(lParam);
166 vpReleaseSemaphore(window->semaClickUp,1,NULL);
172 window->clickXUp = GET_X_LPARAM(lParam);
173 window->clickYUp = GET_Y_LPARAM(lParam);
176 vpReleaseSemaphore(window->semaClickUp,1,NULL);
181 window->coordX = GET_X_LPARAM(lParam);
182 window->coordY = GET_Y_LPARAM(lParam);
183 vpReleaseSemaphore(window->semaMove,1,NULL);
192 GetKeyNameText((LONG)lParam, window->lpString, 10);
194 vpReleaseSemaphore(window->semaKey,1,NULL);
209 window->renderer->render();
212 case vpWM_CLOSEDISPLAY:
222 return DefWindowProc(hWnd, message, wParam, lParam);
230 vpWin32Window::vpWin32Window(vpWin32Renderer * rend): initialized(false)
237 semaInit = CreateSemaphore(NULL,0,1,NULL);
238 semaClick = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
239 semaClickUp = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
240 semaKey = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
241 semaMove = CreateSemaphore(NULL,0,MAX_SEM_COUNT,NULL);
248 vpWin32Window::~vpWin32Window()
251 CloseHandle(semaInit);
252 CloseHandle(semaClick);
253 CloseHandle(semaClickUp);
254 CloseHandle(semaKey);
255 CloseHandle(semaMove);
269 void vpWin32Window::initWindow(
const char* title,
int posx,
int posy,
unsigned int w,
unsigned int h)
272 DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
273 const char g_szClassName[] =
"ViSPWindowClass";
277 rect.right=
static_cast<int>(w);
279 rect.bottom=
static_cast<int>(h);
284 wcex.cbSize =
sizeof(WNDCLASSEX);
286 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
287 wcex.lpfnWndProc = (WNDPROC)WndProc;
290 wcex.hInstance = hInst;
291 wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
292 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
293 wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
294 wcex.lpszMenuName = NULL;
295 wcex.lpszClassName = g_szClassName;
296 wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
298 RegisterClassEx(&wcex);
300 AdjustWindowRectEx(&rect, style,
false, style);
304 hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style,
305 posx, posy, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInst, NULL);
308 DWORD err= GetLastError();
309 std::cout <<
"err CreateWindowEx=" << err << std::endl;
311 "Can't create the window!");
313 SetWindowPos(hWnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
319 renderer->init(hWnd, w, h);
322 ShowWindow(hWnd, SW_SHOWDEFAULT);
331 BOOL val = GetMessage(&msg, NULL, 0, 0);
333 std::cout <<
"GetMessage error:" << GetLastError() << std::endl;
338 if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
340 TranslateMessage(&msg);
341 DispatchMessage(&msg);
Error that can be emited by the vpDisplay class and its derivates.