1

How would I make my program check if a certain application like "notepad" has focus and ultimately open my second form when it does have focus, closing that second form when it loses focus.

(I would Also like to include an updater so that if the checkbox is checked while "notepad" is closed keep the checkbox checked, but do not open my second form until "notepad" has been opened) I know this is a very specific question and hence why I couldn't find anything relative to this.

Here is a mockup of what I believe it would look like:

DLL import
getforeground window
Process g = "notepad" 

if (g is Foreground window in front && checkbox.checked) // my checkbox i use to enable the program
{
show form two
}
else
{
hide form two
}
James
  • 27
  • 4
  • 2
    Does this answer your question? [C#: Detecting which application has focus](https://stackoverflow.com/questions/2183541/c-detecting-which-application-has-focus) – Mikael Apr 16 '22 at 20:00
  • 1
    Right direction but no, I did look into this beforehand and "GetForgegroundWindow()" looks like the answer. The page you had directed to me was more about checking what program was running and doing certain events when a certain program is focused where as i am trying to find a way so that when a certain program lets say "notepad" is focused then allow my form2 to open and when notepad is closed close my form2 with it – James Apr 16 '22 at 20:08
  • 1
    [SetWinEventHook()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook) can setup a delegate to notify when an application comes to the foreground (`EVENT_SYSTEM_FOREGROUND`). So does UI Automation, handling standard events in this case. -- You can use, e.g., [Win32_ProcessTrace](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process) to be notified when a Process starts or closes, in case it's useful. But also UI Automation, using `WindowPatter.WindowOpenedEvent`, in case you only care about windowed processes. – Jimi Apr 16 '22 at 20:17
  • [Detect when a specific window in another process opens or closes](https://stackoverflow.com/a/40285043/3110834) – Reza Aghaei Apr 17 '22 at 04:38
  • That looks correct but how would i now include it opening and closing my second form? – James Apr 17 '22 at 21:11

1 Answers1

0

My solution for anyone who wants to only make their code run when a specific program is open. This is great for Mouse events + sending inputs when a specific program is opened. (Removes that craziness from happening when outside of that program)

FirstlyImport DLLs:

  [DllImport("user32.dll")]
  static extern IntPtr GetForegroundWindow();
  [DllImport("user32.dll")]
  static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

Second create this string to capture what window is active:

public string GetActiveWindowTitle()
        {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);
            IntPtr handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }
            return null;
        }

Reminder if you wish to test which application you have open just maker a timer on 1 second repeat and type this inside the timer:

Console.Writeline(GetForegroundWindow());

Once you know your applications name (Also shows the name on taskbar - not task manager) You will want to type:

if (GetActiveWindowTitle() == "Notepad") // change notepad to your program
{
 // do what you want to do
}

Hopefully this helps someone like it helped me :)

James
  • 27
  • 4