I use an inline function for this, though I haven't spent much time refining it:
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class VAInline
{
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_TOPMOST = 0x8;
public void main()
{
int currentStyle = GetWindowLong(VA.MainWindowHandle, GWL_EXSTYLE);
bool alwaysOnTop = (currentStyle & WS_EX_TOPMOST) != 0;
if (VA.Command.Name().EndsWith("on"))
{
VA.SetOpacity(30);//Changing opacity first works
if (!alwaysOnTop)
{
enableAlwaysOnTop();
}
enableClickThrough(currentStyle);
}
else if (VA.Command.Name().EndsWith("off"))
{
if (alwaysOnTop)
{
//disableAlwaysOnTop();
VA.WriteToLog("Keeping Always On Top enabled", "pink");
}
disableClickThrough(currentStyle);
VA.SetOpacity(100);
}
else
{
if (Application.OpenForms["frmMain"].Opacity < 1.0)//Assume the overlay is off if the window is not transparent
{
if (alwaysOnTop)
{
//disableAlwaysOnTop();
VA.WriteToLog("Keeping Always On Top enabled", "pink");
}
disableClickThrough(currentStyle);
VA.SetOpacity(100);
}
else
{
VA.SetOpacity(30);//Always change opacity first
if (!alwaysOnTop)
{
enableAlwaysOnTop();
}
enableClickThrough(currentStyle);
}
}
}
void enableAlwaysOnTop()
{
VA.Command.Execute("voiceattack on top", true, true);
System.Threading.Thread.Sleep(1000);
}
void disableAlwaysOnTop()
{
VA.Command.Execute("voiceattack not on top", true, true);
System.Threading.Thread.Sleep(1000);
}
void enableClickThrough(int currentStyle)
{
SetWindowLong(VA.MainWindowHandle,GWL_EXSTYLE , currentStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
void disableClickThrough(int currentStyle)
{
SetWindowLong(VA.MainWindowHandle,GWL_EXSTYLE , currentStyle & WS_EX_LAYERED & WS_EX_TRANSPARENT);
}
}
The command phrase, if using this example, would be something like "overlay mode [;on;off]" (the semicolon indicating an optional section must be placed before the actual phrases/words in the section, because the current version of VoiceAttack would pass the phrase "overlay mode on" if the command phrase were "overlay mode [on;off;]" when triggering the command using most non-spoken means, thus preventing the command from toggling correctly)
You'll note the inline function attempts to call a command to switch the "Always on Top" option; that command merely runs another instance of VoiceAttack with the relevant command line parameters to toggle that setting.