仅在可以找到 window 句柄时引发事件

Only raise event if window handle can be found

我无法找到引发事件的正确方法 只有 如果可以找到所需的 window 句柄(即 "DesiredWindow"目前在机器上 运行。

目前,我的应用程序将 SetForegroundWindow 并向正确的 window 发送按键,只要该应用程序是 运行;我的问题是:如果所需的 window 不可用(即目标应用程序不是 运行),它仍会在引发事件时将按键发送到任何活动的 window ,即使我已经指定了要将其发送到的 window 句柄,但系统上不存在该 window 句柄。

我想知道的是:是否可以告诉我的应用程序仅在特定 lpWindowName 存在时才发送按键,而在找不到指定的 window 名称时不执行任何操作?

伪代码:

public partial class form1: Form
{
    [DllImport("User32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("User32.dll")]
    static extern int SetForegroundWindow(IntPtr hWnd);
    static IntPtr DesiredWindow = FindWindow(null, "(Desired Window Name)");

    public form1()
    {
        InitializeComponent();
    }

    //...

                private void MyEvent()
            {   
                if (DesiredWindow cannot be found)
                {
                 return; //Do not send KeyPress
                }

                SetForegroundWindow(DesiredWindow); 
                Thread.Sleep(50);
                Keyboard.KeyPress(Keys.(WhateverKey));
            }
}

我试过:

            private void MyEvent()
            {                       
                if (!DesiredWindow)
                {
                    MessageBox.Show("DesiredWindow not found");
                    return;
                }                  
                SetForegroundWindow(DesiredWindow);
                Thread.Sleep(50);
                Keyboard.KeyPress(Keys.WhateverKey);
            }

但是我收到错误 Operator '!' cannot be applied to operand of type 'IntPtr'

我也试过:

            private void MyEvent()
            {                       
                if (DesiredWindow == IntPtr.Zero)
                {
                    MessageBox.Show("DesiredWindow not found");
                    return;
                }                  
                SetForegroundWindow(DesiredWindow);
                Thread.Sleep(50);
                Keyboard.KeyPress(Keys.WhateverKey);
            }

但是当我使用这个方法时似乎没有任何反应。

我已将 MessageBox.Show("DesiredWindow not found"); 添加到 if 语句中,让我知道它是否有效,但即使所需的 window 可用,消息框也会弹出。

我试过的另一种方法是:

            private void MyEvent()
            {                       
                if (DesiredWindow > 0)
                {                  
                SetForegroundWindow(DesiredWindow);
                Thread.Sleep(50);
                Keyboard.KeyPress(Keys.WhateverKey);
                }
            }

但是我收到错误 Operator '>' cannot be applied to operand of type 'IntPtr' or 'Int'

我不确定检查 DesiredWindow 是否存在的正确方法。

不确定您是否引用了正确的库,我已经尝试了您的代码并且它们没有任何问题。请看下面的工作代码:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("User32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);

        private const string DesiredWindowTitle = "(Desired Window Name)";

        // do not invoke the method to find the window when the form was constructing
        // private static IntPtr DesiredWindow = FindWindow(null, DesiredWindowTitle);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // find the window at run time
            IntPtr DesiredWindow = FindWindow(null, DesiredWindowTitle);

            if (DesiredWindow == IntPtr.Zero)
            {
                return; //Do not send KeyPress
            }

            SetForegroundWindow(DesiredWindow);
            Thread.Sleep(50);
            Keyboard.KeyPress(Keys.(WhateverKey));
        }
    }
}