diff --git a/MemTestHelper2/MainWindow.xaml.cs b/MemTestHelper2/MainWindow.xaml.cs index 2576c64..31d816b 100644 --- a/MemTestHelper2/MainWindow.xaml.cs +++ b/MemTestHelper2/MainWindow.xaml.cs @@ -653,6 +653,9 @@ private void StartMemTests() memtests[i].Start(ram, startMin); }); + foreach (var hwnd in WinAPI.FindAllWindows(MemTest.MSG2)) + WinAPI.ControlClick(hwnd, MemTest.MSGBOX_OK); + if (!chkStartMin.IsChecked.Value) LayOutMemTests(); } diff --git a/MemTestHelper2/MemTest.cs b/MemTestHelper2/MemTest.cs index ce0b5b4..2a5a848 100644 --- a/MemTestHelper2/MemTest.cs +++ b/MemTestHelper2/MemTest.cs @@ -12,7 +12,7 @@ class MemTest public static readonly int WIDTH = 217, HEIGHT = 247, MAX_RAM = 2048; - private const string CLASSNAME = "#32770", + public const string CLASSNAME = "#32770", BTN_START = "Button1", BTN_STOP = "Button2", EDT_RAM = "Edit1", @@ -21,7 +21,9 @@ class MemTest STATIC_FREE_VER = "Static2", MSGBOX_OK = "Button1", MSGBOX_YES = "Button1", - MSGBOX_NO = "Button2"; + MSGBOX_NO = "Button2", + MSG1 = "Welcome, New MemTest User", + MSG2 = "Message for first-time users"; private Process process = null; private bool hasStarted = false, isFinished = false; @@ -92,7 +94,7 @@ public void Start(double ram, bool startMinimised) // Wait for process to start. while (string.IsNullOrEmpty(process.MainWindowTitle)) { - ClickNagMessageBox("Welcome, New MemTest User"); + ClickNagMessageBox(MSG1); Thread.Sleep(100); process.Refresh(); } @@ -102,7 +104,7 @@ public void Start(double ram, bool startMinimised) WinAPI.ControlSetText(hwnd, STATIC_FREE_VER, "MemTestHelper by ∫ntegral#7834"); WinAPI.ControlClick(hwnd, BTN_START); - while (!ClickNagMessageBox("Message for first-time users")) + while (!ClickNagMessageBox(MSG2)) Thread.Sleep(100); if (startMinimised) @@ -200,7 +202,13 @@ public bool ClickNagMessageBox(string messageBoxCaption, MsgBoxButton button = M break; } - WinAPI.ControlClick(hwnd, strBtn); + attempts = 0; + while (!WinAPI.ControlClick(hwnd, strBtn) && attempts < maxAttempts) + { + Thread.Sleep(100); + attempts++; + } + return true; } } diff --git a/MemTestHelper2/WinAPI.cs b/MemTestHelper2/WinAPI.cs index 92eed04..5bce05b 100644 --- a/MemTestHelper2/WinAPI.cs +++ b/MemTestHelper2/WinAPI.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; @@ -17,8 +18,7 @@ public static bool ControlClick(IntPtr hwndParent, string className) { IntPtr hwnd = FindWindow(hwndParent, className); if (hwnd == IntPtr.Zero) return false; - SendNotifyMessage(hwnd, BM_CLICK, IntPtr.Zero, null); - return true; + return SendNotifyMessage(hwnd, BM_CLICK, IntPtr.Zero, null) != 0; } public static bool ControlSetText(IntPtr hwndParent, string className, string text) @@ -76,6 +76,30 @@ public static IntPtr GetHWNDFromPID(int pid, String windowTitle = "") return hwnd; } + public static List FindAllWindows(string windowTitle) + { + var windows = new List(); + + EnumWindows( + delegate (IntPtr hwnd, IntPtr lParam) + { + int len = GetWindowTextLength(hwnd); + if (windowTitle.Length > 0 && len != windowTitle.Length) + return true; + + StringBuilder sb = new StringBuilder(len + 1); + GetWindowText(hwnd, sb, sb.Capacity); + + if (sb.ToString() == windowTitle) + windows.Add(hwnd); + + return true; + }, + IntPtr.Zero); + + return windows; + } + // Imports // [DllImport("user32.dll", SetLastError = true)] @@ -90,7 +114,7 @@ public static IntPtr GetHWNDFromPID(int pid, String windowTitle = "") // doesn't block [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] - public static extern bool SendNotifyMessage(IntPtr hWnd, int Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); + public static extern int SendNotifyMessage(IntPtr hWnd, int Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);