שתף באמצעות


Move Mouse Cursor to Specific ICON based on Saved Image..

Question

Saturday, February 27, 2016 8:40 AM | 1 vote

I have taken the Screen Shot of the ICON

I need to Move the Mouse Cursor and get the Screen Location. I am Fine with Image Recognition or Text Recognition.

I just Need the ICON Position in the Current Screen.

All replies (19)

Saturday, February 27, 2016 6:29 PM ✅Answered

Click an Icon Via Image can never be malicious.....

I am doing 1 project on this where i could not handle Via windows handler, ..Just last thing Iron, How do i Right Click i.e Mouse Right Click..

 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

 mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

Tried this but failed

 I disagree with that,  the image could be of anything within the screen and could be something the user does not want to click on.

 First you need the correct VB.Net signature for the mouse_event Api function.  You can find an example of using this function that i posted in the link below.

How do i make the mouse click automatically?

 PS - Don`t forget to mark the post or posts that answer your question as the answers.   8)

If you say it can`t be done then i`ll try it


Saturday, February 27, 2016 1:41 PM | 2 votes

Here is an example that finds an image on the screen. There may be an easier way to find just the icon on the desktop. But this finds any image on the screen. There may be a faster way to do this too.  :)

This finds the image and gives the screen cords. Then tests the value with the upper left inside corner of the form. Move the form around the screen and test it.

Option Strict On

Public Class Form7
    Private bmpTarget As Bitmap = New Bitmap("c:\bitmaps\rusty face.png")
    Private bmpScreen As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
    Private sw As New Stopwatch

    Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = "Find Rusty"
        Label1.Text = "Click Find to Start"
        Label2.Text = ""

        BackgroundImage = bmpTarget
        BackgroundImageLayout = ImageLayout.None
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'search with fuzzy getpixel 
        CaptureScreen()

        sw.Reset()
        sw.Start()

        Dim clrScreen, clrTarget, clrUL, clrUR, clrLL, clrLR As Color
        Dim Found As Boolean

        clrUL = bmpTarget.GetPixel(0, 0)
        clrUR = bmpTarget.GetPixel(bmpTarget.Width - 1, 0)
        clrLL = bmpTarget.GetPixel(0, bmpTarget.Height - 1)
        clrLR = bmpTarget.GetPixel(bmpTarget.Width - 1, bmpTarget.Height - 1)

        For x = 0 To bmpScreen.Width - bmpTarget.Width
            For y = 0 To bmpScreen.Height - bmpTarget.Height
                clrScreen = bmpScreen.GetPixel(x, y)
                If clrScreen = clrUL Then
                    'found the upperleft pixel check upper right
                    clrScreen = bmpScreen.GetPixel(x + bmpTarget.Width - 1, y)
                    If clrScreen = clrUR Then
                        clrScreen = bmpScreen.GetPixel(x, y + bmpTarget.Height - 1)
                        If clrScreen = clrLL Then
                            clrScreen = bmpScreen.GetPixel(x + bmpTarget.Width - 1, y + bmpTarget.Height - 1)
                            If clrScreen = clrLR Then
                                'found all four courners
                                'check the diagonal
                                Dim w1 As Integer
                                Found = True
                                If bmpTarget.Width > bmpTarget.Height Then w1 = bmpTarget.Height Else w1 = bmpTarget.Width
                                For x1 = 1 To w1 - 2 Step 5
                                    clrTarget = bmpTarget.GetPixel(x1, x1)
                                    clrScreen = bmpScreen.GetPixel(x + x1, y + x1)
                                    If clrTarget <> clrScreen Then
                                        Found = False
                                        Exit For
                                    End If
                                Next
                                If Found Then
                                    ShowResults(x, y)
                                    Exit Sub
                                End If
                            End If
                        End If
                    End If
                End If
            Next
        Next

        ShowResults(-1, 0)

    End Sub
    Private Sub CaptureScreen()

        Using g As Graphics = Graphics.FromImage(bmpScreen)
            g.CopyFromScreen(0, 0, 0, 0, bmpScreen.Size)
        End Using

        Label1.Text = "Searching..."
        Label2.Text = ""
        Me.Refresh()
    End Sub

    Private Sub ShowResults(x As Integer, y As Integer)
        sw.Stop()

        If x = -1 Then
            Label1.Text = "Image not Found"
        Else
            Label1.Text = "Found: (" & x.ToString & ", " & y.ToString & ")  Time: " & sw.ElapsedMilliseconds & " ms"
            Dim p1 As Point = New Point(0, 0)
            p1 = Me.PointToScreen(p1)
            Label2.Text = "Test: (" & p1.X.ToString & ", " & p1.Y.ToString & ")"
        End If
    End Sub
End Class

Saturday, February 27, 2016 3:57 PM | 3 votes

 What do you need to find the image on the screen and move the cursor to it for?  If you need to find an image of a Shortcut or perhaps a control on the screen somewhere and execute the program or click the control,  then there are better (more reliable) ways to do that.

 If you are just interested in finding images on the screen,  then below is an example that uses LockBits to find an image location on the screen and move the cursor to the center of the image (if found).  Lockbits is usually faster than the GetPixel method when processing larger image data such as processing a full screen shot to find a matching image in it.

 The speed at which either method will find the image depends on a few things, such as the location of the image on the screen,  the background color or image you use for the desktop,  the size of the desktop resolution,  the size of the image to find,  and the speed of the computer itself.

 If you are loading the screen shot image of the Icon that you want to find from a file on the hard drive,  it should be saved to the hard drive with a lossless format such as a Png image format.  A Jpg for example will not work because when it is saved,  it will loose some of its original pixel color data and would never match the original image on the desktop.

 For this example,  i captured an image of this Icon on my screen and saved it as a 32bpp Png image on the hard drive.

 Then i used this code in the Form with 1 Button added to the Form.

Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class Form1
    Private ImgToFind As New Bitmap("C:\TestFolder\MyIconScreenShot.png")

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rect As Rectangle = FindImageOnScreen(ImgToFind, False)
        If rect <> Rectangle.Empty Then
            Dim cntr As Point = New Point(rect.X + CInt(rect.Width / 2), rect.Y + CInt(rect.Height / 2))
            Cursor.Position = cntr
        Else
            MessageBox.Show("Image not found")
        End If
    End Sub

    ''' <summary>Finds a matching image on the screen.</summary>
    ''' <param name="bmpMatch">The image to find on the screen.</param>
    ''' <param name="ExactMatch">True finds an exact match (slowerer on large images). False finds a close match (faster on large images).</param>
    ''' <returns>Returns a Rectangle of the found image in sceen coordinates.</returns>
    Private Function FindImageOnScreen(ByVal bmpMatch As Bitmap, ByVal ExactMatch As Boolean) As Rectangle
        Dim ScreenBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        Using g As Graphics = Graphics.FromImage(ScreenBmp)
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.Location, Point.Empty, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
        End Using

        Dim ImgBmd As BitmapData = bmpMatch.LockBits(New Rectangle(0, 0, bmpMatch.Width, bmpMatch.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
        Dim ScreenBmd As BitmapData = ScreenBmp.LockBits(Screen.PrimaryScreen.Bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)

        Dim ImgByts((Math.Abs(ImgBmd.Stride) * bmpMatch.Height) - 1) As Byte
        Dim ScreenByts((Math.Abs(ScreenBmd.Stride) * ScreenBmp.Height) - 1) As Byte

        Marshal.Copy(ImgBmd.Scan0, ImgByts, 0, ImgByts.Length)
        Marshal.Copy(ScreenBmd.Scan0, ScreenByts, 0, ScreenByts.Length)

        Dim FoundMatch As Boolean = False
        Dim rct As Rectangle = Rectangle.Empty
        Dim sindx, iindx As Integer
        Dim spc, ipc As Integer

        Dim skpx As Integer = CInt((bmpMatch.Width - 1) / 10)
        If skpx < 1 Or ExactMatch Then skpx = 1
        Dim skpy As Integer = CInt((bmpMatch.Height - 1) / 10)
        If skpy < 1 Or ExactMatch Then skpy = 1

        For si As Integer = 0 To ScreenByts.Length - 1 Step 3
            FoundMatch = True
            For iy As Integer = 0 To ImgBmd.Height - 1 Step skpy
                For ix As Integer = 0 To ImgBmd.Width - 1 Step skpx
                    sindx = (iy * ScreenBmd.Stride) + (ix * 3) + si
                    iindx = (iy * ImgBmd.Stride) + (ix * 3)
                    spc = Color.FromArgb(ScreenByts(sindx + 2), ScreenByts(sindx + 1), ScreenByts(sindx)).ToArgb
                    ipc = Color.FromArgb(ImgByts(iindx + 2), ImgByts(iindx + 1), ImgByts(iindx)).ToArgb
                    If spc <> ipc Then
                        FoundMatch = False
                        iy = ImgBmd.Height - 1
                        ix = ImgBmd.Width - 1
                    End If
                Next
            Next
            If FoundMatch Then
                Dim r As Double = si / (ScreenBmp.Width * 3)
                Dim c As Double = ScreenBmp.Width * (r Mod 1)
                If r Mod 1 >= 0.5 Then r -= 1
                rct.X = CInt(c)
                rct.Y = CInt(r)
                rct.Width = bmpMatch.Width
                rct.Height = bmpMatch.Height
                Exit For
            End If
        Next
        bmpMatch.UnlockBits(ImgBmd)
        ScreenBmp.UnlockBits(ScreenBmd)
        ScreenBmp.Dispose()
        Return rct
    End Function
End Class

 

 As you can see,  it finds the image on the screen and moves the cursor to the center of the image on the screen.

If you say it can`t be done then i`ll try it


Saturday, February 27, 2016 4:14 PM

Told you there was a faster way.

:)


Saturday, February 27, 2016 4:17 PM

Told you there was a faster way.

:)

PS Razerz, how does one loop through the icons on the desktop? Isnt there just a loop the desktop or something?


Saturday, February 27, 2016 4:26 PM

 What do you need to find the image on the screen and move the cursor to it for?  If you need to find an image of a Shortcut or perhaps a control on the screen somewhere and execute the program or click the control,  then there are better (more reliable) ways to do that.

 If you are just interested in finding images on the screen,  then below is an example that uses LockBits to find an image location on the screen and move the cursor to the center of the image (if found).  Lockbits is usually faster than the GetPixel method when processing larger image data such as processing a full screen shot to find a matching image in it.

 The speed at which either method will find the image depends on a few things, such as the location of the image on the screen,  the background color or image you use for the desktop,  the size of the desktop resolution,  the size of the image to find,  and the speed of the computer itself.

 If you are loading the screen shot image of the Icon that you want to find from a file on the hard drive,  it should be saved to the hard drive with a lossless format such as a Png image format.  A Jpg for example will not work because when it is saved,  it will loose some of its original pixel color data and would never match the original image on the desktop.

 For this example,  i captured an image of this Icon on my screen and saved it as a 32bpp Png image on the hard drive.

 Then i used this code in the Form with 1 Button added to the Form.

Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class Form1
    Private ImgToFind As New Bitmap("C:\TestFolder\MyIconScreenShot.png")

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rect As Rectangle = FindImageOnScreen(ImgToFind, False)
        If rect <> Rectangle.Empty Then
            Dim cntr As Point = New Point(rect.X + CInt(rect.Width / 2), rect.Y + CInt(rect.Height / 2))
            Cursor.Position = cntr
        Else
            MessageBox.Show("Image not found")
        End If
    End Sub

    ''' <summary>Finds a matching image on the screen.</summary>
    ''' <param name="bmpMatch">The image to find on the screen.</param>
    ''' <param name="ExactMatch">True finds an exact match (slowerer on large images). False finds a close match (faster on large images).</param>
    ''' <returns>Returns a Rectangle of the found image in sceen coordinates.</returns>
    Private Function FindImageOnScreen(ByVal bmpMatch As Bitmap, ByVal ExactMatch As Boolean) As Rectangle
        Dim ScreenBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        Using g As Graphics = Graphics.FromImage(ScreenBmp)
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.Location, Point.Empty, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
        End Using

        Dim ImgBmd As BitmapData = bmpMatch.LockBits(New Rectangle(0, 0, bmpMatch.Width, bmpMatch.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
        Dim ScreenBmd As BitmapData = ScreenBmp.LockBits(Screen.PrimaryScreen.Bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)

        Dim ImgByts((Math.Abs(ImgBmd.Stride) * bmpMatch.Height) - 1) As Byte
        Dim ScreenByts((Math.Abs(ScreenBmd.Stride) * ScreenBmp.Height) - 1) As Byte

        Marshal.Copy(ImgBmd.Scan0, ImgByts, 0, ImgByts.Length)
        Marshal.Copy(ScreenBmd.Scan0, ScreenByts, 0, ScreenByts.Length)

        Dim FoundMatch As Boolean = False
        Dim rct As Rectangle = Rectangle.Empty
        Dim sindx, iindx As Integer
        Dim spc, ipc As Integer

        Dim skpx As Integer = CInt((bmpMatch.Width - 1) / 10)
        If skpx < 1 Or ExactMatch Then skpx = 1
        Dim skpy As Integer = CInt((bmpMatch.Height - 1) / 10)
        If skpy < 1 Or ExactMatch Then skpy = 1

        For si As Integer = 0 To ScreenByts.Length - 1 Step 3
            FoundMatch = True
            For iy As Integer = 0 To ImgBmd.Height - 1 Step skpy
                For ix As Integer = 0 To ImgBmd.Width - 1 Step skpx
                    sindx = (iy * ScreenBmd.Stride) + (ix * 3) + si
                    iindx = (iy * ImgBmd.Stride) + (ix * 3)
                    spc = Color.FromArgb(ScreenByts(sindx + 2), ScreenByts(sindx + 1), ScreenByts(sindx)).ToArgb
                    ipc = Color.FromArgb(ImgByts(iindx + 2), ImgByts(iindx + 1), ImgByts(iindx)).ToArgb
                    If spc <> ipc Then
                        FoundMatch = False
                        iy = ImgBmd.Height - 1
                        ix = ImgBmd.Width - 1
                    End If
                Next
            Next
            If FoundMatch Then
                Dim r As Double = si / (ScreenBmp.Width * 3)
                Dim c As Double = ScreenBmp.Width * (r Mod 1)
                If r Mod 1 >= 0.5 Then r -= 1
                rct.X = CInt(c)
                rct.Y = CInt(r)
                rct.Width = bmpMatch.Width
                rct.Height = bmpMatch.Height
                Exit For
            End If
        Next
        bmpMatch.UnlockBits(ImgBmd)
        ScreenBmp.UnlockBits(ScreenBmd)
        ScreenBmp.Dispose()
        Return rct
    End Function
End Class

 

 As you can see,  it finds the image on the screen and moves the cursor to the center of the image on the screen.

If you say it can`t be done then i`ll try it

It Worked for Unique Image, But failed to Identify the below Screenshot which has 2 excel file in desktop, where i took the Screen Shot of One.

Current Desktop Screen Shot.

Tried Taking Screenshot of IDGO.xls, Failed to find the Image.


Saturday, February 27, 2016 4:35 PM

ID perhaps you should use this instead. But which duplicate one do you want to find?

You are not being clear on what you want to do. You should answer Razerz questions.


Saturday, February 27, 2016 4:47 PM

Told you there was a faster way.

:)

PS Razerz, how does one loop through the icons on the desktop? Isnt there just a loop the desktop or something?

 Well,  it would depend on what you wanted to do with the Shortcut or Icon.  If you wanted to execute a program from a desktop shortcut it would be easier to just find the file in the Desktop Folder by its name. Then you could use the Process class to start the program from the full path of the file.  For example:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Dim shrtcut As String = IO.Directory.GetFiles(DesktopFolder, "Gif Creator 2.lnk").FirstOrDefault
        If shrtcut <> Nothing Then Process.Start(shrtcut)
    End Sub

 

 Being the shortcuts/icons on the desktop are actually listview items there are other ways to iterate through an external application`s ListView`s items using a bunch of Api functions and get the rectangle bounds of the listview item on the screen.  That gets a bit involved though and would only be practical if you actually wanted to change the listview items of the desktop in some way or maybe find the selected icon/ListView item(s) on the listview.

 

If you say it can`t be done then i`ll try it


Saturday, February 27, 2016 4:48 PM

ID perhaps you should use this instead. But which duplicate one do you want to find?

You are not being clear on what you want to do. You should answer Razerz questions.

Tommy I am Trying your Code Now,...

1. But which duplicate one do you want to find?

Ans : I have 2 Excel Sheet file in desktop, a. IDGO.xls b.Check.xls,

Problem is both have same Icon .....I need to Right Click on IDGO.xls using Image


Saturday, February 27, 2016 4:52 PM

I want to solve this Problem using Image , not Via Environment.GetFolderPath,

IronRazer & Tom both of your Code is 90% working....

Problem Statement is ....

1. If i have 2 Similar Images but Text is different ...its causing Problem.

2. Just Need to Right Click on the Icon Once Image Found


Saturday, February 27, 2016 5:06 PM

I want to solve this Problem using Image , not Via Environment.GetFolderPath,

IronRazer & Tom both of your Code is 90% working....

Problem Statement is ....

1. If i have 2 Similar Images but Text is different ...its causing Problem.

2. Just Need to Right Click on the Icon Once Image Found

 Did you try changing the second parameter of this line in my example to True?  If it is set True it will find an exact match by checking that every pixel of the image matches.  If it is set to False it will only check every 10 pixes in a grid pattern of pixels in the image to find a close match.

Dim rect As Rectangle = FindImageOnScreen(ImgToFind, False) 'try setting to True to find exact match

** Why do you need to right click on it ?**

 

If you say it can`t be done then i`ll try it


Saturday, February 27, 2016 5:30 PM

I want to solve this Problem using Image , not Via Environment.GetFolderPath,

IronRazer & Tom both of your Code is 90% working....

Problem Statement is ....

1. If i have 2 Similar Images but Text is different ...its causing Problem.

2. Just Need to Right Click on the Icon Once Image Found

 Did you try changing the second parameter of this line in my example to True?  If it is set True it will find an exact match by checking that every pixel of the image matches.  If it is set to False it will only check every 10 pixes in a grid pattern of pixels in the image to find a close match.

Dim rect As Rectangle = FindImageOnScreen(ImgToFind, False) 'try setting to True to find exact match

** Why do you need to right click on it ?**

 

If you say it can`t be done then i`ll try it

Yep...Its Working, but there is a Lag while Running and 2nd Time If i Click the Button it Fails.


Saturday, February 27, 2016 5:30 PM | 1 vote

I want to solve this Problem using Image , not Via Environment.GetFolderPath,

IronRazer & Tom both of your Code is 90% working....

Problem Statement is ....

1. If i have 2 Similar Images but Text is different ...its causing Problem.

2. Just Need to Right Click on the Icon Once Image Found

You need to put on your thinking cap and look at the code we give you and understand it. What my example does is "fuzzy" so it works where it works. Its only checking some of the pixels, the corners and a diagonal. 

Razerz example will do an exact match. Of course that is a bit slower (exact lockbits vs fuzzy lockbits) but it sounds like speed is not an issue?

It still seems there is a better way. Just look for the text of the icon in the desktop icons? Again if you tell Razerz why you are doing this I am sure he can give you a good way.

PS Razerz and I had fun with this find Waldo problem a while ago and we both know the result. That is why I knew he had a better way of finding the image than GetPixel as I showed in my example. Using lockbits is faster if done right.

It does get to the speed vs accuracy and what your exact needs are. We dont know what you are doing so we just shoot out some ideas. You are the project manager and final decision maker and know what is going on person and debugger. Not us. You take the bits and pieces we give you and bend and shape and join them into your project code sculpture. Have a question? Feel free to ask.


Saturday, February 27, 2016 6:03 PM

"Yep...Its Working, but there is a Lag while Running and 2nd Time If i Click the Button it Fails."

 If you read my post you will see i said,  the speed at which these methods can find a matching image depends on several things.  I gave a list of the things that will effect the speed.  Finding an image like this will not be a fast process in any which way.

 If it works the first time you press the button but not the second,  then there must be something different the second time you press the button.  If you have selected the icon,  perhaps there is a focus rectangle around the icon on the screen.  There is something that had to change from the first to the second time,  you just need to figure out what because we can`t see your screen or what exactly you are doing.

 I agree with Tom that there is probably much better and a lot more reliable ways to do what it is you need to do.  However,  you don`t seem to want to answer my questions or explain in detail what you are doing.  This is starting to make me feel like this may have a malicious purpose which i won`t help with.

If you say it can`t be done then i`ll try it


Saturday, February 27, 2016 6:11 PM

Click an Icon Via Image can never be malicious.....

I am doing 1 project on this where i could not handle Via windows handler, ..Just last thing Iron, How do i Right Click i.e Mouse Right Click..

 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

 mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

Tried this but failed


Saturday, February 27, 2016 6:12 PM

"Yep...Its Working, but there is a Lag while Running and 2nd Time If i Click the Button it Fails."

 If you read my post you will see i said,  the speed at which these methods can find a matching image depends on several things.  I gave a list of the things that will effect the speed.  Finding an image like this will not be a fast process in any which way.

 If it works the first time you press the button but not the second,  then there must be something different the second time you press the button.  If you have selected the icon,  perhaps there is a focus rectangle around the icon on the screen.  There is something that had to change from the first to the second time,  you just need to figure out what because we can`t see your screen or what exactly you are doing.

 I agree with Tom that there is probably much better and a lot more reliable ways to do what it is you need to do.  However,  you don`t seem to want to answer my questions or explain in detail what you are doing.  This is starting to make me feel like this may have a malicious purpose which i won`t help with.

If you say it can`t be done then i`ll try it

Click an Icon Via Image can never be malicious.....

I am doing 1 project on this where i could not handle Via windows handler, ..Just last thing Iron, How do i Right Click i.e Mouse Right Click..

 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

 mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

Tried this but failed


Saturday, February 27, 2016 6:18 PM


Saturday, February 27, 2016 6:40 PM

Click an Icon Via Image can never be malicious.....

I am doing 1 project on this where i could not handle Via windows handler, ..Just last thing Iron, How do i Right Click i.e Mouse Right Click..

 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

 mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

Tried this but failed

 I disagree with that,  the image could be of anything within the screen and could be something the user does not want to click on.

 First you need the correct VB.Net signature for the mouse_event Api function.  You can find an example of using this function that i posted in the link below.

How do i make the mouse click automatically?

 PS - Don`t forget to mark the post or posts that answer your question as the answers.   8)

If you say it can`t be done then i`ll try it

Thank You Iron...this Did the Job, But there is lot of Lag finding the Icon....Appreciate your Support.


Saturday, February 27, 2016 6:41 PM

I want to solve this Problem using Image , not Via Environment.GetFolderPath,

IronRazer & Tom both of your Code is 90% working....

Problem Statement is ....

1. If i have 2 Similar Images but Text is different ...its causing Problem.

2. Just Need to Right Click on the Icon Once Image Found

You need to put on your thinking cap and look at the code we give you and understand it. What my example does is "fuzzy" so it works where it works. Its only checking some of the pixels, the corners and a diagonal. 

Razerz example will do an exact match. Of course that is a bit slower (exact lockbits vs fuzzy lockbits) but it sounds like speed is not an issue?

It still seems there is a better way. Just look for the text of the icon in the desktop icons? Again if you tell Razerz why you are doing this I am sure he can give you a good way.

PS Razerz and I had fun with this find Waldo problem a while ago and we both know the result. That is why I knew he had a better way of finding the image than GetPixel as I showed in my example. Using lockbits is faster if done right.

It does get to the speed vs accuracy and what your exact needs are. We dont know what you are doing so we just shoot out some ideas. You are the project manager and final decision maker and know what is going on person and debugger. Not us. You take the bits and pieces we give you and bend and shape and join them into your project code sculpture. Have a question? Feel free to ask.

Thank You tommytwotrain for your Help as well..