Question[Help]Win Api / Mouse to color

Posts 1–14 of 14 · Page 1 of 1
[Help]Win Api / Mouse to color
hey i saw this cool thing online it was a thing were you could put you mouse over a color then you could save the color and push go and the mouse would automaticly go to that color anytime it saw it
Sure, If you want it
(Not mine, Standard MSDN)

Quote Originally Posted by Dan From MSDN
Dim bitmap As Bitmap = New Bitmap("myBitmap.bmp")
Dim x As Integer
Dim y As Integer
For x = 0 To bitmap.Width
For y = 0 To bitmap.Height
If bitmap.GetPixel(x, y) = Color.AliceBlue Then
'this is where you'd move the mouse to, and possibly exit the loop
End If
Next
Next

inside the loop, you can call this function :
move_mouse_to_location(x,y)

this is where it gets really tricky. You will need to call the windows API to get the mouse to move. Add this to the top of your code outside of any function definitions:
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

Private Const MOUSEEVENTF_MOVE As Integer = &H1 ' mouse move

Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2 ' left button down

Private Const MOUSEEVENTF_LEFTUP As Integer = &H4 ' left button up

Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8 ' right button down

Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10 ' right button up

Private Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20 ' middle button down

Private Const MOUSEEVENTF_MIDDLEUP As Integer = &H40 ' middle button up

Private Const MOUSEEVENTF_WHEEL As Integer = &H800 ' wheel button rolled

Private Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000 ' absolute move

Private slowmode As Boolean = False

this is the constants and the declaration allowing you to call the windows API.

then you can use this subroutine to move the mouse to the desired location:
Private Function move_mouse_to_location(ByVal x As Integer, ByVal y As Integer)

Dim desired As Point

Dim current As Point

Dim move As Point

Dim resolution As Rectangle

resolution = My.Computer.Screen.Bounds



If x > 0 And x < resolution.Right Then 'the value is legimate

desired.X = x

Else

MsgBox("The value for the mouse that you passed is out of bounds. X must be a value between 0 and " & resolution.Right & " You passed the value " & x)

desired.X = 0

End If

If y > 0 And y < resolution.Bottom Then

desired.Y = y

Else

MsgBox("The value for the mouse that you passed is out of bounds. Y must be a value between 0 and " & resolution.Bottom & " You passed the value " & y)

desired.Y = 0

End If

move = desired - current

current.X = MousePosition.X

current.Y = MousePosition.Y

While current.Y > desired.Y

mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, 0)

current.Y = MousePosition.Y

End While

While current.Y < desired.Y

mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, 0)

current.Y = MousePosition.Y

End While

While current.X > desired.X

mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0)

current.X = MousePosition.X

End While

While current.X < desired.X

mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0)

current.X = MousePosition.X

End While







Return True

End Function
what will this do
.........................................
This Takes a Color, and Uses Windows API to move the mouse location to the same color or chosen color.

You will need a few modifications. But not alot.

okay but hows this code i only get one error plz help

[php]
Imports System.Drawing

Public Class Form1
Inherits System.Windows.Forms.Form

Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents Start As System.Windows.Forms.Button
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents lfred As System.Windows.Forms.Label
Friend WithEvents lfgreen As System.Windows.Forms.Label
Friend WithEvents lfblue As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents Label8 As System.Windows.Forms.Label
Friend WithEvents Label9 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtInterval As System.Windows.Forms.TextBox
Friend WithEvents Label10 As System.Windows.Forms.Label
Friend WithEvents Label11 As System.Windows.Forms.Label
Friend WithEvents txtX As System.Windows.Forms.TextBox
Friend WithEvents txtY As System.Windows.Forms.TextBox
Friend WithEvents lsred As System.Windows.Forms.Label
Friend WithEvents lsgreen As System.Windows.Forms.Label
Friend WithEvents lsblue As System.Windows.Forms.Label
Friend WithEvents PickColor As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
Dim WithEvents Tmr As New Timer
Dim Bmp As New Bitmap(1, 1)
Dim G As Graphics = Graphics.FromImage(Bmp)
Dim X As Integer
Dim Y As Integer
Dim Red As Integer
Dim Green As Integer
Dim Blue As Integer
Dim Px As System.Drawing.Color

Const Tolerance As Integer = 10

