CodeSnippetPrivate colorToFind AsKnownColor=4928371PrivateSub b_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Handles b.ClickThreading.Thread.Sleep(1000)Application.DoEvents()Dim searchRectangle AsNewRectangle(1,1,1280,800)Dim foundPt AsPoint=PixelSearch(searchRectangle, colorToFind)Me.Text=String.Format("Pixel location in bitmap: ({0},{1})", foundPt.X.ToString, foundPt.Y.ToString)' move mouse and click
Windows.Forms.Cursor.Position=NewPoint(foundPt.X + searchRectangle.X, foundPt.Y + searchRectangle.Y)NativeMethods.MouseClick()EndSub' returns (-1,-1)if not found
PrivateFunctionPixelSearch(ByVal rec AsRectangle,ByVal colorToFind AsInteger)AsPoint' find the location in the bitmap
Dim x AsInteger=-1Dim y AsInteger=-1'First grab the screen
Using bm AsNewBitmap(rec.Width, rec.Height)'Copy a portion of the screen.Dim topleft AsPoint=NewPoint(rec.X, rec.Y)Using g AsGraphics=Graphics.FromImage(bm)
g.CopyFromScreen(topleft,NewPoint, rec.Size)EndUsing' lock the bits
Dim bmd AsBitmapData= bm.LockBits(NewRectangle(0,0, bm.Width, bm.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb)' copy all pixels out into an integer array.Dim pixels(bm.Width* bm.Height-1)AsIntegerMarshal.Copy(bmd.Scan0, pixels,0, pixels.Length)' find the color
For i AsInteger=0To pixels.Length-1If pixels(i)= colorToFind Then
x = i Mod bm.Width
y = i \ bm.WidthExitForEndIfNext' unlock bm
bm.UnlockBits(bmd)EndUsingReturnNewPoint(x, y)EndFunction
CodeSnippetOptionStrictOnOptionExplicitOnImportsSystem.ComponentModelImportsSystem.Drawing.ImagingImportsSystem.Runtime.InteropServicesPublicClassForm1Private rand AsRandomPrivate colorToFind AsInteger=&HFFFF0000 '&HFF all rest are &HFE
PrivateWithEvents b AsNewButtonPrivate colorPoint AsPointSubNew()'This call is required by the WindowsFormDesigner.InitializeComponent()'Add any initialization after the InitializeComponent() call.
b.Location=NewPoint(10,10)
b.Text="go"Me.Controls.Add(b)
rand =NewRandomEndSub'Create a random colored bitmap, quickly.PrivateFunctionGetRandomColorBitmap(ByVal size AsSize)AsBitmapDim bm AsNewBitmap(size.Width, size.Height)Dim bmd AsBitmapData= bm.LockBits(NewRectangle(NewPoint, size),ImageLockMode.WriteOnly,PixelFormat.Format32bppArgb)Dim colors(size.Width* size.Height-1)AsIntegerFor i AsInteger=0To colors.Length-1
colors(i)= rand.Next(Integer.MinValue,Integer.MaxValue)Or&HFE000000
NextMarshal.Copy(colors,0, bmd.Scan0, colors.Length)
bm.UnlockBits(bmd)Return bm
EndFunction'The point just happens to be on the form, it can also be Off the form...PrivateSub b_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Handles b.Click
b.Visible=FalseMessageBox.Show("I'm going to draw 1 pixel on the form with a unique red color, then I'm going to wait for 1 second, then I'm going to draw random mess all over the form, and redraw the pixel on top, then I'm going to move the mouse over the pixel and click.")Using g AsGraphics=Me.CreateGraphics
colorPoint =NewPoint(rand.Next(0,Me.ClientSize.Width), rand.Next(0,Me.ClientSize.Height))
g.FillRectangle(NewSolidBrush(Color.FromArgb(colorToFind)),NewRectangle(colorPoint,NewSize(1,1)))
g.DrawEllipse(Pens.Black,NewRectangle(colorPoint.X -10, colorPoint.Y -10,20,20))EndUsingThreading.Thread.Sleep(1000)Dim backg AsBitmap=GetRandomColorBitmap(Me.ClientSize)Using g AsGraphics=Graphics.FromImage(backg)
g.FillRectangle(NewSolidBrush(Color.FromArgb(colorToFind)),NewRectangle(colorPoint,NewSize(1,1)))EndUsingMe.BackgroundImage= backg
Application.DoEvents()Dim searchRectangle AsNewRectangle
searchRectangle.Location=Me.PointToScreen(NewPoint)' top left of client area
searchRectangle.Size=Me.ClientSizeDim foundPt AsPoint=PixelSearch(searchRectangle, colorToFind)Me.Text=String.Format("Pixel location in bitmap: ({0},{1})", foundPt.X.ToString, foundPt.Y.ToString)' move mouse and click
Cursor.Position=NewPoint(foundPt.X + searchRectangle.X, foundPt.Y + searchRectangle.Y)NativeMethods.MouseClick()EndSubPrivateSubForm1_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)HandlesMe.Click
b.Visible=TrueDim pt AsPoint=Me.PointToClient(Cursor.Position)MessageBox.Show(String.Format("Point clicked:{0}{1}Color point:{2}", _
pt.ToString,Environment.NewLine, colorPoint.ToString))EndSub' returns (-1,-1)if not found
PrivateFunctionPixelSearch(ByVal rec AsRectangle,ByVal colorToFind AsInteger)AsPoint'Right, there is one pixel on the form in the correct color.' find the location in the bitmap
Dim x AsInteger=-1Dim y AsInteger=-1'First grab the screen
Using bm AsNewBitmap(rec.Width, rec.Height)'Copy a portion of the screen.Dim topleft AsPoint=NewPoint(rec.X, rec.Y)Using g AsGraphics=Graphics.FromImage(bm)
g.CopyFromScreen(topleft,NewPoint, rec.Size)EndUsing' lock the bits
Dim bmd AsBitmapData= bm.LockBits(NewRectangle(0,0, bm.Width, bm.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb)' copy all pixels out into an integer array.Dim pixels(bm.Width* bm.Height-1)AsIntegerMarshal.Copy(bmd.Scan0, pixels,0, pixels.Length)' find the color
For i AsInteger=0To pixels.Length-1If pixels(i)= colorToFind Then
x = i Mod bm.Width
y = i \ bm.WidthExitForEndIfNext' unlock bm
bm.UnlockBits(bmd)EndUsingReturnNewPoint(x, y)EndFunctionEndClassFriendClassNativeMethods<DllImport("user32.dll",SetLastError:=True)> _
PrivateSharedFunctionSendInput( _
ByVal cInputs AsInteger, _
ByVal pInputs()As INPUT, _
ByVal cbSize AsInteger)AsIntegerEndFunctionPrivateStructure INPUT
Public dwType AsIntegerPublic mi As MOUSEINPUT
EndStructure<StructLayout(LayoutKind.Sequential)> _
PrivateStructure MOUSEINPUT
Public dx AsIntegerPublic dy AsIntegerPublic mouseData AsUIntegerPublic dwFlags AsUIntegerPublic time AsUIntegerPublic dwExtraInfo AsIntPtrEndStructurePrivateConst INPUT_MOUSE AsInteger=0PrivateConst MOUSEEVENTF_LEFTDOWN AsInteger=&H2
PrivateConst MOUSEEVENTF_LEFTUP AsInteger=&H4
PrivateConst MOUSEEVENTF_MIDDLEDOWN AsInteger=&H20
PrivateConst MOUSEEVENTF_MIDDLEUP AsInteger=&H40
PrivateConst MOUSEEVENTF_MOVE AsInteger=&H1
PrivateConst MOUSEEVENTF_ABSOLUTE AsInteger=&H8000
PrivateConst MOUSEEVENTF_RIGHTDOWN AsInteger=&H8
PrivateConst MOUSEEVENTF_RIGHTUP AsInteger=&H10
' not accurate!PublicSharedSubMoveMouseAndClick(ByVal x AsInteger,ByVal y AsInteger)Dim inputs(2)As INPUT
For i AsInteger=0To inputs.Length-1
inputs(i).dwType = INPUT_MOUSE
Next
inputs(0).mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE
' oh, this isn't accurate. I've abandoned it and used Cursor.Position instead.
inputs(0).mi.dx =Convert.ToInt32(Math.Ceiling(x *65535/Screen.PrimaryScreen.Bounds.Width))
inputs(0).mi.dy =Convert.ToInt32(Math.Ceiling(y *65535/Screen.PrimaryScreen.Bounds.Height))
inputs(1).mi.dwFlags = MOUSEEVENTF_LEFTDOWN
inputs(2).mi.dwFlags = MOUSEEVENTF_LEFTUP
Dim cbSize AsInteger=Marshal.SizeOf(inputs(0))Dim result AsInteger=SendInput(inputs.Length, inputs, cbSize)If result =0ThenThrowNewSystem.ComponentModel.Win32ExceptionEndIfEndSubPublicSharedSubMouseClick()Dim inputs(1)As INPUT
For i AsInteger=0To inputs.Length-1
inputs(i).dwType = INPUT_MOUSE
Next
inputs(0).mi.dwFlags = MOUSEEVENTF_LEFTDOWN
inputs(1).mi.dwFlags = MOUSEEVENTF_LEFTUP
Dim cbSize AsInteger=Marshal.SizeOf(inputs(0))Dim result AsInteger=SendInput(inputs.Length, inputs, cbSize)If result =0ThenThrowNewSystem.ComponentModel.Win32ExceptionEndIfEndSubEndClass
CodeSnippetOptionStrictOnOptionExplicitOnImportsSystem.ComponentModelImportsSystem.Drawing.ImagingImportsSystem.Runtime.InteropServicesPublicClassForm1Private rand AsRandomPrivate colorToFind AsInteger=&HFFFF0000 '&HFF all rest are &HFE
PrivateWithEvents b AsNewButtonPrivate colorPoint AsPointSubNew()'This call is required by the WindowsFormDesigner.InitializeComponent()'Add any initialization after the InitializeComponent() call.
b.Location=NewPoint(10,10)
b.Text="go"Me.Controls.Add(b)
rand =NewRandomEndSub'Create a random colored bitmap, quickly.PrivateFunctionGetRandomColorBitmap(ByVal size AsSize)AsBitmapDim bm AsNewBitmap(size.Width, size.Height)Dim bmd AsBitmapData= bm.LockBits(NewRectangle(NewPoint, size),ImageLockMode.WriteOnly,PixelFormat.Format32bppArgb)Dim colors(size.Width* size.Height-1)AsIntegerFor i AsInteger=0To colors.Length-1
colors(i)= rand.Next(Integer.MinValue,Integer.MaxValue)Or&HFE000000
NextMarshal.Copy(colors,0, bmd.Scan0, colors.Length)
bm.UnlockBits(bmd)Return bm
EndFunction'The point just happens to be on the form, it can also be Off the form...PrivateSub b_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Handles b.Click
b.Visible=FalseMessageBox.Show("I'm going to draw 1 pixel on the form with a unique red color, then I'm going to wait for 1 second, then I'm going to draw random mess all over the form, and redraw the pixel on top, then I'm going to move the mouse over the pixel and click.")Using g AsGraphics=Me.CreateGraphics
colorPoint =NewPoint(rand.Next(0,Me.ClientSize.Width), rand.Next(0,Me.ClientSize.Height))
g.FillRectangle(NewSolidBrush(Color.FromArgb(colorToFind)),NewRectangle(colorPoint,NewSize(1,1)))
g.DrawEllipse(Pens.Black,NewRectangle(colorPoint.X -10, colorPoint.Y -10,20,20))EndUsingThreading.Thread.Sleep(1000)Dim backg AsBitmap=GetRandomColorBitmap(Me.ClientSize)Using g AsGraphics=Graphics.FromImage(backg)
g.FillRectangle(NewSolidBrush(Color.FromArgb(colorToFind)),NewRectangle(colorPoint,NewSize(1,1)))EndUsingMe.BackgroundImage= backg
Application.DoEvents()Dim searchRectangle AsNewRectangle
searchRectangle.Location=Me.PointToScreen(NewPoint)' top left of client area
searchRectangle.Size=Me.ClientSizeDim foundPt AsPoint=PixelSearch(searchRectangle, colorToFind)Me.Text=String.Format("Pixel location in bitmap: ({0},{1})", foundPt.X.ToString, foundPt.Y.ToString)' move mouse and click
Cursor.Position=NewPoint(foundPt.X + searchRectangle.X, foundPt.Y + searchRectangle.Y)NativeMethods.MouseClick()EndSubPrivateSubForm1_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)HandlesMe.Click
b.Visible=TrueDim pt AsPoint=Me.PointToClient(Cursor.Position)MessageBox.Show(String.Format("Point clicked:{0}{1}Color point:{2}", _
pt.ToString,Environment.NewLine, colorPoint.ToString))EndSub' returns (-1,-1)if not found
PrivateFunctionPixelSearch(ByVal rec AsRectangle,ByVal colorToFind AsInteger)AsPoint'Right, there is one pixel on the form in the correct color.' find the location in the bitmap
Dim x AsInteger=-1Dim y AsInteger=-1'First grab the screen
Using bm AsNewBitmap(rec.Width, rec.Height)'Copy a portion of the screen.Dim topleft AsPoint=NewPoint(rec.X, rec.Y)Using g AsGraphics=Graphics.FromImage(bm)
g.CopyFromScreen(topleft,NewPoint, rec.Size)EndUsing' lock the bits
Dim bmd AsBitmapData= bm.LockBits(NewRectangle(0,0, bm.Width, bm.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb)' copy all pixels out into an integer array.Dim pixels(bm.Width* bm.Height-1)AsIntegerMarshal.Copy(bmd.Scan0, pixels,0, pixels.Length)' find the color
For i AsInteger=0To pixels.Length-1If pixels(i)= colorToFind Then
x = i Mod bm.Width
y = i \ bm.WidthExitForEndIfNext' unlock bm
bm.UnlockBits(bmd)EndUsingReturnNewPoint(x, y)EndFunctionEndClassFriendClassNativeMethods<DllImport("user32.dll",SetLastError:=True)> _
PrivateSharedFunctionSendInput( _
ByVal cInputs AsInteger, _
ByVal pInputs()As INPUT, _
ByVal cbSize AsInteger)AsIntegerEndFunctionPrivateStructure INPUT
Public dwType AsIntegerPublic mi As MOUSEINPUT
EndStructure<StructLayout(LayoutKind.Sequential)> _
PrivateStructure MOUSEINPUT
Public dx AsIntegerPublic dy AsIntegerPublic mouseData AsUIntegerPublic dwFlags AsUIntegerPublic time AsUIntegerPublic dwExtraInfo AsIntPtrEndStructurePrivateConst INPUT_MOUSE AsInteger=0PrivateConst MOUSEEVENTF_LEFTDOWN AsInteger=&H2
PrivateConst MOUSEEVENTF_LEFTUP AsInteger=&H4
PrivateConst MOUSEEVENTF_MIDDLEDOWN AsInteger=&H20
PrivateConst MOUSEEVENTF_MIDDLEUP AsInteger=&H40
PrivateConst MOUSEEVENTF_MOVE AsInteger=&H1
PrivateConst MOUSEEVENTF_ABSOLUTE AsInteger=&H8000
PrivateConst MOUSEEVENTF_RIGHTDOWN AsInteger=&H8
PrivateConst MOUSEEVENTF_RIGHTUP AsInteger=&H10
' not accurate!PublicSharedSubMoveMouseAndClick(ByVal x AsInteger,ByVal y AsInteger)Dim inputs(2)As INPUT
For i AsInteger=0To inputs.Length-1
inputs(i).dwType = INPUT_MOUSE
Next
inputs(0).mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE
' oh, this isn't accurate. I've abandoned it and used Cursor.Position instead.
inputs(0).mi.dx =Convert.ToInt32(Math.Ceiling(x *65535/Screen.PrimaryScreen.Bounds.Width))
inputs(0).mi.dy =Convert.ToInt32(Math.Ceiling(y *65535/Screen.PrimaryScreen.Bounds.Height))
inputs(1).mi.dwFlags = MOUSEEVENTF_LEFTDOWN
inputs(2).mi.dwFlags = MOUSEEVENTF_LEFTUP
Dim cbSize AsInteger=Marshal.SizeOf(inputs(0))Dim result AsInteger=SendInput(inputs.Length, inputs, cbSize)If result =0ThenThrowNewSystem.ComponentModel.Win32ExceptionEndIfEndSubPublicSharedSubMouseClick()Dim inputs(1)As INPUT
For i AsInteger=0To inputs.Length-1
inputs(i).dwType = INPUT_MOUSE
Next
inputs(0).mi.dwFlags = MOUSEEVENTF_LEFTDOWN
inputs(1).mi.dwFlags = MOUSEEVENTF_LEFTUP
Dim cbSize AsInteger=Marshal.SizeOf(inputs(0))Dim result AsInteger=SendInput(inputs.Length, inputs, cbSize)If result =0ThenThrowNewSystem.ComponentModel.Win32ExceptionEndIfEndSubEndClass
No todos los codigos son correctos porque el original lo perdi, así que deje todos los que probamos para que tu mismo apliques el codigo de forma que te sirva.
Puede publicar ahora y registrarse más tarde.
Si tiene una cuenta, iniciar sesión para publicar con su cuenta.
Nota: Tu publicación requerirá la aprobación de un moderador antes de que sea visible.
Hemos colocado cookies en su dispositivo para ayudar a mejorar este sitio web. Puede ajustar la configuración de sus cookies, de lo contrario asumiremos que está de acuerdo en continuar.
SOURCE CODE
[ PixelAim ]
Información:
+ Autor: @Zabb, Eqeza, SensacFa, Jeskqq
+ Desarrollador: @Zabb & EqeZa
+ Versión: 0.6
+ Compatibilidad: Visual Studio 2010+
+ Lenguaje: VB.NET
Códigos:
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
No todos los codigos son correctos porque el original lo perdi, así que deje todos los que probamos para que tu mismo apliques el codigo de forma que te sirva.