Il Mouse
Mouse
C1. Mouse position with
reference to the Form
C1. Posizione Mouse con riferimento al Form
Inserire 2 TextBox e 3 Label
Il colore giallo dello sfondo cambia in rosso se si supera la coordinata X impostata.
C2. Assegnare un’attività al Mouse e delimitarla in uno spazio
Inserire 2 TextBox e 2 Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Risoluzione schermo
Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
Timer1.Enabled = True
TextBox1.Text = intX & " X " & intY
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString 'Posizione X
TextBox2.Text = System.Windows.Forms.Cursor.Position.Y.ToString 'Posizione Y
End Sub
C2. Mouse position with
reference to the screen
Insert two TextBoxes and two
Labels.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label3.Text = "Posizione X, Y"
Label1.Text = "Form : "
Label2.Text = "Schermo (tasto sx): "
Timer1.Start()
Timer1.Interval = 100
End Sub
'Trova la posizione nello schermo
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim x As Integer
Dim y As Integer
If MouseButtons = Windows.Forms.MouseButtons.Left Then
x = MousePosition.X
y = MousePosition.Y
TextBox1.Text = (x & " " & y)
End If
End Sub
'Trova la posizione nel Form
Private Sub form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles _
Me.MouseMove
TextBox2.Text = e.X & " " & e.Y 'Scrive le coordinate
If e.X > 150 Then
Me.BackColor = Color.Red
Else
Me.BackColor = Color.Yellow
End If
End Sub
C3. Rappresentare le attività del mouse
Inserire tre Label.
C3. Representing mouse
activities
Insert 3 Labels.
Insert 2 TextBox and 3 Label
The yellow color of the background
changes to red if you exceed the set
X coordinate.
Private Sub Form1_MouseEvent(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles MyBase.MouseDown, MyBase.MouseUp, MyBase.MouseMove, MyBase.MouseWheel
Label1.Text = ("Mouse Buttons : " & e.Button.ToString)
Label3.Text = String.Format("x={0} y={1} Clicks={2} Delta={3}", e.X, e.Y, e.Clicks,
e.Delta)
'Usare Location per leggere le coordinate
Label2.Text = e.Location.ToString
End Sub
C4. Assegnare attività al mouse
All’interno del rettangolo il cursore cambia forma ed esegue un beep quando si clicca con il tasto
sinistro.
Dimensionare il Form1 a 300x300 e inserire il seguente listato
Dim hotspot1 As Rectangle = New Rectangle(50, 50, 150, 150)
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If hotspot1.Contains(e.X, e.Y) Then
Beep()
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If hotspot1.Contains(e.X, e.Y) Then
Cursor = Cursors.Hand
Else
Cursor = Cursors.Default
End If
End Sub
C4. Assegnare attività al mouse
Inside the rectangle the cursor
changes shape and beeps when the
left button is clicked.
Size Form1 to 300x300 and enter the
following code
C5. Delimitare l’area di attività del mouse
Il mouse è attivo solo all’interno del Panel o all’interno del Form.
Creare un nuovo progetto con un Form 300x300, tre Button ed un Panel 200x200
Inserire il Button3 all'interno di Panel1.
C5. Delimit the mouse activity
area
The mouse is active only within the
Panel or within the Form.
Create a new project with a 300x300
Form, three Buttons and a 200x200
Panel
Inserire il Button3 all'interno di Panel1.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.BackColor = Color.Bisque
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Il cursore si muove solo all'interno di Panel1
Cursor.Clip = New Rectangle(Me.Panel1.Left + Me.Left,
Me.Panel1.Top + Me.Top, Me.Panel1.Width, Me.Panel1.Height)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Il cursore si muove solo all'interno del Form
Cursor.Clip = New Rectangle(Me.Left, Me.Top, Me.Width, Me.Height)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
C6. Sposta il cursore da una posizione ad un’altra
Inserire il Button1 in basso a destra del Form1.
C6. Move the cursor from one
position to another
Insert Button1 at the bottom right
of Form1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Cursor = New Cursor(Cursor.Current.Handle)
Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)
System.Threading.Thread.Sleep(500)
Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)
System.Threading.Thread.Sleep(500)
Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y + 50)
End Sub
C7. Altri esempi gestione Mouse
Inserire 3 Button 1 TextBox e 5 Label come da immagine.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Cursor = System.Windows.Forms.Cursors.Hand 'cambia aspetto cursore in croce
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Cursor = System.Windows.Forms.Cursors.Cross ‘cambia aspetto cursore in mano
End Sub
Private Sub TextBox1_MouseEnter(sender As Object, e As EventArgs) Handles TextBox1.MouseEnter
TextBox1.BackColor = Color.Blue ‘cambia colore sfondo
TextBox1.ForeColor = Color.White
End Sub
Private Sub TextBox1_MouseLeave(sender As Object, e As EventArgs) Handles TextBox1.MouseLeave
TextBox1.BackColor = Color.White ‘ripristina colore sfondo
TextBox1.ForeColor = Color.Blue
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Cursor.Position = Me.PointToScreen(Label1.Location) ‘‘posiziona cursore su Label1
End Sub
C7. Altri esempi gestione
Mouse
Insert 3 Button 1 TextBox e 5 Label
C8. Accompagnare un testo al cursore
Inserire 1 TextBox e 1 Label.
La parola Puntamento segue il cursore distanziata di 30 pixel a dx e 10 in basso
C8. Accompanying a text to
the cursor
Insert 1 TextBox e 1 Label.
The word Puntamento follows the
cursor spaced 30 pixels to the right
and 10 below.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Puntamento"
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Me.TextBox1.Location = New System.Drawing.Point(e.X + 30, e.Y + 10)
End Sub
C9. Accompagnare un’immagine al cursore
Inserire 1 Panel size 40x40
L’icona segue il cursore distanziata di 10 pixel a dx e 10 in basso
Private Immagine As System.Drawing.Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Immagine = New System.Drawing.Bitmap("J:\Immagini\Icone\Scopa.png")
Panel1.Visible = False
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Panel1.Visible = True 'Il Panel viene reso visibile
Panel1.Left = e.X + 10
Panel1.Top = e.Y + 10
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
e.Graphics.DrawImage(Immagine, 1, 1, 40, 40)
End Sub
C9. Accompanying an image
to the cursor
Insert 1 Panel 40x40
The icon follows the cursor spaced
10 pixels to the right and 10 pixels
downwards
C10. Far seguire un’immagine al cursore con effetto “elastico” (Trackball)
Una pallina segue in modo inerziale il cursore e rimbalza quando tocca i bordi dello schermo.
La grafica (Backcolor e TransparencyKey etc) configurata opzionalmente per annullare lo sfondo
dell’immagine bmp.
Immagine e file wav possono essere scaricati qui.
.bmp file
.wav file
Questo esempio è uno spunto interessante per sviluppare applicazioni di grafica dinamica
C9. Follow a cursor image with
"elastic" effect (Trackball)
A ball follows the cursor inertially and
bounces when it touches the edges of
the screen.
The graphics (Backcolor and
TransparencyKey etc) optionally
configured to cancel the background
of the bmp image.
Image and wav files can be
downloaded here.
.bmp file
.wav file
This example is an interesting starting
point for developing dynamic graphics
applications
'Fonte: http://www.vb-fun.de
Public Class Form1
Dim TSpeed As Integer = 1 'Decelerazione della palla
Dim Speed() As Double = {0, 0} 'Spinta
Dim RB As Double = 0.2 'Attrito
Dim BS As Double = 0.5 'Forza di attrazione del mouse
Dim MaxSpeed As Integer = 20 'Velocità massima della palla
Dim MPos As Point 'Posizione del mouse
'Limiti dello schermo
Dim Bound As Point = New Point(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
'
'Percorsi di file predefiniti
Dim UP As String = My.Computer.FileSystem.CurrentDirectory & "\data\"
Dim IP As String = UP & "gfx\" 'L'immagine .bmp 22x22 va posta in questa cartella
Dim SP As String = UP & "sound\" 'Il file .wav va posto in questa cartella
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Size = New Size(22, 22)
Me.BackColor = Color.Fuchsia 'corrisponde al colore di sfondo dell'immagine
Me.TransparencyKey = Color.Fuchsia 'corrisponde al colore di sfondo dell'immagine
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.StartPosition = FormStartPosition.CenterScreen
LoadImages()
Timer1.Interval = TSpeed
End Sub
'Carica il file immagine
Sub LoadImages()
BackgroundImage = Image.FromFile(IP & "Blackball.bmp")
End Sub
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
GetMPos()
'Controlla la posizione del mouse e poi sposta la palla
If MPos.X < Location.X Then
Speed(0) -= BS
End If
If MPos.X > Location.X Then
Speed(0) += BS
End If
If MPos.Y < Location.Y Then
Speed(1) -= BS
End If
If MPos.Y > Location.Y Then
Speed(1) += BS
End If
BoundingSpeed()
Collision()
SpeedDown(RB, 0)
SpeedDown(RB, 1)
Location += New Point(Speed(0), Speed(1))
End Sub
'Controllo delle collisioni
Sub Collision()
If Location.X <= 0 Then
Location = New Point(1, Location.Y)
Speed(0) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", AudioPlayMode.Background)
End If
If Location.X >= Bound.X - 20 Then
Location = New Point(Bound.X - 21, Location.Y)
Speed(0) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", AudioPlayMode.Background)
End If
If Location.Y <= 0 Then
Location = New Point(Location.X, 1)
Speed(1) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", AudioPlayMode.Background)
End If
If Location.Y >= Bound.Y - 20 Then
Location = New Point(Location.X, Bound.Y - 21)
Speed(1) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", AudioPlayMode.Background)
End If
End Sub
'limita la velocità massima se viene superata
Sub BoundingSpeed()
If Speed(0) < -MaxSpeed Then
Speed(0) = -MaxSpeed
End If
If Speed(0) > MaxSpeed Then
Speed(0) = MaxSpeed
End If
If Speed(1) < -MaxSpeed Then
Speed(1) = -MaxSpeed
End If
If Speed(1) > MaxSpeed Then
Speed(1) = MaxSpeed
End If
End Sub
'Decelera la sfera (attrito)
Sub SpeedDown(ByVal Value As Double, ByVal ax As Integer)
If Speed(ax) < 0 Then
Speed(ax) += Value
ElseIf Speed(ax) > 0 Then
Speed(ax) -= Value
End If
End Sub
'Scrive la posizione corrente del mouse in MPos
Sub GetMPos()
MPos = New Point(Control.MousePosition.X - 10, Control.MousePosition.Y - 10)
End Sub
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
'Click sulla pallina per uscire
Application.Exit()
End Sub
End Class
C11. Visualizzare un TextBox al passaggio del mouse sopra un controllo
Inserire nel form 2 RadioButton e 1 Label
Al passaggio del cursore sui RadioButton TextBox diventa visibile e il RdioButton corrispondente
diventa attivo.
C11. Display a TextBox when
move the mouse over a control
Insert in the form 2 RadioButton and 1
Label
As the cursor passes over the
RadioButton TextBox becomes visible
and the corresponding RdioButton
becomes active.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With RadioButton1
AddHandler .MouseMove, AddressOf mouseRB1In
AddHandler .MouseLeave, AddressOf mouseRB1Out
End With
With RadioButton2
AddHandler .MouseMove, AddressOf mouseRB2In
AddHandler .MouseLeave, AddressOf mouseRB2Out
End With
End Sub
Private Sub mouseRB1In(ByVal sender As System.Object, ByVal e As System.EventArgs)
If RadioButton1.Focus = True Then
TextBox1.Visible = True
TextBox1.Text = ("vbnetapplications.com")
End If
End Sub
Private Sub mouseRB1Out(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Visible = False
TextBox1.Text = ("")
End Sub
Private Sub mouseRB2In(ByVal sender As System.Object, ByVal e As System.EventArgs)
If RadioButton2.Focus = True Then
TextBox1.Visible = True
TextBox1.Text = ("http://codetips.vbnetapplications.com/")
End If
End Sub
Private Sub mouseRB2Out(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Visible = False
TextBox1.Text = ("")
End Sub
Folders
Backup
è
un’efficiente
applicazione
per
il
salvataggio
di
cartelle
o
intere
partizioni.
Offre
quanto
serve
per
garantire
una
corretta
gestione
dei
backup
con
una
notevole
semplicità
d’uso
e
un’interfaccia
che
permette
un’impostazione
alla
portata
dei
meno
esperti
e
che
fornisce
subito
tutte
le
informazioni
necessarie
per
avere
una
chiara visione dello stato dei propri backups.
Folders
Backup
is
an
efficient
application
for saving folders or entire partitions.
It
offers
what
you
need
to
ensure
proper
backup
management
with
a
remarkable
ease
of
use
and
an
interface
that
allows
a
setting
within
the
reach
of
the
less
experienced
and
that
immediately
provides
all
the
information
you
need
to
have
a
clear
view of the status of your backups.