Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImGui_ImplWin32_UpdateMousePos BugFix #2087

Open
1s0t opened this issue Sep 17, 2018 · 6 comments
Open

ImGui_ImplWin32_UpdateMousePos BugFix #2087

1s0t opened this issue Sep 17, 2018 · 6 comments
Labels

Comments

@1s0t
Copy link

1s0t commented Sep 17, 2018

v1.66
Back-ends: ImGui_ImplWin32_UpdateMousePos
OS: win10 pro
Compiler:VS2017

I found a bug in ImGui_ImplWin32_UpdateMousePos more specific the windows function GetActiveWindow. It fails in some cases to get the window without any error codes so i re coded it using GetGUIThreadInfo instead and that managed to return the active window handle. I have no experience with github so i cant push this so i am giving the fix here.

// ImGui v1.66
static void ImGui_ImplWin32_UpdateMousePos()
{
   ImGuiIO& io = ImGui::GetIO();

   // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
   if (io.WantSetMousePos)
   {
       POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
       ::ClientToScreen(g_hWnd, &pos);
       ::SetCursorPos(pos.x, pos.y);
   }

   // Set mouse position
   io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
   POINT pos;
   
   // Getting active window handle from the window threads processID # Bacause ::GetActiveWindow() fails in some cases.
   GUITHREADINFO guiThreadInfo; guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
   GetGUIThreadInfo(GetWindowThreadProcessId(g_hWnd, 0), &guiThreadInfo);

   if (guiThreadInfo.hwndActive == g_hWnd && ::GetCursorPos(&pos))
       if (::ScreenToClient(g_hWnd, &pos))
           io.MousePos = ImVec2((float)pos.x, (float)pos.y);
}
@ocornut
Copy link
Owner

ocornut commented Sep 17, 2018

Could you explain/justify in greater in which situation ::GetActiveWindow() may fails.

(Linking this to #1951.)

@1s0t
Copy link
Author

1s0t commented Sep 17, 2018

I am hooking into an app and when the app loads the 3d Environment GetActiveWindow fails. I am not sure whats happening but GetActiveWindow fails and GetForegroundWindow is not a good solution. I read somewhere that you do it like this because you wanna be able to drag outside of the clientarea. And that's cool and if that wasnt the case i think putting it back to being handled inside the WndProc would be the best way. But doing it like im doing it here i think is the best solution to this problem. I dont think that checking witch window the mouse is in is that good as Nukem9 does here #1951 Because if theres an overlay then that could possibly trigger on that window instead.

@Epicccccccccc
Copy link

thank you this was troubling me for a while

@ghost
Copy link

ghost commented Nov 12, 2018

I can confirm this issue when hooking into the Unity Editor as well as my Unity game. Your bugfix allows mouse input to get through as intended.

ocornut added a commit that referenced this issue Jan 15, 2019
…ow() to be compatible with windows created in a different thread. (#1951, #2087, #2156, #2232) [many people]
@ocornut
Copy link
Owner

ocornut commented Jan 15, 2019

@skillessgamer Today I have pushed a change to use GetForegroundWindow(), howver your last post mention:

I am hooking into an app and when the app loads the 3d Environment GetActiveWindow fails. I am not sure whats happening but GetActiveWindow fails and GetForegroundWindow is not a good solution.

Could you clarify why GetForegroundWindow is not a good solution?

@1s0t
Copy link
Author

1s0t commented Jan 17, 2019

@ocornut GetForegroundWindow() will return the topmost window if i am not mistaken. That will be an issue if you have a overlay of some sort. My solution will get the active window from the executable ImGui runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants