VB Net - Basic Mouse Info
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class Form1
<DllImport(”gdi32.dll”)> Private Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As Integer
End Function
<DllImport(”gdi32.dll”)> Private Shared Function CreateDC(ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport(”gdi32.dll”)> Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
Private Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
Dim hdcScreen As IntPtr = _
CreateDC(”Display”, Nothing, Nothing, IntPtr.Zero)
Dim colorRef As Integer = GetPixel(hdcScreen, x, y)
DeleteDC(hdcScreen)
Return Color.FromArgb(colorRef And &HFF, (colorRef And &HFF00) >> 8, (colorRef And &HFF0000) >> 16)
End Function
<DllImport(”user32.dll”, SetLastError:=True)> Private Shared Function BringWindowToTop(ByVal hwnd As IntPtr) As Boolean
End Function
<DllImport(”user32.dll”, SetLastError:=True)> Private Shared Function GetActiveWindow() As IntPtr
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.BackColor = Color.FromArgb(GetPixelColor(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y).ToArgb)
Dim screenmax As Size
screenmax.Height = My.Computer.Screen.WorkingArea.Bottom - Me.Height
screenmax.Width = My.Computer.Screen.WorkingArea.Right - Me.Width
X.Text = “X: ” & Windows.Forms.Cursor.Position.X
Y.Text = “Y: ” & Windows.Forms.Cursor.Position.Y
Z.Text = “ARGB: ” & GetPixelColor(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y).A & ” ” & GetPixelColor(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y).R & ” ” & GetPixelColor(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y).G & ” ” & GetPixelColor(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y).B
BringWindowToTop(Me.Handle.ToInt32)
Dim scs As Size
scs.Height = My.Computer.Screen.WorkingArea.Height
scs.Width = My.Computer.Screen.WorkingArea.Width
Dim locat As Point
locat = Windows.Forms.Cursor.Position
locat.X += 3
locat.Y += 3
If Windows.Forms.Cursor.Position.Y > scs.Height - Me.Height Then
If Windows.Forms.Cursor.Position.X > scs.Width - Me.Width Then
Dim sizh As Integer = Me.Height
Dim sizw As Integer = Me.Width
Dim loct2 As Point
loct2 = Windows.Forms.Cursor.Position
locat.X = Windows.Forms.Cursor.Position.X - sizw
locat.Y = Windows.Forms.Cursor.Position.Y - sizh
Me.Location = locat
Else
Dim sizh As Integer = Me.Height
Dim sizw As Integer = Me.Width
Dim loct2 As Point
loct2 = Windows.Forms.Cursor.Position
locat.X = locat.X
locat.Y = Windows.Forms.Cursor.Position.Y - sizh
Me.Location = locat
End If
ElseIf Windows.Forms.Cursor.Position.X > scs.Width - Me.Width Then
If Windows.Forms.Cursor.Position.Y > scs.Height - Me.Height Then
Dim sizh As Integer = Me.Height
Dim sizw As Integer = Me.Width
Dim loct2 As Point
loct2 = Windows.Forms.Cursor.Position
locat.X = Windows.Forms.Cursor.Position.X - sizw
locat.Y = Windows.Forms.Cursor.Position.Y - sizh
Me.Location = locat
Else
Dim sizh As Integer = Me.Height
Dim sizw As Integer = Me.Width
Dim loct2 As Point
loct2 = Windows.Forms.Cursor.Position
locat.Y = locat.Y
locat.X = Windows.Forms.Cursor.Position.X - sizw
Me.Location = locat
End If
Else
Me.Location = locat
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
End Class
Displays a box next to your cursor with the mouse location, and the colour of the pixel it is over in ARGB and as an image
Comments(0)