I think you should use `ScreenToClient` here. You're using the window's top left corner as the origin, but it needs to be the client area's top left corner for the calculation below to be correct.