Compartilhar via


Como: Uso teste de Hit com uma região

O objetivo do teste de visitas é determinar se o cursor está sobre um determinado objeto, como, por exemplo, um ícone ou um botão.

Exemplo

O exemplo a seguir cria uma região em forma de sinal de adição, formando a união de duas regiões retangulares. Suponha que a variável point contém o local do clique mais recente. O código verifica para ver se point é a região em forma de sinal de adição. Se o ponto estiver na região (um acerto), a região é preenchida com um pincel vermelho opaco. Caso contrário, a região é preenchida com um pincel vermelho semitransparente.

        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)

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);

Compilando o código

O exemplo anterior é projetado para uso com o Windows Forms e requer PaintEventArgs e, que é um parâmetro de PaintEventHandler.

Consulte também

Tarefas

Como: Usar o corte com uma região

Referência

Region

Conceitos

Regiões em GDI+