如果可以找到窗口句柄,则仅引发事件

时间:2016-02-19 17:08:49

标签: c# winforms keypress findwindow

如果可以找到所需的窗口句柄(即。"DesiredWindow"当前正在计算机上运行),我无法找出正确的方法

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

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

伪码:

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语句中以告知我它是否正常工作,但即使所需窗口可用,也会弹出消息框。

我尝试过的另一种方法是:

            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是否存在的正确方法。

1 个答案:

答案 0 :(得分:1)

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

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