如何获得在WPF与台式机鼠标的位置? [英] How to get mouse position related to desktop in WPF?
问题描述
当您搜索使用这样的问题,谷歌,你得到了很多次,但所有的解决方案假定您有至少一个窗口。
When you search for such question using google you get a lot of hits but all solutions assume you have at least one window.
但我的问题是,就像我的话来说 - 没有假设的。我可以有一个窗口,但我可以有零窗口(因为我没有甚至显示一个或我刚刚关闭了最后一个)。因此,在短期的解决方案,可以不依赖任何部件或窗口 - 唯一的事情是已知的,是有一个桌面(和应用程序运行,但它不具有任何窗口)
But my question is just like I phrased it -- not assumptions at all. I can have a window, but I could have zero windows (because I didn't even show one or I just closed the last one). So in short the solution cannot rely on any widget or window -- the only thing is known, is there is a desktop (and app running, but it does not have any windows).
所以,问题是 - ?如何让鼠标位置
So the question is -- how to get the mouse position?
我想展示中心,以鼠标位置的窗口。有一个在WPF没有这样的模式(只有中心,为业主,或中心屏幕),所以我不得不做手工。缺少的部分是鼠标的位置。
I would like to show windows centered to mouse position. There is no such mode in WPF (there are only center to owner, or center to screen) so I have to do it manually. The missing piece is mouse position.
感谢大家,所以我现在的第一部分该解决方案 - 原始位置。现在有一个问题,如何将数据转换为WPF。我发现了这样的话题:
http://stackoverflow.com/questions/1189384/wpf -pixels到桌面像素
但同样,它假定有一些窗口。
Thank you all, so now I have the first part of the solution -- raw position. Now there is a problem how to convert the data for WPF. I found such topic: http://stackoverflow.com/questions/1189384/wpf-pixels-to-desktop-pixels but again, it assumes having some window.
然后我GOOGLE了更多,我找到解决办法:
的 http://jerryclin.wordpress.com/2007/11/13/creating-non-rectangular-windows-with-interop/
Then I googled more and I found solution: http://jerryclin.wordpress.com/2007/11/13/creating-non-rectangular-windows-with-interop/
中的代码包括类放大/缩小坐标只依靠有关桌面信息。因此,这两条的作品,我终于得到解决:-)。再次感谢
the code includes class for scaling up/down coordinates relying only on info about desktop. So joining those two pieces, I finally get the solution :-). Thanks again.
推荐答案
获取屏幕坐标:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
private void WritePoint(object sender, RoutedEventArgs e)
{
POINT p;
if (GetCursorPos(out p))
{
System.Console.WriteLine(Convert.ToString(p.X) + ";" + Convert.ToString(p.Y));
}
}
转换像素WPF单位:
Converting Pixels to WPF Units:
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
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;
}
把两者一起:
Putting both together:
private void WriteMouseCoordinatesInWPFUnits()
{
POINT p;
if (GetCursorPos(out p))
{
Point wpfPoint = ConvertPixelsToUnits(p.X, p.Y);
System.Console.WriteLine(Convert.ToString(wpfPoint.X) + ";" + Convert.ToString(wpfPoint.Y));
}
}
这篇关于如何获得在WPF与台式机鼠标的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!