Sub New()
MyBase.New()
Me.components = New System.ComponentModel.Container
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Start = New System.Windows.Forms.Button
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
Me.Label1 = New System.Windows.Forms.Label
Me.lfred = New System.Windows.Forms.Label
Me.lfgreen = New System.Windows.Forms.Label
Me.lfblue = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.Label8 = New System.Windows.Forms.Label
Me.Label9 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.txtInterval = New System.Windows.Forms.TextBox
Me.Label10 = New System.Windows.Forms.Label
Me.Label11 = New System.Windows.Forms.Label
Me.txtX = New System.Windows.Forms.TextBox
Me.txtY = New System.Windows.Forms.TextBox
Me.lsred = New System.Windows.Forms.Label
Me.lsgreen = New System.Windows.Forms.Label
Me.lsblue = New System.Windows.Forms.Label
Me.PickColor = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Start
'
Me.Start.Location = New System.Drawing.Point(15, 203)
Me.Start.Name = "Start"
Me.Start.Size = New System.Drawing.Size(265, 42)
Me.Start.TabIndex = 0
Me.Start.Text = "Start"
Me.Start.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 39)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 17)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Found"
'
'lfred
'
Me.lfred.AutoSize = True
Me.lfred.Location = New System.Drawing.Point(124, 39)
Me.lfred.Name = "lfred"
Me.lfred.Size = New System.Drawing.Size(24, 17)
Me.lfred.TabIndex = 4
Me.lfred.Text = "FF"
'
'lfgreen
'
Me.lfgreen.AutoSize = True
Me.lfgreen.Location = New System.Drawing.Point(185, 39)
Me.lfgreen.Name = "lfgreen"
Me.lfgreen.Size = New System.Drawing.Size(24, 17)
Me.lfgreen.TabIndex = 5
Me.lfgreen.Text = "FF"
'
'lfblue
'
Me.lfblue.AutoSize = True
Me.lfblue.Location = New System.Drawing.Point(242, 39)
Me.lfblue.Name = "lfblue"
Me.lfblue.Size = New System.Drawing.Size(24, 17)
Me.lfblue.TabIndex = 6
Me.lfblue.Text = "FF"
'
'Label7
'
Me.Label7.AutoSize = True
Me.Label7.Location = New System.Drawing.Point(120, 18)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(34, 17)
Me.Label7.TabIndex = 7
Me.Label7.Text = "Red"
'
'Label8
'
Me.Label8.AutoSize = True
Me.Label8.Location = New System.Drawing.Point(177, 18)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(48, 17)
Me.Label8.TabIndex = 8
Me.Label8.Text = "Green"
'
'Label9
'
Me.Label9.AutoSize = True
Me.Label9.Location = New System.Drawing.Point(237, 18)
Me.Label9.Name = "Label9"
Me.Label9.Size = New System.Drawing.Size(36, 17)
Me.Label9.TabIndex = 9
Me.Label9.Text = "Blue"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(12, 66)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(83, 17)
Me.Label2.TabIndex = 10
Me.Label2.Text = "Looking For"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(14, 96)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(54, 17)
Me.Label3.TabIndex = 14
Me.Label3.Text = "Interval"
'
'txtInterval
'
Me.txtInterval.Location = New System.Drawing.Point(127, 96)
Me.txtInterval.Name = "txtInterval"
Me.txtInterval.Size = New System.Drawing.Size(77, 22)
Me.txtInterval.TabIndex = 15
Me.txtInterval.Text = "500"
'
'Label10
'
Me.Label10.AutoSize = True
Me.Label10.Location = New System.Drawing.Point(25, 131)
Me.Label10.Name = "Label10"
Me.Label10.Size = New System.Drawing.Size(17, 17)
Me.Label10.TabIndex = 16
Me.Label10.Text = "X"
'
'Label11
'
Me.Label11.AutoSize = True
Me.Label11.Location = New System.Drawing.Point(149, 131)
Me.Label11.Name = "Label11"
Me.Label11.Size = New System.Drawing.Size(17, 17)
Me.Label11.TabIndex = 17
Me.Label11.Text = "Y"
'
'txtX
'
Me.txtX.Location = New System.Drawing.Point(48, 128)
Me.txtX.Name = "txtX"
Me.txtX.Size = New System.Drawing.Size(77, 22)
Me.txtX.TabIndex = 18
Me.txtX.Text = "500"
'
'txtY
'
Me.txtY.Location = New System.Drawing.Point(180, 128)
Me.txtY.Name = "txtY"
Me.txtY.Size = New System.Drawing.Size(92, 22)
Me.txtY.TabIndex = 19
Me.txtY.Text = "500"
'
'lsred
'
Me.lsred.AutoSize = True
Me.lsred.Location = New System.Drawing.Point(124, 66)
Me.lsred.Name = "lsred"
Me.lsred.Size = New System.Drawing.Size(24, 17)
Me.lsred.TabIndex = 20
Me.lsred.Text = "FF"
'
'lsgreen
'
Me.lsgreen.AutoSize = True
Me.lsgreen.Location = New System.Drawing.Point(185, 66)
Me.lsgreen.Name = "lsgreen"
Me.lsgreen.Size = New System.Drawing.Size(24, 17)
Me.lsgreen.TabIndex = 21
Me.lsgreen.Text = "FF"
'
'lsblue
'
Me.lsblue.AutoSize = True
Me.lsblue.Location = New System.Drawing.Point(242, 66)
Me.lsblue.Name = "lsblue"
Me.lsblue.Size = New System.Drawing.Size(24, 17)
Me.lsblue.TabIndex = 22
Me.lsblue.Text = "FF"
'
'PickColor
'
Me.PickColor.Location = New System.Drawing.Point(15, 156)
Me.PickColor.Name = "PickColor"
Me.PickColor.Size = New System.Drawing.Size(265, 42)
Me.PickColor.TabIndex = 23
Me.PickColor.Text = "Pick Color"
Me.PickColor.UseVisualStyleBackColor = True
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(20, 254)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(252, 29)
Me.Button1.TabIndex = 24
Me.Button1.Text = "Go to Position"
Me.Button1.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 296)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PickColor)
Me.Controls.Add(Me.lsblue)
Me.Controls.Add(Me.lsgreen)
Me.Controls.Add(Me.lsred)
Me.Controls.Add(Me.txtY)
Me.Controls.Add(Me.txtX)
Me.Controls.Add(Me.Label11)
Me.Controls.Add(Me.Label10)
Me.Controls.Add(Me.txtInterval)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label9)
Me.Controls.Add(Me.Label8)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.lfblue)
Me.Controls.Add(Me.lfgreen)
Me.Controls.Add(Me.lfred)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Start)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick

'G.CopyFromScreen(MousePosition, New Point(0, 0), Bmp.Size)
G.CopyFromScreen(New Point(X, Y), New Point(0, 0), Bmp.Size)
Px = Bmp.GetPixel(0, 0)
lfred.Text = Hex(Px.R)
lfgreen.Text = Hex(Px.G)
lfblue.Text = Hex(Px.B)

Me.Text = MousePosition().ToString
If (Px.R >= Red - Tolerance) And (Px.R <= Red + Tolerance) Then
If (Px.G >= Green - Tolerance) And (Px.G <= Green + Tolerance) Then
If (Px.B >= Blue - Tolerance) And (Px.B <= Blue + Tolerance) Then
SetCursorPos(X, Y)
Lef***ick()
End If
End If
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
X = txtX.Text
Y = txtY.Text
Tmr.Interval = txtInterval.Text
End Sub

Public Sub Lef***ick()
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

Public Sub Right_Click()
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
End Sub

Public Sub Middle_Click()
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
End Sub

Private Sub PickColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PickColor.Click
If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Red = ColorDialog1.Color.R
Blue = ColorDialog1.Color.B
Green = ColorDialog1.Color.G
lsred.Text = Hex(Red)
lsblue.Text = Hex(Blue)
lsgreen.Text = Hex(Green)
End If
End Sub

Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
X = txtX.Text
Y = txtY.Text
Tmr.Interval = txtInterval.Text
Tmr.Enabled = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SetCursorPos(X, Y)
End Sub
End Class
[/php]
What is the error?......
i forgot can you just check it out
1. just [code] tags
2. POST the error results we can't help you else

3. nextgen answer my pm xD
Warning 1 'Public Sub New()' in designer-generated type 'WindowsApplication1.Form1' should call InitializeComponent method.
So in

Sub New()

The Top Part should be

[php]
Sub New()

MyBase.New()
InitializeComponent()

[/php]

InitializeComponent()
Should be placed under the existing myBase.new in the Sub New()



Side Note - this code looks like it was part of a module once - doesn't matter, just saying, it probably should have been a module

i tested it sucked can you tell me you code again
FFS Go make a web browser or something, don't jump directly into the complicated stuff.
@ lolland, I would agree, Start small, but I am assuming this is needed for something , if it is "just because" then maybe learning windows API would be a good idea
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?