Skip to content

Instantly share code, notes, and snippets.

@maximilian-krauss
Created March 23, 2012 12:15
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 maximilian-krauss/2170153 to your computer and use it in GitHub Desktop.
Save maximilian-krauss/2170153 to your computer and use it in GitHub Desktop.
Lunchtimestunt #1
using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using System.Drawing;class T { class L : Control { List<Point> dataPoints = new List<Point>(); public delegate void LeMoveHandler(object sender, Point cords); public event LeMoveHandler LeMove; public L() { SetStyle(ControlStyles.OptimizedDoubleBuffer, true); MouseMove += (o, e) => { Invalidate(); onLeMove(); }; MouseLeave += (o, e) => { Invalidate(); onLeMove(); }; Click += (o, e) => { dataPoints.Add(PointToClient(new Point(MousePosition.X, MousePosition.Y))); Invalidate(); }; } void onLeMove() { if (LeMove != null) LeMove(this, PointToClient(new Point(MousePosition.X, MousePosition.Y))); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle); dataPoints.ForEach(p => e.Graphics.FillRectangle(Brushes.Green, new Rectangle(new Point(p.X - (6 / 2), p.Y - (6 / 2)), new Size(6, 6)))); if (dataPoints.Count > 1) e.Graphics.DrawLines(Pens.Yellow, dataPoints.ToArray()); var mPos = PointToClient(new Point(MousePosition.X, MousePosition.Y)); using (var pLine = new Pen(Color.Red) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash }) { e.Graphics.DrawLine(pLine, new Point(0, mPos.Y), new Point(ClientRectangle.Width, mPos.Y)); e.Graphics.DrawLine(pLine, new Point(mPos.X, 0), new Point(mPos.X, ClientRectangle.Height)); } if (!ClientRectangle.Contains(mPos)) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(190, Color.Maroon)), ClientRectangle); TextRenderer.DrawText(e.Graphics, "Move your damn Mouse in here!", new Font(Font.FontFamily, 10), ClientRectangle, Color.White, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); } e.Graphics.DrawRectangle(SystemPens.ControlDark, new Rectangle(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1)); } } static void Main() { var f = new Form { Size = new Size(400, 400), Text = "LulForm", StartPosition = FormStartPosition.CenterScreen }; f.Controls.Add(new L { Dock = DockStyle.Fill, BackColor = Color.Black }); (f.Controls[0] as L).LeMove += (o, p) => { f.Text = string.Format("x:{0} y:{1}", p.X, p.Y); }; Application.Run(f); } }
@maximilian-krauss
Copy link
Author

Yes, it compiles :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment