Screen Saver

/********************************************************
* User screen saver developer can also
* use animate effects for this screen saver
*********************************************************/

using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Mesoft.Control
{
    public partial class MeScreenSaver : Form
    {
        #region Win32 API functions
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
        #endregion
        private Point mouseLocation;
        private bool previewMode = false;
        private Random rand = new Random();
        public MeScreenSaver()
        {
            InitializeComponent();
        }
        public static void ShowScreenSaver()
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                MeScreenSaver screensaver = new MeScreenSaver(screen.Bounds);
                screensaver.Show();
            }
        }
        public MeScreenSaver(Rectangle Bounds)
        {
            InitializeComponent();
            this.Bounds = Bounds;
        }
        public MeScreenSaver(IntPtr PreviewWndHandle)
        {
            InitializeComponent();
            // Set the preview window as the parent of this window
            SetParent(this.Handle, PreviewWndHandle);
            // Make this a child window so it will close when the parent dialog closes
            SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));
            // Place our window inside the parent
            Rectangle ParentRect;
            GetClientRect(PreviewWndHandle, out ParentRect);
            Size = ParentRect.Size;
            Location = new Point(0, 0);
            // Make text smaller
            txtLabel.Font = new System.Drawing.Font("Arial", 6);
            previewMode = true;
        }
        private void ScreenSaverForm_Load(object sender, EventArgs e)
        {            
            LoadSettings();
            Cursor.Hide();            
            TopMost = true;
            PicBox.Image = Mesoft.Drawing.MeImage.DrawReflection(PicBox.Image, Color.Black);
            moveTimer.Interval = 1000;
            moveTimer.Tick += new EventHandler(moveTimer_Tick);
            moveTimer.Start();
        }
        private void moveTimer_Tick(object sender, System.EventArgs e)
        {
            // Move text to new location
            PicBox.Left = rand.Next(Math.Max(1, Bounds.Width - PicBox.Width));
            PicBox.Top = rand.Next(Math.Max(1, Bounds.Height - PicBox.Height));            
        }
        private void LoadSettings()
        {
            // Use the string from the Registry if it exists
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Demo_ScreenSaver");
            if (key == null)
                txtLabel.Text = "Mesoft Technologies";
            else
                txtLabel.Text = (string)key.GetValue("text");
        }
        private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (!previewMode)
            {
                if (!mouseLocation.IsEmpty)
                {
                    // Terminate if mouse is moved a significant distance
                    if (Math.Abs(mouseLocation.X - e.X) > 5 ||
                        Math.Abs(mouseLocation.Y - e.Y) > 5)
                        Application.Exit();
                }
                mouseLocation = e.Location;
            }
        }
        private void ScreenSaverForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!previewMode)
                Application.Exit();
        }
        private void ScreenSaverForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (!previewMode)
                Application.Exit();
        }
    }
}

No comments: