Created
August 19, 2019 09:36
-
-
Save defaultlocale/ed82b06e0ae05217967484e273916066 to your computer and use it in GitHub Desktop.
An example of two forms with a synchronized cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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