Last active
December 12, 2015 10:48
-
-
Save blacktaxi/4761874 to your computer and use it in GitHub Desktop.
Tracking whether mouse pointer is inside a rectangle with .NET Rx.
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
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