DX11 Win32 Implementation | Mouse cursor slightly offset

Before I get criticized, yes I read the two other posts about the problem having to do with io.DisplaySize
but I have checked and verified that the size of both coordinates is correct.

Probably missing something really simple and I will kick myself for it later. The current setup I am using is mostly from the DX11 Win32 example code.

I have disabled resizing both the imgui window and the application window so the issue shouldn’t have to do with not updating io.Displaysize correctly. Also it looks like the gap between the real mouse position and the position imgui uses for the mouse increases as you get farther from the origin.

The two text renders show the results of GetWindowRect and io.DisplaySize

ImGui::Text("[%d | %d | %d | %d]", rect.top, rect.bottom, rect.left, rect.right);
ImGui::Text("[%f | %f]", io.DisplaySize.x, io.DisplaySize.y);

If you used the default font if it would be easier to tell if your display is scaled correctly. Most “position is offset” issues are in fact rendering issues.

You can try to display a shape at the mouse position first e.g. GetOverlayDrawList()->AddCircleFilled(io.MousePos, ...) to visualize the difference.

but I have checked and verified that the size of both coordinates is correct.

Here’s one issue… GetWindowRect is the wrong function to use it includes the size of window decoration (e.g. title screen). You want to use GetClientRect.

So your render function is likely project WindowRect-worth-of-space into a ClientRect-sized framebuffer, and your mouse positions are likely passed in client space and everything is off.

You could have other issues lying around. Again hard to guess.

1 Like

That’s coincidentally exactly what was happening to me in my post as well. You’re passing Window size to a viewport or buffer somewhere instead of client rect size. Note that doing this to just about any buffer/vp is going to cause the issue. Mine was a depth buffer.

You need to be passing in client rect size vs window size to everything except actual window creation. Go see the example I just posted of my solution in my topic and it’ll show you exactly what to be on the lookout for.

1 Like

Thank you for the quick response and sorry if I am wasting your time on something super trivial.

Not sure if this is an issue for me because I don’t actually have any window decoration so GetClientRect and GetWindowRect return the same values.

I have temporarily removed the custom font and updated the code to render a circle on the mouse position as you suggested.

Like I said earlier I am basically using the code from the Win32 DX11 example in the repo, so unless there is an issue with that code I don’t really see where I could have messed up.

I have included a gif showing what I mean about the mouse position offset increasing as I move away from the origin.

If you compile the dx11+win32 examples from the repo without any modification do you have the same problem?

1 Like

Looks like I found the issue but I am still working on finding the solution.

Seems that using SetWindowLong to remove the window decoration is whats causing the issue.
If I keep the window decoration the mouse seems to line up perfectly although unfortunately if I want to complete this application I need to remove the title.

In both versions of the project I am creating a 800 x 400 window but when I remove the window decoration GetClientRect returns 800 x 400 and when I keep the window decoration GetClientRect returns 784 x 361

I tried manually setting io.DisplaySize to 784 x 361 when I remove the window decoration but it seems to have no affect on the mouse offset.