How to get mouse position related to desktop in WPF?

后端 未结 3 1686
我在风中等你
我在风中等你 2020-12-06 12:03

Problem

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 l

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 12:47

    Two options:

    Use System.Windows.Forms.Control.MousePosition, or p/invoke

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern bool GetCursorPos([In, Out] NativeMethods.POINT pt);
    

    The first option already does the p/invoke for you. I'm not entirely sure it requires you have some UI splashed up, but I don't think so. Yes, its winforms and not wpf, but it really doesn't have anything to do with where its located at.

    If you want to skip any dependencies on system.windows.forms.dll then check out more information about the second on pinvoke.net.

提交回复
热议问题