如何使用C#检测我的光标在打开的窗口上的时间

时间:2011-11-17 13:28:55

标签: c# wpf

当我的光标位于带有C#WPF的打开窗口时,我想做点什么。我该怎么做?我有一些想法,但我不确定。

  1. 我应该先检测打开的窗户吗?怎么样?
  2. 我应该如何检测光标在打开的窗口上的时间?
  3. 这将是一个想法:

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

2 个答案:

答案 0 :(得分:1)

你需要一些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;
    }
}

您可以从Application.Current.Windows属性获取WPF应用程序窗口列表,并使用WindowInteropHelper类获取其句柄:

    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;
}

答案 1 :(得分:0)

 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
        }
    }
}

}