35 #include <visp3/core/vpConfig.h>
36 #include <visp3/gui/vpWin32API.h>
38 #if (defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9))
40 #define MAX_SEM_COUNT 2147483647
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 #include <visp3/gui/vpWin32Window.h>
50 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
54 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
60 __thread vpWin32Window *window;
62 __declspec(thread) vpWin32Window *window;
65 bool vpWin32Window::registered =
false;
69 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
73 if (window !=
nullptr) {
74 if (!window->isInitialized()) {
75 window->initialized =
true;
76 vpReleaseSemaphore(window->semaInit, 1,
nullptr);
82 InvalidateRect(window->getHWnd(),
nullptr, TRUE);
83 UpdateWindow(window->getHWnd());
86 case vpWM_DISPLAY_ROI: {
89 rect.left = LOWORD(wParam);
90 rect.right = HIWORD(wParam);
92 rect.top = LOWORD(lParam);
93 rect.bottom = HIWORD(lParam);
95 InvalidateRect(window->getHWnd(), &rect, TRUE);
96 UpdateWindow(window->getHWnd());
99 case WM_LBUTTONDOWN: {
100 window->clickX = GET_X_LPARAM(lParam);
101 window->clickY = GET_Y_LPARAM(lParam);
104 vpReleaseSemaphore(window->semaClick, 1,
nullptr);
107 case WM_MBUTTONDOWN: {
108 window->clickX = GET_X_LPARAM(lParam);
109 window->clickY = GET_Y_LPARAM(lParam);
112 vpReleaseSemaphore(window->semaClick, 1,
nullptr);
115 case WM_RBUTTONDOWN: {
116 window->clickX = GET_X_LPARAM(lParam);
117 window->clickY = GET_Y_LPARAM(lParam);
120 vpReleaseSemaphore(window->semaClick, 1,
nullptr);
124 window->clickXUp = GET_X_LPARAM(lParam);
125 window->clickYUp = GET_Y_LPARAM(lParam);
128 vpReleaseSemaphore(window->semaClickUp, 1,
nullptr);
132 window->clickXUp = GET_X_LPARAM(lParam);
133 window->clickYUp = GET_Y_LPARAM(lParam);
136 vpReleaseSemaphore(window->semaClickUp, 1,
nullptr);
140 window->clickXUp = GET_X_LPARAM(lParam);
141 window->clickYUp = GET_Y_LPARAM(lParam);
144 vpReleaseSemaphore(window->semaClickUp, 1,
nullptr);
147 window->coordX = GET_X_LPARAM(lParam);
148 window->coordY = GET_Y_LPARAM(lParam);
149 vpReleaseSemaphore(window->semaMove, 1,
nullptr);
157 GetKeyNameText((LONG)lParam, window->lpString,
160 vpReleaseSemaphore(window->semaKey, 1,
nullptr);
175 window->renderer->render();
178 case vpWM_CLOSEDISPLAY:
188 return DefWindowProc(hWnd, message, wParam, lParam);
196 vpWin32Window::vpWin32Window(vpWin32Renderer *rend) : initialized(false)
204 semaInit = CreateSemaphore(
nullptr, 0, 1,
nullptr);
205 semaClick = CreateSemaphore(
nullptr, 0, MAX_SEM_COUNT,
nullptr);
206 semaClickUp = CreateSemaphore(
nullptr, 0, MAX_SEM_COUNT,
nullptr);
207 semaKey = CreateSemaphore(
nullptr, 0, MAX_SEM_COUNT,
nullptr);
208 semaMove = CreateSemaphore(
nullptr, 0, MAX_SEM_COUNT,
nullptr);
214 vpWin32Window::~vpWin32Window()
217 CloseHandle(semaInit);
218 CloseHandle(semaClick);
219 CloseHandle(semaClickUp);
220 CloseHandle(semaKey);
221 CloseHandle(semaMove);
234 void vpWin32Window::initWindow(
const char *title,
int posx,
int posy,
unsigned int w,
unsigned int h)
237 DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
238 const char g_szClassName[] =
"ViSPWindowClass";
242 rect.right =
static_cast<int>(w);
244 rect.bottom =
static_cast<int>(h);
249 wcex.cbSize =
sizeof(WNDCLASSEX);
251 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE;
252 wcex.lpfnWndProc = (WNDPROC)WndProc;
255 wcex.hInstance = hInst;
256 wcex.hIcon = LoadIcon(
nullptr, IDI_APPLICATION);
257 wcex.hCursor = LoadCursor(
nullptr, IDC_ARROW);
258 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
259 wcex.lpszMenuName =
nullptr;
260 wcex.lpszClassName = g_szClassName;
261 wcex.hIconSm = LoadIcon(
nullptr, IDI_APPLICATION);
263 RegisterClassEx(&wcex);
265 AdjustWindowRectEx(&rect, style,
false, style);
270 hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style, posx, posy, rect.right - rect.left,
271 rect.bottom - rect.top,
nullptr,
nullptr, hInst,
nullptr);
272 if (hWnd ==
nullptr) {
273 DWORD err = GetLastError();
274 std::cout <<
"err CreateWindowEx=" << err << std::endl;
277 SetWindowPos(hWnd,
nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
283 renderer->init(hWnd, w, h);
286 ShowWindow(hWnd, SW_SHOWDEFAULT);
294 BOOL val = GetMessage(&msg,
nullptr, 0, 0);
296 std::cout <<
"GetMessage error:" << GetLastError() << std::endl;
303 if (!TranslateAccelerator(msg.hwnd,
nullptr, &msg)) {
304 TranslateMessage(&msg);
305 DispatchMessage(&msg);
314 #elif !defined(VISP_BUILD_SHARED_LIBS)
316 void dummy_vpWin32Window() { };
Error that can be emitted by the vpDisplay class and its derivatives.
@ cannotOpenWindowError
Unable to open display window.