using System;
using System.Drawing;
using System.Windows.Forms;
// 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
{
//Replace this variable with the name of the desktop icon profile that you would like to save and load
private const string DesktopIconProfileName = "Default";
public static void Run(IntPtr windowHandle)
{
//check to see if we are already running
foreach(IntPtr window in BFS.Window.GetAllWindowHandles())
{
if(BFS.Window.GetText(window).Equals("PowerBroadcastForm", StringComparison.Ordinal))
return;
}
//start up our form
using(PowerBroadcastForm form = new PowerBroadcastForm())
form.ShowDialog();
}
//this starts an invisible form that listens for power state changes
private class PowerBroadcastForm : Form
{
public PowerBroadcastForm()
{
this.SuspendLayout();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Text = "PowerBroadcastForm";
this.ResumeLayout(false);
}
private const long WM_POWERBROADCAST = 0x218;
private const long PBT_APMSUSPEND = 0x0004; //suspending, sleeping
private const long PBT_APMRESUMEAUTOMATIC = 0x0012; //resuming
private const long PBT_APMRESUMESUSPEND = 0x0007; //sent after resume if user woke it up
//override the form function that listens to messages from windows
protected override void WndProc(ref Message m)
{
try
{
//if windows isn't telling us about a power change, ignore it
if (m.Msg != WM_POWERBROADCAST)
return;
//it's a power change! find out what it is and do something accordingly
switch (m.WParam.ToInt64())
{
case PBT_APMSUSPEND: //sleep
BFS.DisplayFusion.SaveDesktopIconsProfile(DesktopIconProfileName);
break;
case PBT_APMRESUMEAUTOMATIC: //resume
BFS.DisplayFusion.LoadDesktopIconsProfile(DesktopIconProfileName);
break;
}
}
finally
{
//do default stuff
base.WndProc(ref m);
}
}
}
}