Je me suis amusé à utiliser des marqueurs comme si c'etait des sprites:
namespace HitTest
{
public class Mark
{
private Bitmap bmp;
private Color mask;
private Rectangle bounds;
public Rectangle Bounds { get { return bounds; } }
public Mark( Bitmap bmp, Rectangle bounds )
{
this.bmp = bmp;
this.bounds = bounds;
this.mask = bmp.GetPixel( 0, 0 );
}
public void Draw( Graphics g )
{
if ( bmp != null )
{
g.DrawImage( bmp, bounds ); // Add try/catch.
}
}
public bool Hit( int x, int y )
{
if ( bounds.Contains( x, y ) )
{
x = x - bounds.X;
y = y - bounds.Y;
if ( bmp.GetPixel( x, y ) != mask )
{
return true;
}
}
return false;
}
}
public class MainForm : Form
{
private Bitmap bmp;
private ArrayList marks;
public MainForm( ) : base( )
{
this.Text = "HitTest";
this.SetStyle( ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true );
this.Visible = true;
bmp = new Bitmap( "mark.bmp" ); // Add try/catch
bmp.MakeTransparent( bmp.GetPixel( 0, 0 ) );
marks = new ArrayList( );
marks.Add( new Mark( bmp, new Rectangle( 0, 0, 48, 48 ) ) );
marks.Add( new Mark( bmp, new Rectangle( 100, 100, 48, 48 ) ) );
}
protected override void OnMouseDown( MouseEventArgs e )
{
//base.OnMouseDown( e );
if ( e.Button == MouseButtons.Left )
{
foreach( Mark m in marks )
{
if ( m.Hit( e.X, e.Y ) )
{
MessageBox.Show( "Hit !" );
}
}
}
}
protected override void OnPaint( PaintEventArgs e )
{
//base.OnPaint( e );
Graphics g = e.Graphics;
foreach( Mark m in marks )
{
m.Draw( g );
}
}
[ STAThread ]
private static void Main( )
{
Application.EnableVisualStyles( );
Application.Run( new MainForm( ) );
}
}
}
Par exemple si le marqueur ressemble au drapeau japonais, càd détouré avec une couleur unique et un rond au centre, c'est seulement en cliquant sur le rond que le message "Hit !" apparaît, si c'est pas ce que tu veux faire c'est pas grave, ca servira à d'autres, me suis amusé : )