Advertisement
gigi78

how the mousemove event was born

Sep 17th, 2020
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.63 KB | None | 0 0
  1. Public Class ArgomentiEvento
  2.  
  3.     Private _posizione As Point
  4.     Private _pulsante As Byte
  5.  
  6.     Public Property Posizione As Point
  7.         Get
  8.             Return _posizione
  9.         End Get
  10.         Set(value As Point)
  11.             _posizione = value
  12.         End Set
  13.     End Property
  14.  
  15.     Public Property Pulsante As Byte
  16.         Get
  17.             Return _pulsante
  18.         End Get
  19.         Set(value As Byte)
  20.             _pulsante = value
  21.         End Set
  22.     End Property
  23.  
  24.     Public Sub New(pos As Point, puls As Byte)
  25.  
  26.         Posizione = pos
  27.         Pulsante = puls
  28.  
  29.     End Sub
  30.  
  31. End Class
  32.  
  33. Public Class Form1
  34.  
  35.     Private Delegate Sub delegato(oggetto As Object, argomenti As argomentiEvento)
  36.     Private Event MuoviMouse As delegato
  37.     Private stringa As String
  38.     Private dbBuffer As BufferedGraphics
  39.     Private dbBufferContext As New BufferedGraphicsContext
  40.     Private Const WM_MOUSEMOVE As Long = &H200
  41.  
  42.     Protected Overrides Sub WndProc(ByRef m As Message)
  43.         MyBase.WndProc(m)
  44.  
  45.         Select Case m.Msg
  46.             Case WM_MOUSEMOVE
  47.                 Dim x As Integer
  48.                 Dim y As Integer
  49.  
  50.                 x = m.LParam.ToInt32 And &HFFFF
  51.                 y = m.LParam.ToInt32 >> 16
  52.  
  53.                 RaiseEvent MuoviMouse(Me, New ArgomentiEvento(New Point(x, y), m.WParam.ToInt32))
  54.  
  55.         End Select
  56.  
  57.     End Sub
  58.  
  59.     Private Sub Form_MuoviMouse(sender As Object, e As argomentiEvento) Handles Me.muoviMouse
  60.  
  61.         Dim pulsante As String = ""
  62.  
  63.         Select Case e.Pulsante
  64.             Case 0
  65.                 pulsante = "NESSUNO"
  66.             Case 1
  67.                 pulsante = "SINISTRO"
  68.             Case 2
  69.                 pulsante = "DESTRO"
  70.         End Select
  71.  
  72.         stringa = "X = " & e.Posizione.X & "   Y = " & e.Posizione.Y & "   pulsante premuto = " & pulsante
  73.  
  74.         Dim dimensione As SizeF = Me.CreateGraphics.MeasureString(stringa, New Font("Arial", 20, FontStyle.Bold))
  75.         Dim x As Integer = (Me.Width - dimensione.Width) / 2
  76.         Dim y As Integer = (Me.Height - dimensione.Height) / 2
  77.  
  78.         dbBuffer.Graphics.Clear(Me.BackColor)
  79.         dbBuffer.Graphics.DrawString(stringa, New Font("Arial", 20, FontStyle.Regular), New SolidBrush(Color.Black), New Point(x, y))
  80.         dbBuffer.Render(Me.CreateGraphics)
  81.  
  82.     End Sub
  83.  
  84.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  85.  
  86.         dbBufferContext.MaximumBuffer = Me.Size
  87.         dbBuffer = dbBufferContext.Allocate(Me.CreateGraphics, New Rectangle(New Point(0, 0), Me.Size))
  88.         dbBuffer.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  89.  
  90.     End Sub
  91. End Class
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement