Skip to content

Instantly share code, notes, and snippets.

@defaultlocale
Created August 19, 2019 10:05
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/7f716491fe75769bd05b6f75cf2bb945 to your computer and use it in GitHub Desktop.
Save defaultlocale/7f716491fe75769bd05b6f75cf2bb945 to your computer and use it in GitHub Desktop.
More complete Map Runner example
using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
class ExampleForm : Form
{
/// <summary>
/// Main method (just to run a form)
/// </summary>
[STAThread]
private static void Main()
{
Application.Run(new ExampleForm());
}
//All the variables
private Point startingPoint = Point.Empty;
private Point movingPoint = Point.Empty;
private bool panning;
/// <summary>
/// Main panel. Technically, drawing can be done on the form itself.
/// Creting a panel to keep it similar with original example
/// </summary>
private TableLayoutPanel tableLayoutPanel;
private ExampleForm()
{
InitiailizeComponent();
//not sure if this one is necessary for example
//keeping it to keep as close as possible to original code
typeof(Panel).InvokeMember("DoubleBuffered",
BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, tableLayoutPanel, new object[] { true });
}
/// <summary>
/// For simplicity just a panel, filling a form
/// Also, event handlers
/// </summary>
private void InitiailizeComponent()
{
//background image for panel
var backgroundImage = new Bitmap(10, 10);
var graphics = Graphics.FromImage(backgroundImage);
graphics.FillRectangle(Brushes.Red, 0, 0, 10,10);
Load += TableLayoutPanel_Load;
tableLayoutPanel = new TableLayoutPanel {Dock = DockStyle.Fill, BackgroundImage = backgroundImage};
tableLayoutPanel.MouseDown += TableLayoutPanel_MouseDown;
tableLayoutPanel.MouseUp += TableLayoutPanel_MouseUp;
tableLayoutPanel.MouseMove += TableLayoutPanel_MouseMove;
tableLayoutPanel.Paint += TableLayoutPanel_Paint;
Controls.Add(tableLayoutPanel);
}
/// <summary>
/// Create another form next to the main form when main form loads
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TableLayoutPanel_Load(object sender, EventArgs e)
{
//if this is the main form of an application
if (Application.OpenForms.Count != 1) return;
//create and show another form
var another = new ExampleForm
{
StartPosition = FormStartPosition.Manual,
Location = new Point(Location.X + Width, Location.Y)
};
another.Show();
}
//exact copy
private void TableLayoutPanel_MouseUp(object sender, MouseEventArgs e)
{
panning = false;
}
//exact copy
private void TableLayoutPanel_MouseDown(object sender, MouseEventArgs e)
{
panning = true;
startingPoint = new Point(e.Location.X - movingPoint.X,
e.Location.Y - movingPoint.Y);
}
//exact copy
private void TableLayoutPanel_MouseMove(object sender, MouseEventArgs e)
{
if (panning)
{
int xLocation = e.Location.X;
if (e.Location.X < 0)
xLocation = 0;
if (e.Location.X > tableLayoutPanel.Width)
xLocation = tableLayoutPanel.Width;
int yLocation = e.Location.Y;
if (e.Location.Y < 0)
yLocation = 0;
if (e.Location.Y > tableLayoutPanel.Height)
yLocation = tableLayoutPanel.Height;
movingPoint = new Point(xLocation - startingPoint.X,
yLocation - startingPoint.Y);
tableLayoutPanel.Invalidate();
var openForms = Application.OpenForms.OfType<ExampleForm>().Where(display => !GetHashCode().Equals(display.GetHashCode())).ToList();
foreach (var mapRunnerDisplay in openForms)
{
mapRunnerDisplay.UpdateMapPosition(movingPoint);
}
}
}
//exact copy
private void UpdateMapPosition(Point point)
{
movingPoint = point;
tableLayoutPanel.Invalidate();
}
//exact copy
private void TableLayoutPanel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Black);
e.Graphics.DrawImage(tableLayoutPanel.BackgroundImage, movingPoint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment