Skip to content

Commit

Permalink
All nag message boxes should be (hopefully) automatically closed now
Browse files Browse the repository at this point in the history
  • Loading branch information
integralfx committed Jul 27, 2019
1 parent 1512e39 commit 1e3a176
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
3 changes: 3 additions & 0 deletions MemTestHelper2/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 13 additions & 5 deletions MemTestHelper2/MemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand Down
30 changes: 27 additions & 3 deletions MemTestHelper2/WinAPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -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)
Expand Down Expand Up @@ -76,6 +76,30 @@ public static IntPtr GetHWNDFromPID(int pid, String windowTitle = "")
return hwnd;
}

public static List<IntPtr> FindAllWindows(string windowTitle)
{
var windows = new List<IntPtr>();

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)]
Expand All @@ -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);
Expand Down

0 comments on commit 1e3a176

Please sign in to comment.