VB - Simulando um hiperlink com o controle Label


Vou mostrar como obter o efeito de um hiperlink usando controle Label no Visual Basic step by step...
- Inicie o VB e no formulário padrão insira duas etiquetas - Label1 e Label2.

- No formulário padrão insira a seguinte declaração para a API - ShellExecute - pois vamos usá-la para chamar o Navegado padrão( Brownser para os íntimos).

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

- Agora no evento Load do formulário , insira o código abaixo para iniciar as etiquetas:(altere os endereços para o seu caso)

Private Sub Form_Load()
With Label1
  .AutoSize = True
  .ForeColor = vbBlue
  .Font.Underline = True
  .Caption = "http://www.geocities.com/macoratti"
End With
With Label2
  .AutoSize = True
  .ForeColor = vbBlue
  .Font.Underline = True
  .Caption = "macoratti@riopreto.com.br"
End With

End Sub

No evento de cada uma das etiquetas vamos invocar a API para abrir o Browser e o correspondente endereço ou URL.

Private Sub Label1_Click()
Dim ret&
ret = ShellExecute(Me.hwnd, "
OPen", "http://www.geocities.com/macoratti", "", "", 1)
End Sub
Private Sub Label2_Click()
Dim ret&
ret = ShellExecute(Me.hwnd,
"OPen", "mailto:macoratti@riopreto.com.br?subject=Qual o problema?", "", "", 1)
End Sub

Para terminar iremos tratar os eventos de mouse - MouseDown , MouseUp e MouseMove - sobre cada etiqueta :

Para Label 1 :

Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.ForeColor = vbBlue
End Sub
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.ForeColor = vbRed
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Label1.ForeColor = vbRed Then
    Label1.ForeColor = vbBlue
  ElseIf Label1.ForeColor = vbBlue Then
    Label1.ForeColor = vbRed
  End If
  Screen.MousePointer = 10
End Sub
Para etiqueta Label2 :
Private Sub Label2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.ForeColor = vbBlue
End Sub
Private Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.ForeColor = vbRed
End Sub
Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Label2.ForeColor = vbRed Then
    Label2.ForeColor = vbBlue
  ElseIf Label2.ForeColor = vbBlue Then
    Label2.ForeColor = vbRed
  End If
  Screen.MousePointer = 10
End Sub
Até a próxima...