Skip to content

Instantly share code, notes, and snippets.

@defaultlocale
Created August 19, 2019 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defaultlocale/ed82b06e0ae05217967484e273916066 to your computer and use it in GitHub Desktop.
Save defaultlocale/ed82b06e0ae05217967484e273916066 to your computer and use it in GitHub Desktop.
An example of two forms with a synchronized cursor
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
class MapRunnerDisplay : Form
{
[STAThread]
private static void Main()
{
Application.Run(new MapRunnerDisplay());
}
private Point movingPoint = Point.Empty;
private MapRunnerDisplay()
{
Load += MapRunnerDisplay_Load;
MouseMove += MapRunnerDisplay_MouseMove;
Paint += MapRunnerDisplay_Paint;
}
private void MapRunnerDisplay_Load(object sender, EventArgs e)
{
if (Application.OpenForms.Count != 1) return;
var another = new MapRunnerDisplay
{
StartPosition = FormStartPosition.Manual,
Location = new Point(Location.X + Width, Location.Y + Height)
};
another.Show();
}
private void MapRunnerDisplay_MouseMove(object sender, MouseEventArgs e)
{
foreach (var another in Application.OpenForms.OfType<MapRunnerDisplay>())
{
another.UpdatePosition(e.Location);
}
}
private void UpdatePosition(Point point)
{
movingPoint = point;
Invalidate();
}
private void MapRunnerDisplay_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.DrawEllipse(new Pen(Color.Red), movingPoint.X - 2, movingPoint.Y - 2, 4, 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment