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) (static_cast<int>(static_cast<short>(LOWORD(lp))))
54#define GET_Y_LPARAM(lp) (static_cast<int>(static_cast<short>(HIWORD(lp))))
60__thread vpWin32Window *window;
62__declspec(thread) vpWin32Window *window;
65bool vpWin32Window::registered =
false;
69LRESULT 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);
196vpWin32Window::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);
214vpWin32Window::vpWin32Window(
const vpWin32Window &window)
222vpWin32Window &vpWin32Window::operator=(
const vpWin32Window &window)
224 hInst = window.hInst;
226 initialized = window.initialized;
227 semaInit = window.semaInit;
228 semaClick = window.semaClick;
229 semaClickUp = window.semaClickUp;
230 semaKey = window.semaKey;
231 semaMove = window.semaMove;
232 clickX = window.clickX;
233 clickXUp = window.clickXUp;
234 clickY = window.clickY;
235 clickYUp = window.clickYUp;
236 coordX = window.coordX;
237 coordY = window.coordY;
238 clickButton = window.clickButton;
239 clickButtonUp = window.clickButtonUp;
240 registered = window.registered;
241 renderer = window.renderer;
248vpWin32Window::~vpWin32Window()
251 CloseHandle(semaInit);
252 CloseHandle(semaClick);
253 CloseHandle(semaClickUp);
254 CloseHandle(semaKey);
255 CloseHandle(semaMove);
268void vpWin32Window::initWindow(
const char *title,
int posx,
int posy,
unsigned int w,
unsigned int h)
271 DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
272 const char g_szClassName[] =
"ViSPWindowClass";
276 rect.right =
static_cast<int>(
w);
278 rect.bottom =
static_cast<int>(
h);
283 wcex.cbSize =
sizeof(WNDCLASSEX);
285 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE;
286 wcex.lpfnWndProc = (WNDPROC)WndProc;
289 wcex.hInstance = hInst;
290 wcex.hIcon = LoadIcon(
nullptr, IDI_APPLICATION);
291 wcex.hCursor = LoadCursor(
nullptr, IDC_ARROW);
292 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
293 wcex.lpszMenuName =
nullptr;
294 wcex.lpszClassName = g_szClassName;
295 wcex.hIconSm = LoadIcon(
nullptr, IDI_APPLICATION);
297 RegisterClassEx(&wcex);
299 AdjustWindowRectEx(&rect, style,
false, style);
304 hWnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, title, style, posx, posy, rect.right - rect.left,
305 rect.bottom - rect.top,
nullptr,
nullptr, hInst,
nullptr);
306 if (hWnd ==
nullptr) {
307 DWORD err = GetLastError();
308 std::cout <<
"err CreateWindowEx=" << err << std::endl;
311 SetWindowPos(hWnd,
nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
317 renderer->init(hWnd, w, h);
320 ShowWindow(hWnd, SW_SHOWDEFAULT);
328 BOOL val = GetMessage(&msg,
nullptr, 0, 0);
330 std::cout <<
"GetMessage error:" << GetLastError() << std::endl;
337 if (!TranslateAccelerator(msg.hwnd,
nullptr, &msg)) {
338 TranslateMessage(&msg);
339 DispatchMessage(&msg);
348#elif !defined(VISP_BUILD_SHARED_LIBS)
350void dummy_vpWin32Window() { }
Error that can be emitted by the vpDisplay class and its derivatives.
@ cannotOpenWindowError
Unable to open display window.