Implementing Screen Savers in .NET
by Richard Grimes

Listing One

Process[] processes = Process.GetProcesses();
// Get the name of the file of this process
string thisProcess = Process.GetCurrentProcess().MainModule.FileName;
foreach (Process process in processes)
{
   // Check the file name of the processes main module
   if (thisProcess.CompareTo(process.MainModule.FileName) != 0)
      continue;
   // Found an instance of the process
   if (Process.GetCurrentProcess().Id == process.Id)
   {
      // We don't want to commit suicide
      continue;
   }
   // Tell the other instance to die
   Win32.SendMessage(
      process.MainWindowHandle, Win32.WM_CLOSE, 0, 0);
}


Listing Two

Process[] processes = Process.GetProcesses();
string thisProcess = Process.GetCurrentProcess().MainModule.FileName;
string thisProcessName = Process.GetCurrentProcess().ProcessName;
foreach (Process process in processes)
{
   // Compare process name, this will weed out most processes
   if (thisProcessName.CompareTo(process.ProcessName) != 0) continue;
   // Check the file name of the processes main module
   if (thisProcess.CompareTo(process.MainModule.FileName) != 0) continue;
   if (Process.GetCurrentProcess().Id == process.Id)
   {
     // We don't want to commit suicide
     continue;
   }
   // Tell the other instance to die
   Win32.SendMessage(process.MainWindowHandle, Win32.WM_CLOSE, 0, 0);
}


Listing Three

[Serializable]
public abstract class ConfigurationBase
{
   public ConfigurationBase(string name);
   // Call this on the derived object to write the config data to disk
   public void Serialize();
   // Call this on the derived object to read the config data into the
   // derived object
   public void Deserialize();
}


Listing Four

[STAThread]
static void Main()
{
   Saver saver = new Saver();
   // Test to see if the screen saver is full screen, preview, or started for
   // the config or password dialogs
   bool animate = saver.InitScreenSaver();
   // Preview or full screen
   if (animate)
   {
      saver.Initialize();
      Application.Run(saver);
   }
}


Listing Five

public class Saver : ScreenSaver
{
   public Saver() : base();
   protected void Initialize();
   protected override void PaintHandler(Graphics g);
   protected override Form GetConfigForm();
}



