Only raise event if window handle can be found

964 Views Asked by At

I'm having trouble figuring out the correct way to raise an event only if the desired window handle can be found (ie. "DesiredWindow" is currently running on the machine).

Currently, my application will SetForegroundWindow and send a keypress to the proper window as long as that application is running; My issue is: If the desired window isn't available (ie. the target application is not running), it will still send the keypress to any active window when the event is raised, even though I've specified the window handle that I want to send it to, and that window handle does not exist on the system.

What I'm wondering is: Is it possible to tell my application to only send the keypress if the particular lpWindowName exists, and do nothing if the specified window name cannot be found?

Pseudo code:

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

I've tried:

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

But I get the error Operator '!' cannot be applied to operand of type 'IntPtr'

I've also tried:

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

But nothing seems to happen when I use this method.

I've added MessageBox.Show("DesiredWindow not found"); into the if statement to let me know if it's working, but the messagebox pops up even when the desired window is available.

Another method I've tried is:

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

But I get the error Operator '>' cannot be applied to operand of type 'IntPtr' or 'Int'

I'm not sure the correct way of checking to see if DesiredWindow exists.

1

There are 1 best solutions below

5
On BEST ANSWER

Not sure if you had referenced the right libraries, I have tried your codes and they worked without any problem. Please see the working codes as below:

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