using System; using System.Drawing; using System.Windows.Forms; using System.Threading; using System.Collections.Generic; using System.Runtime.InteropServices; // The 'windowHandle' parameter will contain the window handle for the: // - Active window when run by hotkey // - Window Location target when run by a Window Location rule // - TitleBar Button owner when run by a TitleBar Button // - Jump List owner when run from a Taskbar Jump List // - Currently focused window if none of these match public static class DisplayFusionFunction { private static readonly string SettingName = "ActivlyDimUnfocusedMonitorsWhileWindowIsRunning_TransparentForm_Running"; public static void Run(IntPtr windowHandle) { //toggle the setting from running to not running, and vice versa ToggleSetting(); //check to see if we should exit bool isExiting = !IsTransparentWindowRunning(); //if we are exiting, exit if(isExiting) return; //add forms for each monitor except the used one List forms = new List(); foreach(uint id in BFS.Monitor.GetMonitorIDs()) { if(BFS.Monitor.GetMonitorIDByWindow(windowHandle) == id) continue; forms.Add(new TransparentForm(0, id)); } try { //this will open the forms we added to the list by using our custom application context Application.Run(new MultipleFormApplicationContext(windowHandle, forms)); } catch (Exception ex) { BFS.Dialog.ShowMessageError(ex.Message); } } //this function toggles the script settings from running to not running private static void ToggleSetting() { string status = BFS.ScriptSettings.ReadValue(SettingName); if(status.Equals("running", StringComparison.Ordinal)) BFS.ScriptSettings.WriteValue(SettingName, "not"); else BFS.ScriptSettings.WriteValue(SettingName, "running"); } //this function allows us to see the currect state of the script private static bool IsTransparentWindowRunning() { string status = BFS.ScriptSettings.ReadValue(SettingName); return status.Equals("running", StringComparison.Ordinal); } //extend the ApplicationContext class to support opening multiple forms //this class will also listen for active window changes, and play with the form transparency private class MultipleFormApplicationContext : ApplicationContext { //this is the function signature for the hook callback private delegate void WinEventProcDelegate(IntPtr hWinEventHook, uint e, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); //this function will let us listen to windows events [DllImport("User32.dll")] private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProcDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwflags); //this function will let us stop listening to windows events [DllImport("User32.dll")] private static extern bool UnhookWinEvent(IntPtr hWinEventHook); private IntPtr WindowChangedHookHandle; private WinEventProcDelegate HookDelegateReference; private List Forms; private readonly IntPtr SelectedWindow; private const uint EVENT_SYSTEM_FOREGROUND = 0x0003; internal MultipleFormApplicationContext(IntPtr selectedWindow, List forms) { this.Forms = forms; this.SelectedWindow = selectedWindow; //store the reference to the function so .NET doesn't feel like disposing it this.HookDelegateReference = this.WindowChanged; //start the window hook this.WindowChangedHookHandle = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, this.HookDelegateReference, 0, 0, 0); //open each of the forms, and add our closing event to them foreach(Form form in forms) { form.FormClosed += this.OnFormClosed; form.Show(); } this.SetFormsTransparency(this.SelectedWindow); } //when all the forms close, make sure to exit the application private void OnFormClosed(object sender, EventArgs e) { try { if(Application.OpenForms.Count != 0) return; //release the window hooks and exit the application if(this.WindowChangedHookHandle != IntPtr.Zero) UnhookWinEvent(this.WindowChangedHookHandle); this.HookDelegateReference = null; this.ExitThread(); } catch //ignore any errors { } } private void SetFormsTransparency(IntPtr focusedWindow) { try { //loop through each form and toggle the visibility foreach(TransparentForm form in this.Forms) BFS.Window.SetTransparency(form.Handle, (focusedWindow == this.SelectedWindow) ? 40m : 0); } catch //ignore any errors { } } private void WindowChanged(IntPtr hWinEventHook, uint e, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { try { this.SetFormsTransparency(hwnd); } catch //ignore any errors { } } } //extend the Form class to get the behavior we want private class TransparentForm : Form { //this will tell us what transparency to use private decimal Transparency; //a variable to store the monitor id this form is on public uint MonitorId {get; private set;} //the contructor for our class internal TransparentForm(decimal transparency, uint id) { this.Transparency = transparency; this.MonitorId = id; this.SuspendLayout(); //setup the layout of this form this.BackColor = Color.Black; this.FormBorderStyle = FormBorderStyle.None; this.ShowInTaskbar = false; //setup the form load event this.Load += Form_Load; this.ResumeLayout(false); } private void Form_Load(object sender, EventArgs e) { //make the window transparent BFS.Window.SetTransparency(this.Handle, this.Transparency); //add a windows style to the current style that will //tell this window to ignore user input uint style = (uint)BFS.Window.GetWindowStyleEx(this.Handle); BFS.Window.SetWindowStyleEx((BFS.WindowEnum.WindowStyleEx)(style | (uint)BFS.WindowEnum.WindowStyleEx.WS_EX_TRANSPARENT | (uint)BFS.WindowEnum.WindowStyleEx.WS_EX_LAYERED), this.Handle); //set the form's bounds to the monitor this.Bounds = BFS.Monitor.GetMonitorBoundsByID(this.MonitorId); //set topmost BFS.Window.SetAlwaysOnTop(this.Handle, true); //start up a thread to listen for an exit event new Thread(new ThreadStart(ExitListener)).Start(); } private void ExitListener() { try { while(true) { //if we should close, tell the main thread to close the form if(!IsTransparentWindowRunning()) { try { this.Invoke((MethodInvoker) delegate { this.Close(); }); } catch //something went wrong, ignore { } break; } //sleep for a quarter of a second BFS.General.ThreadWait(500); } } catch //ignore errors { } } } }