HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Scrolly - A (very) simple infinite mouse "scroll"

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scrollyinfinitesimplescrollverymouse

Problem

Out of fun, and to practice my rusty C# skills, I've made a very basic program.

It only has 1 function: When you move the mouse to a side of the screen, it shown on the other side. Like an infinite scroll!

I am totally aware that there are users with 2 or more screens. I'm sorry, but it only works for 1. It's a limitation I've imposed on purpose.

I've used C# 6.0 to make this project, and I guarantee that it works with that very specific version.

Program.cs

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

namespace Scrolly
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //hides the form at startup
            Form form = new settings();
            Application.Run();
        }
    }
}


settings.Designer.cs (ignorable, but required to compile)

```
namespace Scrolly
{
partial class settings
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();

Solution

private void ticker_Tick(object sender, EventArgs e)
{
    Point pos = Cursor.Position;

    if (pos.X = screen.Bounds.Width - paddingX.Value - 1)
    {
        Cursor.Position = new Point(paddingX.Value + 2, Cursor.Position.Y);
    }

    if (pos.Y = screen.Bounds.Height - paddingY.Value - 1)
    {
        Cursor.Position = new Point(Cursor.Position.X, paddingY.Value + 2);
    }

}


-
Don't use abbreviations for variable names.

-
the calculation of the new position of the Cursor should be extracted to a separate method to be called from the event.

-
Instead of assigning the cursors position twice, you should calculate the X and Y values and then assign the calculated values to the position.

Implementing the mentioned points will lead to

private Point CalculateCursorPosition(Point currentPosition, Rectangle bounds, int offsetX, int offsetY)
{
    // initialize x and y to the former values for the case that none of the
    // conditions will be met.  

    int x = currentPosition.X;
    int y = currentPosition.Y;

    if (currentPosition.X = bounds.Width - offsetX - 1)
    {
        x = offsetX + 2;
    }

    if (currentPosition.Y = bounds.Height - offsetY - 1)
    {
        y = offsetY + 2;
    }

    return new Point(x, y);
}   

private void ticker_Tick(object sender, EventArgs e)
{

    Cursor.Position = CalculateCursorPosition(Cursor.Position, screen.Bounds,  
                                              paddingX.Value, paddingY.Value);

}


To keep the amount of passed method arguments low I have decided to pass the Cursor.Position instead of two parameters Cursor.Position.X and Cursor.Position.Y.

Code Snippets

private void ticker_Tick(object sender, EventArgs e)
{
    Point pos = Cursor.Position;

    if (pos.X <= paddingX.Value)
    {
        Cursor.Position = new Point(screen.Bounds.Width - paddingX.Value - 1, Cursor.Position.Y);
    }
    else if (pos.X >= screen.Bounds.Width - paddingX.Value - 1)
    {
        Cursor.Position = new Point(paddingX.Value + 2, Cursor.Position.Y);
    }

    if (pos.Y <= paddingY.Value)
    {
        Cursor.Position = new Point(Cursor.Position.X, screen.Bounds.Height - paddingY.Value - 1);
    }
    else if (pos.Y >= screen.Bounds.Height - paddingY.Value - 1)
    {
        Cursor.Position = new Point(Cursor.Position.X, paddingY.Value + 2);
    }

}
private Point CalculateCursorPosition(Point currentPosition, Rectangle bounds, int offsetX, int offsetY)
{
    // initialize x and y to the former values for the case that none of the
    // conditions will be met.  

    int x = currentPosition.X;
    int y = currentPosition.Y;

    if (currentPosition.X <= offsetX)
    {
        x = bounds.Width - offsetX - 1;
    }
    else if (currentPosition.X >= bounds.Width - offsetX - 1)
    {
        x = offsetX + 2;
    }

    if (currentPosition.Y <= offsetY)
    {
        y = bounds.Height - offsetY - 1;
    }
    else if (currentPosition.Y >= bounds.Height - offsetY - 1)
    {
        y = offsetY + 2;
    }

    return new Point(x, y);
}   

private void ticker_Tick(object sender, EventArgs e)
{

    Cursor.Position = CalculateCursorPosition(Cursor.Position, screen.Bounds,  
                                              paddingX.Value, paddingY.Value);

}

Context

StackExchange Code Review Q#100645, answer score: 6

Revisions (0)

No revisions yet.