I'm attempting to perform a drag and drop operation in WPF. I distinctly remember the days when I did this sort of thing regularly.
I'm using a p/invoke of GetCursorPos() to return screen coordinates. This number is offset from the cursor by quite a bit.
I assumed the issue was scaling due to my laptop's high-DPI setting (120) so I used the following code (stolen from this answer):
private Point ConvertPixelsToUnits(int x, int y)
{
// get the system DPI
IntPtr dDC = GetDC(IntPtr.Zero); // Get desktop DC
int dpi = GetDeviceCaps(dDC, 88);
bool rv = ReleaseDC(IntPtr.Zero, dDC);
// WPF's physical unit size is calculated by taking the
// "Device-Independant Unit Size" (always 1/96)
// and scaling it by the system DPI
double physicalUnitSize = (1d / 96d) * (double)dpi;
Point wpfUnits = new Point(physicalUnitSize * (double)x,
physicalUnitSize * (double)y);
return wpfUnits;
}
While the dpi value is correct from the p/invokes,, the result is way, way off (significantly worse than using the raw GetCursorPos() value).
So after lots of playing around with all the different options I can find, I am at a loss as to how to get the correct values on the Window.