Skip to content

Instantly share code, notes, and snippets.

@blacktaxi
Last active December 12, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blacktaxi/4761874 to your computer and use it in GitHub Desktop.
Save blacktaxi/4761874 to your computer and use it in GitHub Desktop.
Tracking whether mouse pointer is inside a rectangle with .NET Rx.
private void Form1_Load(object sender, EventArgs e)
{
// get an observable for mouse move event
var mouseMove = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(
x => this.MouseMove += x, x => this.MouseMove -= x);
// our rectangle
var rect = new Rectangle(50, 50, 50, 50);
// create event stream transformation
mouseMove
.Select(ea => rect.Contains(ea.EventArgs.X, ea.EventArgs.Y))
.DistinctUntilChanged()
.Select(x => x ? "Inside!" : "Outside")
.Subscribe(x => MessageBox.Show(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment