как определить, когда мой курсор находится на открытых окнах с C # - PullRequest
0 голосов
/ 17 ноября 2011

Я хочу что-то сделать, когда мой курсор находится в открытом окне с C # WPF. Как мне это сделать? У меня есть некоторые идеи, но я не уверен.

  1. Должен ли я сначала обнаружить открытые окна? и как?
  2. Как мне определить, когда мой курсор находится в открытых окнах?

Это будет идея:

if ( cursor is on any open window( How to do this? ) ) {
    I will do something here
}
else {
    I will do something here 
}

Ответы [ 2 ]

1 голос
/ 17 ноября 2011

Вам понадобится WinAPI:

static class NativeMethods
{
    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(POINT point);

    [DllImport("user32.dll")]
    public static extern IntPtr GetParent(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(ref 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;
    }
}

Вы можете получить список окон приложений WPF из свойства Application.Current.Windows и получить их дескрипторы, используя WindowInteropHelper class:

    public static Window GetWindowFromPoint(Point point)
    {
        var hwnd = NativeMethods.WindowFromPoint(new POINT((int)point.X, (int)point.Y));
        if(hwnd == IntPtr.Zero) return null;
        var p = NativeMethods.GetParent(hwnd);
        while(p != IntPtr.Zero)
        {
            hwnd = p;
            p = NativeMethods.GetParent(hwnd);
        }
        foreach(Window w in Application.Current.Windows)
        {
            if(w.IsVisible)
            {
                var helper = new WindowInteropHelper(w);
                if(helper.Handle == hwnd) return w;
            }
        }
        return null;
    }

    public static Window GetWindowFromMousePosition()
    {
        POINT p = new POINT();
        NativeMethods.GetCursorPos(ref p);
        return GetWindowFromPoint(new Point(p.x, p.y));
    }

Использование:

if(GetWindowFromMousePosition() != null)
{
    // mouse cursor is over window
}
else
{
    // mouse cursor is somewhere else
}

Обновление:

Поскольку вы хотите проверять окна за пределами своего приложения, это еще проще:

public static bool IsCursorOverWindow()
{
    POINT p = new POINT();
    NativeMethods.GetCursorPos(ref p);
    var hwnd = NativeMethods.WindowFromPoint(p);
    if(hwnd == IntPtr.Zero) return false;
}
0 голосов
/ 25 ноября 2011
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.Runtime.InteropServices;
 using System.Windows.Interop;
 using System.IO;
 using System.Windows.Forms;



 namespace WpfApplication6
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    [Flags]
    public enum MouseEventFlags
    {
        LEFTDOWN = 0x00000002,
        LEFTUP = 0x00000004,
        MIDDLEDOWN = 0x00000020,
        MIDDLEUP = 0x00000040,
        MOVE = 0x00000001,
        ABSOLUTE = 0x00008000,
        RIGHTDOWN = 0x00000008,
        RIGHTUP = 0x00000010
    }

    public static void LeftClick(int X, int Y)
    {
        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
        mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
    }

    public static void LeftClickRelease(int X, int Y)
    {
        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);

        mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
    } 



    static class NativeMethods
    {
        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(POINT point);

        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(ref 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;
        }
    }

    public static Window GetWindowFromPoint(Point point)
    {
        var hwnd = NativeMethods.WindowFromPoint(new POINT((int)point.X, (int)point.Y));
        if (hwnd == IntPtr.Zero) return null;
        var p = NativeMethods.GetParent(hwnd);
        while (p != IntPtr.Zero)
        {
            hwnd = p;
            p = NativeMethods.GetParent(hwnd);
        }
        foreach (Window w in System.Windows.Application.Current.Windows)
        {
            if (w.IsVisible)
            {
                var helper = new WindowInteropHelper(w);
                if (helper.Handle == hwnd) return w;
            }
        }
        return null;
    }

    public static Window GetWindowFromMousePosition()
    {
        POINT p = new POINT();
        NativeMethods.GetCursorPos(ref p);
        return GetWindowFromPoint(new Point(p.x, p.y));
    }



    public MainWindow()
    {


        InitializeComponent();



        if (GetWindowFromMousePosition() != null)
        {

            LeftClick(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
            // mouse cursor is over window
        }
        else
        {
            LeftClickRelease(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
                // mouse cursor is somewhere else
        }
    }
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...