Browse Source

Examples: DirectX9/10/11: Tweak usage of SetCapture/ReleaseCapture. (#1375)
ps: DirectX 12 example (#302) may want to adopt that as well.

omar 7 years ago
parent
commit
d2c65aa3e8

+ 19 - 16
examples/directx10_example/imgui_impl_dx10.cpp

@@ -234,35 +234,38 @@ static bool IsAnyMouseButtonDown()
     return false;
 }
 
+// We use the Win32 capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
 IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
     ImGuiIO& io = ImGui::GetIO();
     switch (msg)
     {
     case WM_LBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[0] = true;
-        return 0;
     case WM_RBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[1] = true;
-        return 0;
     case WM_MBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[2] = true;
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONDOWN) button = 0;
+        if (msg == WM_RBUTTONDOWN) button = 1;
+        if (msg == WM_MBUTTONDOWN) button = 2;
+        if (!IsAnyMouseButtonDown() && GetCapture() == NULL)
+            SetCapture(hwnd);
+        io.MouseDown[button] = true;
         return 0;
+    }
     case WM_LBUTTONUP:
-        io.MouseDown[0] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_RBUTTONUP:
-        io.MouseDown[1] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_MBUTTONUP:
-        io.MouseDown[2] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONUP) button = 0;
+        if (msg == WM_RBUTTONUP) button = 1;
+        if (msg == WM_MBUTTONUP) button = 2;
+        io.MouseDown[button] = false;
+        if (!IsAnyMouseButtonDown() && GetCapture() == hwnd)
+            ReleaseCapture();
         return 0;
+    }
     case WM_MOUSEWHEEL:
         io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
         return 0;

+ 19 - 16
examples/directx11_example/imgui_impl_dx11.cpp

@@ -241,35 +241,38 @@ static bool IsAnyMouseButtonDown()
     return false;
 }
 
+// We use the Win32 capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
 IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
     ImGuiIO& io = ImGui::GetIO();
     switch (msg)
     {
     case WM_LBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[0] = true;
-        return 0;
     case WM_RBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[1] = true;
-        return 0;
     case WM_MBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[2] = true;
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONDOWN) button = 0;
+        if (msg == WM_RBUTTONDOWN) button = 1;
+        if (msg == WM_MBUTTONDOWN) button = 2;
+        if (!IsAnyMouseButtonDown() && GetCapture() == NULL)
+            SetCapture(hwnd);
+        io.MouseDown[button] = true;
         return 0;
+    }
     case WM_LBUTTONUP:
-        io.MouseDown[0] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_RBUTTONUP:
-        io.MouseDown[1] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_MBUTTONUP:
-        io.MouseDown[2] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONUP) button = 0;
+        if (msg == WM_RBUTTONUP) button = 1;
+        if (msg == WM_MBUTTONUP) button = 2;
+        io.MouseDown[button] = false;
+        if (!IsAnyMouseButtonDown() && GetCapture() == hwnd)
+            ReleaseCapture();
         return 0;
+    }
     case WM_MOUSEWHEEL:
         io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
         return 0;

+ 19 - 17
examples/directx9_example/imgui_impl_dx9.cpp

@@ -180,36 +180,38 @@ static bool IsAnyMouseButtonDown()
     return false;
 }
 
-// We use Win32 SetCapture/ReleaseCapture() API to enable reading the mouse outside our Windows bounds.
+// We use the Win32 capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
 IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
     ImGuiIO& io = ImGui::GetIO();
     switch (msg)
     {
     case WM_LBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[0] = true;
-        return 0;
     case WM_RBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[1] = true;
-        return 0;
     case WM_MBUTTONDOWN:
-        if (!IsAnyMouseButtonDown()) ::SetCapture(hwnd);
-        io.MouseDown[2] = true;
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONDOWN) button = 0;
+        if (msg == WM_RBUTTONDOWN) button = 1;
+        if (msg == WM_MBUTTONDOWN) button = 2;
+        if (!IsAnyMouseButtonDown() && GetCapture() == NULL)
+            SetCapture(hwnd);
+        io.MouseDown[button] = true;
         return 0;
+    }
     case WM_LBUTTONUP:
-        io.MouseDown[0] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_RBUTTONUP:
-        io.MouseDown[1] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
-        return 0;
     case WM_MBUTTONUP:
-        io.MouseDown[2] = false;
-        if (!IsAnyMouseButtonDown()) ::ReleaseCapture();
+    {
+        int button = 0;
+        if (msg == WM_LBUTTONUP) button = 0;
+        if (msg == WM_RBUTTONUP) button = 1;
+        if (msg == WM_MBUTTONUP) button = 2;
+        io.MouseDown[button] = false;
+        if (!IsAnyMouseButtonDown() && GetCapture() == hwnd)
+            ReleaseCapture();
         return 0;
+    }
     case WM_MOUSEWHEEL:
         io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
         return 0;