You can use a WH_CBT hook to detect the window or FindWindow, then check the control
For example with FindWindow, test on a button click (change the location of python-3.11.0-amd64.exe) :
(remove space at S leep (editor bug))
public partial class Form1 : Form
{
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
public const int BM_GETCHECK = 0x00F0;
public const int BM_SETCHECK = 0x00F1;
public const int BST_UNCHECKED = 0x0000;
public const int BST_CHECKED = 0x0001;
public Form1()
{
InitializeComponent();
}
private Thread workerThread = null;
private readonly AutoResetEvent isCanceled = new AutoResetEvent(false);
private void WorkerThread()
{
for (; !isCanceled.WaitOne(0);)
{
IntPtr hWnd = FindWindow("PythonBA", null);
if (hWnd != IntPtr.Zero)
{
IntPtr hWndCheckBox = FindWindowEx(hWnd, IntPtr.Zero, "Button", "Add &python.exe to PATH");
if (hWndCheckBox != IntPtr.Zero)
{
// To not send the message too early...
Thread.S leep(200);
SendMessage(hWndCheckBox, BM_SETCHECK, BST_CHECKED, IntPtr.Zero);
isCanceled.Set();
}
}
Thread.S leep(10);
}
}
private void button1_Click(object sender, EventArgs e)
{
workerThread = new Thread(new ThreadStart(this.WorkerThread));
workerThread.Start();
using (Process p = new Process())
{
p.StartInfo.FileName = "E:\\temp\\python-3.11.0-amd64.exe";
p.Start();
}
}
}