Created
March 3, 2013 02:35
-
-
Save mizuneko/5074223 to your computer and use it in GitHub Desktop.
イベントハンドラを yield return
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.Collections.Generic; | |
using System.Drawing; | |
using System.Windows.Forms; | |
class MyForm : Form | |
{ | |
static void Main() | |
{ | |
Application.Run( new MyForm() ); | |
} | |
readonly List<Action<Graphics>> _entities = new List<Action<Graphics>>(); | |
IEnumerator<MouseEventHandler> _handler; | |
public MyForm() | |
{ | |
this.Paint += ( sender, e ) => | |
{ | |
foreach ( var draw in _entities ) draw( e.Graphics ); | |
}; | |
this.MouseClick += ( sender, e ) => | |
{ | |
if ( _handler != null ) { | |
_handler.Current( this, e ); | |
this.MoveNext(); | |
} | |
}; | |
_handler = this.DrawLines(); | |
this.MoveNext(); | |
} | |
void MoveNext() | |
{ | |
if ( !_handler.MoveNext() ) { | |
_handler.Dispose(); | |
_handler = null; | |
} | |
} | |
IEnumerator<MouseEventHandler> DrawLines() | |
{ | |
while ( true ) { | |
var p1 = new Point(); | |
var p2 = new Point(); | |
yield return ( sender, e ) => p1 = e.Location; | |
yield return ( sender, e ) => p2 = e.Location; | |
_entities.Add( g => g.DrawLine( new Pen( Brushes.Black ), p1, p2 ) ); | |
this.Invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2点をクリックすることで直線を描くことができるというお話。
いわゆる複数点間で何かイベント起こしたらどうこうするって時に役立ちそう。