Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:41:39+00:00 2026-05-31T04:41:39+00:00

I have made a bouncing screensaver in C#. When there is only a single

  • 0

I have made a bouncing screensaver in C#. When there is only a single monitor to display it works perfectly. However, when I have a dual monitor display the bounds do not work properly. The position of secondary monitor seems to have an effect and there does not seem to be a right bound (secondary screen is on the right side).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace LIUScreensaver
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length > 0)
            {
                string firstArg = args[0].ToLower().Trim();
                string secondArg = null;

                //handle cases where arguments are separated by colon
                if (firstArg.Length > 2)
                {
                    secondArg = firstArg.Substring(3).Trim();
                    firstArg = firstArg.Substring(0, 2);
                }
                else if (args.Length > 1)
                {
                    secondArg = args[1];
                }

                if (firstArg == "/c")
                {
                    MessageBox.Show("This screensaver does not allow configuration changes");
                }
                else if (firstArg == "/p")
                {
                    if (secondArg == null)
                     {
                        MessageBox.Show("Sorry, but there was an error.", "Screensaver", MessageBoxButtons.OK, 
                            MessageBoxIcon.Error);
                        return;
                    }
                    IntPtr preview = new IntPtr(long.Parse(secondArg));
                    Application.Run(new ScreensaverForm(preview));
                }
                else if (firstArg == "/s")
                {
                    ShowScreenSaver();
                    Application.Run();
                }
                else
                {
                    MessageBox.Show("Invalid command line argument. \"" + firstArg + "\" is not a valid ", "Screensaver",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
            else
            {
                MessageBox.Show("There has been an error displaying the screensaver.  Please report this issue.");
            }
        }
        public static void ShowScreenSaver()
        {
            //creates form on each display
            int i = 0;
            Screen[] screen = Screen.AllScreens;
            for (i = 0; i < screen.Length; i++)
            {
                ScreensaverForm screensaver = new ScreensaverForm(screen[i].Bounds);
                screensaver.Show();
            }
        }
    }    
}

//Screensaver form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace LIUScreensaver
{
public partial class ScreensaverForm : Form
{

    [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);

    private Boolean previewMode = false;
    private int mouseX = 0, mouseY = 0;
    private int imgX = 0, imgY = 0;
    private Boolean bot = true, left = true, right = false, top = false;
    Point p;

    //constructor with no arguments
    public ScreensaverForm()
    {
        InitializeComponent();
    }

    //constructor with bound arguments
    public ScreensaverForm(Rectangle Bounds)
    {
        InitializeComponent();
        this.Bounds = Bounds;
    }

    //preview constructor
    public ScreensaverForm(IntPtr preview)
    {
        InitializeComponent();

        SetParent(this.Handle, preview);

        SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));

        Rectangle parentRec;
        GetClientRect(preview, out parentRec);
        Size = parentRec.Size;
        Location = new Point(0, 0);

        txtLabel.Font = new System.Drawing.Font("Arial", 6);

        previewMode = true;
    }

    //form load
    private void ScreensaverForm_Load(object sender, EventArgs e)
    {
        Cursor.Hide();
        TopMost = true;
        System.Drawing.Color background = System.Drawing.ColorTranslator.FromHtml("#002668");
        this.BackColor = background;

        moveTimer.Interval = 1;
        moveTimer.Tick += new EventHandler(moveTimer_Tick);
        moveTimer.Start();
    }

    //
    //-----------------------------------------------------------------------------------------------------
    //The following code ends the application and exits the screensaver
    private void ScreensaverForm_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!previewMode)
        {
            Application.Exit();
        }
    }

    private void ScreensaverForm_MouseMove(object sender, MouseEventArgs e)
    {
        if (!previewMode)
        {
            if (mouseX == 0)
            {
                mouseX = e.X;
                mouseY = e.Y;
            }

            if ((mouseX - e.X) > 5 || (mouseY - e.Y) > 5)
            {
                Application.Exit();
            }
        }
    }

    private void ScreensaverForm_MouseClick(object sender, MouseEventArgs e)
    {
        if (!previewMode)
        {
            Application.Exit();
        }
    }

    //
    //
    //timer to bounce the image across the screen
    private void moveTimer_Tick(object sender, System.EventArgs e)
    {
        // Move text to new location
        bounce();
    }

    //function that moves the image
    private void bounce()
    {

        //Checks boundaries
       if (txtLabel.Location.Y + txtLabel.Image.Height - this.Bounds.Bottom>= 0)
       {
           top = true;
           bot = false;
       }
       if (txtLabel.Location.X + txtLabel.Image.Width - this.Bounds.Right >= 0)
       {
           right = false;
           left = true;
       }
       if (txtLabel.Location.X <= this.Bounds.Left)
       {
           right = true;
           left = false;
       }

       if (txtLabel.Location.Y <= this.Bounds.Top)
       {
           top = false;
           bot = true;
       }

        //moves image
        if (bot == true)
        {
            if (right == true)
            {
                ++imgX;
                ++imgY;

                p.X = imgX;
                p.Y = imgY;
                txtLabel.Location = p;
            }
            else if (left == true)
            {
                --imgX;
                ++imgY;

                p.X = imgX;
                p.Y = imgY;
                txtLabel.Location = p;
            }

        }
        if (top == true)
        {
            if (right == true)
            {
                ++imgX;
                --imgY;

                p.X = imgX;
                p.Y = imgY;
                txtLabel.Location = p;
            }
            else if (left == true)
            {
                --imgX;
                --imgY;

                p.X = imgX;
                p.Y = imgY;
                txtLabel.Location = p;
            }
        }

        Invalidate();
    }

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:41:40+00:00Added an answer on May 31, 2026 at 4:41 am

    The problem is the label’s Location is its position relative to the monitor that it’s on while the bounds you are comparing it too are an absolute position across all monitors.

    So if the bounds of the primary monitor are Top: 0, Bottom: 900, Left: 0, Right: 1600
    the bounds of the secondary monitor might be something like Top: 0, Bottom: 900, Left: 1600, Right: 3200. While the Location of the label on the secondary monitor will returning its position relative to the secondary monitor so for example Location.X: 200, Location.Y: 300.

    You need to change the bounce Method to make the comparsion use either only absolute coordinates or only relative coordinates.

    Here is the comparison code modified to use relative position of the label on the monitor;

    if (txtLabel.Location.Y + txtLabel.Image.Height - (this.Bounds.Bottom - this.Bounds.Top) >= 0)
    {
        top = true;
        bot = false;
    }
    if (txtLabel.Location.X + txtLabel.Image.Width - (this.Bounds.Right - this.Bounds.Left) >= 0)
    {
        right = false;
        left = true;
    }
    // in relative coordinates left is always 0
    if (txtLabel.Location.X <= 0)
    {
        right = true;
        left = false;
    }
    // in relative coordinates top is always 0
    if (txtLabel.Location.Y <= 0)
    {
        top = false;
        bot = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: Have made a small mail program which works perfectly on my developer pc
I have made a new windows service which works fine using barebone code (just
I have made this example and it works fine on a plain aspx webpage.
I have made an application in openCVSharp.It works great. I have made the setup/installer
I have made an mvc3 application with Windows Authentication. Everything works fine but I
I have made a SSIS package to create an XML file, which works fine
I have made an application in which there is a file main.c which uses
I have made an AJAX chatroom; and it works in chrome and FF, but
I have made a Linux CUI app that communicates between processes via Message-quene. There
I have made a simple html page with an image on it to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.