Ivan Gentile

Office and .NET blog
posts - 24, comments - 0, trackbacks - 2

Confinare il Mouse in una form in VB.Net

Questo è un metodo per impedire che il mouse esca dal Form corrente. Una volta attivato, il mouse non potrà essere spostato fuori dal Form.

 

 

ATTENZIONE: Vi consiglio di stare molto attenti a seguire i passi della mia descrizione o a non uscire dal Form prima di averlo sbloccato. Vi garantisco che una volta chiuso senza sbloccarlo è davvero un “casino” rimettere tutto apposto.

1) Impostare le proprietà della Form seguenti:

     Text = “Blocco Mouse Demo”

     FormBorderStyle = FixedDialog

     MaximizeBox = False

     MinimizeBox = False

2) Inserire una Label di nome Label1:

     Text = ”Il Cursore E' Libero Su Tutto Lo Schermo!!”

3) Inserire un Button di nome Button1:

     Text = “Sblocca”

4) Inserire un Button di nome Button2:

     Text = “Blocca”

Come in figura sopra.

Aggiungete il seguente codice:

Imports System.Windows.Forms

Imports System.Windows.Forms.Cursors

Public Class Form1

    Declare Function ClipCursor Lib "user32" (ByRef lpRect As Rectangle) As Integer

    Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, _

                ByRef lpRect As Rectangle) As Integer

    Declare Function GetDesktopWindow Lib "user32" () As Integer

   

    Dim Zona As Rectangle

    Private Sub Blocca()

        Dim retval As Long

        ' Inserisce le cordinate del Form in Zona

        retval = GetWindowRect(Me.Handle, Zona) 

        ' Confina il cursore al Form

        retval = ClipCursor(Zona)

        Label1.Text = "Il Cursore E' Confinato Alla Form!!"

    End Sub

   

    Private Sub Sblocca()

        Dim retval As Long

        Dim Schermo As Long

        ' Cattura l'Handle dello schermo

        Schermo = GetDesktopWindow()

        ' Inserisce le coordinate dello schermo in Zona

        retval = GetWindowRect(Schermo, Zona)

        ' Confina il cursore allo schermo

        retval = ClipCursor(Zona) 

        Label1.Text = "Il Cursore E' Libero Su Tutto Lo Schermo!!"

    End Sub

   

    Private Sub Button2_Click(ByVal sender As System.Object, _

                    ByVal e As System.EventArgs) Handles Button2.Click

        Blocca()

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, _

                   ByVal e As System.EventArgs) Handles Button1.Click

        Sblocca()

    End Sub

 

    Private Sub Form1_Disposed(ByVal sender As Object, _

                  ByVal e As System.EventArgs) Handles Me.Disposed

        Sblocca()

    End Sub

End Class

 

Il gioco è fatto!!!

Ciao

Ivan

posted on sabato 28 gennaio 2006 20.08