Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Save/Restore Window Positions on Sleep/Resume

Description
This function will save the open window positions when the computer enters sleep mode, and will restore them when it resumes from sleep.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Apr 7, 2015
Date Last Modified
Apr 7, 2015

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;

public static class DisplayFusionFunction
{
	public static void Run()
	{
		//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

		protected override void WndProc(ref Message m)
		{
			try
			{
				if (m.Msg != WM_POWERBROADCAST)
					return;

				switch (m.WParam.ToInt64())
				{
					case PBT_APMSUSPEND: //sleep
						BFS.DisplayFusion.RunFunction("Save Window Positions");
						break;
					case PBT_APMRESUMEAUTOMATIC: //resume
						BFS.DisplayFusion.RunFunction("Restore Window Positions From Last Save");
						break;				
				}
			}
			finally
			{
				base.WndProc(ref m);
			}
		}
	}
}