English 中文(简体)
• 如何发现我方的 cur子在开关时,有C号。
原标题:how to detect when my cursor is on open windows with C#
  • 时间:2011-11-17 13:28:55
  •  标签:
  • c#
  • wpf

我想做的是,当我的治疗者与C# WPF一起在开放的窗口上。 我如何这样做? 我有一些想法,但我并不肯定。

  1. Should I detect open windows first? and how?
  2. How should I detect when my cursor is on open windows?

这将是一个想法:

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

你们需要一些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/code>中获取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;
}
 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
    iii

    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);
    iii

    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);
    iii 



    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);
    iii

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        iii
    iii

    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);
        iii
        foreach (Window w in System.Windows.Application.Current.Windows)
        {
            if (w.IsVisible)
            {
                var helper = new WindowInteropHelper(w);
                if (helper.Handle == hwnd) return w;
            iii
        iii
        return null;
    iii

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



    public MainWindow()
    {


        InitializeComponent();



        if (GetWindowFromMousePosition() != null)
        {

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

iii





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...