Comment : utiliser les tests d'atteinte avec une région

L’objectif du test de positionnement est de déterminer si le curseur se trouve sur un objet donné, tel qu’une icône ou un bouton.

Exemple

L’exemple suivant crée une zone en forme plus en formant l’union de deux régions rectangulaires. Supposons que la variable point contient l’emplacement du clic le plus récent. Le code case activée pour voir s’il point se trouve dans la région plus mise en forme. Si le point se trouve dans la région (un coup), la région est remplie d’un pinceau rouge opaque. Sinon, la région est remplie d’un pinceau rouge semi-transparent.

Point point = new Point(60, 10);

// Assume that the variable "point" contains the location of the
// most recent mouse click.
// To simulate a hit, assign (60, 10) to point.
// To simulate a miss, assign (0, 0) to point.

SolidBrush solidBrush = new SolidBrush(Color.Black);
Region region1 = new Region(new Rectangle(50, 0, 50, 150));
Region region2 = new Region(new Rectangle(0, 50, 150, 50));

// Create a plus-shaped region by forming the union of region1 and
// region2.
// The union replaces region1.
region1.Union(region2);

if (region1.IsVisible(point, e.Graphics))
{
    // The point is in the region. Use an opaque brush.
    solidBrush.Color = Color.FromArgb(255, 255, 0, 0);
}
else
{
    // The point is not in the region. Use a semitransparent brush.
    solidBrush.Color = Color.FromArgb(64, 255, 0, 0);
}

e.Graphics.FillRegion(solidBrush, region1);
Dim point As New Point(60, 10)

' Assume that the variable "point" contains the location of the
' most recent mouse click.
' To simulate a hit, assign (60, 10) to point.
' To simulate a miss, assign (0, 0) to point.

Dim solidBrush As New SolidBrush(Color.Black)
Dim region1 As New [Region](New Rectangle(50, 0, 50, 150))
Dim region2 As New [Region](New Rectangle(0, 50, 150, 50))

' Create a plus-shaped region by forming the union of region1 and region2.
' The union replaces region1.
region1.Union(region2)

If region1.IsVisible(point, e.Graphics) Then
    ' The point is in the region. Use an opaque brush.
    solidBrush.Color = Color.FromArgb(255, 255, 0, 0)
Else
    ' The point is not in the region. Use a semitransparent brush.
    solidBrush.Color = Color.FromArgb(64, 255, 0, 0)
End If

e.Graphics.FillRegion(solidBrush, region1)

Compilation du code

L'exemple précédent est conçu pour une utilisation avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre de PaintEventHandler.

Voir aussi