Jump to content

Recommended Posts

Posted

Hello,

At the beggining I have to say "Hello" to everyone. It's my first post here.
I am student and I got an exercise from my teacher (He is active member here, so if you reading this, don't lower my grade please ^^ ) 
I have to write program that click on dots in MSPaint. 

Here it is visualisation how it should work. (image)

Teacher makes dots in random places on screen in MSPaint, and my code has to click each and then click them backward. 

Okey. That's all about how it should work. Now the part what I did figuret out:

local dots[] = []
$dot = PixelSearch(0,90,1365,650,0x000000)
If Not @error Then
    MouseClick("Left",$dot[0],$dot[1],1,10)
EndIf

I don't know how exacly should I add the position of the each dot ( or I shouldn't ?) . It's strange for me - I mostly code in Java. 

Is there any library that I can use? 

Thanks for help. ~StudentJack

exercise.png

Posted (edited)

Hi, this is interesting, there are probably lots of ways to do it, but i think my approach would start by looping the pixelsearch until i got all points, somehow ignore points too close to each other, meaning pixels around a specific pixel, because what you have there is a circle with lots of black pixels. When i say somehow, i mean maybe add those points into an exclusion list if the difference between x1 and x2 < 10px or so.

After i got all the points, i'd sort them by the X coordinate, then loop through them with the mouse click, reverse sort, and then click all back.

Edited by careca
  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

@StudentJack 

:welcome:to the forum.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I tried to figure it out, so I wrote this code and found out something weird. 
PixelSearch - searches the nearest point to x1 and x2 (PixelSearch(x1,x2)).

My code: 

Global $Paused
HotKeySet('{Insert}','TogglePause')
WinActivate("dots - Paint")
Func FindDot()
        $dot = PixelSearch(8,146,1187,613,0x000000)
        $nextdot = PixelSearch(8,146,1187,613,0x000000,0,10)
        If @error Then
            Exit
        Else
             MouseMove($nextdot[0],$nextdot[1],10)
        EndIf
EndFunc

While 1
    FindDot()
WEnd

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
    WEnd
EndFunc

In Java: I'd get mouse cords every time I found the black dot, then add it to the table, stop the loop while it's on first dot and then search for another and again.
In autoit I have found something like "Imagesearch" ? Maybe it's better to use than PixelSearch? 

 

@mLipok
Hello :D

dots.gif

Posted

Try searching from left to right?

Global $Paused
HotKeySet('{Insert}', 'TogglePause')
WinActivate("dots - Paint")
$dot = 1
$count = 1
While $count < 1187
    $dot = PixelSearch(8 + $count, 146, 8 + $count, 613, 0x000000)
    If Not @error Then
        MouseMove($dot[0], $dot[1], 50)
        MouseClick("Left", $dot[0], $dot[1], 1, 10)
    EndIf
    $count = $count + 4
WEnd


Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
    WEnd
EndFunc   ;==>TogglePause

 

Posted

Okey. That's nice, thanks @JoHanatCent. So if I'd like to make it search from left to right and when there is no more black dots, start searching from right to left I need to change this: (?) 

Global $Paused
HotKeySet('{Insert}', 'TogglePause')
WinActivate("dots - Paint")
$dot = 1
$count = 1 ; =>change here to 1187
While $count < 1187 ; => and here to 8?
; and then $dot would looks like this: 
; $dot = PixelSearch(0 + $count, 613, 0 + $count, 146, 0x00000)


    $dot = PixelSearch(8 + $count, 146, 8 + $count, 613, 0x000000)
    If Not @error Then
        MouseMove($dot[0], $dot[1], 50)
        MouseClick("Left", $dot[0], $dot[1], 1, 10)
    EndIf
    $count = $count + 4
WEnd

I understand the point? 

Posted

"  .. how can I make it search for dots from Left to Right after script finished searching from right to left?   .."

You can change this :

While $count < 1187

to While 0.

The counter then needs to be changed to minus when going over 1187 until you get back to less than 8.

As soon as it gets to 8 change the counter to positive again?

Posted (edited)
#include<Misc.au3>
WinActivate("[CLASS:MSPaintApp]")
Global $WPos = WinGetPos("[CLASS:MSPaintApp]")
Global $dot = 0, $h = $WPos[1] + 155

While 1
If _IsPressed('1B') Then
    Exit
EndIf
For $i = 1 To 2
If $i = 1 Then
For $w = $WPos[0] + 15 To $WPos[2] Step 10
        $dot = PixelSearch($w, $h, $w+10, $WPos[3], 0x000000, 0, 2)
        If Not @error Then
                MouseMove($dot[0], $dot[1], 5)
        EndIf
Next
Else
For $w = $WPos[2] To $WPos[0] + 15 Step -10
        $dot = PixelSearch($w, $h, $w-10, $WPos[3], 0x000000, 0, 2)
        If Not @error Then
                MouseMove($dot[0], $dot[1], 5)
        EndIf
Next
EndIf
Next
WEnd

EDIT corrections

Edited by careca
  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

This is how i did that, but i dont know why it doesnt draw straight lines , but it curves at the second point ?

 

HotKeySet("{ESC}", "Terminate")
WinActivate("[CLASS:MSPaintApp]")

Global $array[0]
$hDLL = DllOpen("user32.dll")
While 1
    Sleep(10)
    If _IsPressed("01", $hDLL) Then
        $aPos = MouseGetPos()
        $string = _ArrayToString($aPos,".")
        _ArrayAdd($array,$string)
        Sleep(200)
    EndIf
;~ $dot = PixelSearch($w, $h, $w+10, $WPos[3], 0x000000, 0, 2)
WEnd

Func Terminate()
    DllClose($hDLL)
    ConnectDots($array)
;~  _ArrayDisplay($array,"$array")
    Exit
EndFunc

Func ConnectDots($arr)
    For $i=0 to UBound($arr) - 1 Step 1
        If $i= UBound($arr) - 1 Then ExitLoop
        $test = StringSplit($arr[$i],".",2)
        $test2 = StringSplit($arr[$i+1],".",2)
        MouseClickDrag($MOUSE_CLICK_LEFT, $test[0],$test[1], $test2[0],$test2[1],30)
    Next
EndFunc

 

Posted (edited)

Hello, 

Here's code that I used and there is result. Thanks to @careca

#include<Misc.au3>
WinActivate("[CLASS:MSPaintApp]")
Global $WPos = [5,143,823,630]
Global $dot = 0, $h = $WPos[1]

While 1
If _IsPressed('1B') Then
    Exit
EndIf
For $i = 1 To 2
If $i = 1 Then
For $w = $WPos[0] + 15 To $WPos[2] Step 10
        $dot = PixelSearch($w, $h, $w+10, $WPos[3], 0x000000, 0, 2)
        If Not @error Then
                MouseMove($dot[0], $dot[1], 5)
        EndIf
Next
Else
For $w = $WPos[2] To $WPos[0] + 15 Step -10
        $dot = PixelSearch($w, $h, $w-10, $WPos[3], 0x000000, 0, 2)
        If Not @error Then
                MouseMove($dot[0], $dot[1], 5)
        EndIf
Next
EndIf
Next
WEnd

dots_script.gif

I've got another task and need a little more help ^^ . I do not want to start new thread so I will ask question here. 
I have to measure the distance from one dot to another and write the result. But it couldn't be so easy, so here is a catch! If the distance from mouse cord is less than 1, do not click this dot. Look for another that is further. I have an idea, that I will show to you guys. 
To calculate the distance we can use this: 

Func Pixel_Distance($x1, $y1, $x2, $y2) ;Pythagoras theorem for 2D
    Local $a, $b, $c
    If $x2 = $x1 And $y2 = $y1 Then
        Return 0
    Else
        $a = $y2 - $y1
        $b = $x2 - $x1
        $c = Sqrt($a * $a + $b * $B)
        Return $c
    EndIf
EndFunc   ;==>Pixel_Distance

The credits goes to @UEZ. I did not write it.

To get the mouse cords, I can use: 
MouseGetPos

Now I don't know exacly how to glue it into one. Script looks for dots and it's great. I definitly use it for finding dots, but now I have to calculate distance betwen this dots.
 

@Edit
Please look at this pseudocode, is my way of thinking right? 

dotsora.png.2229dbe62f2da1efd12b5ae234f4852b.png

$BlackDotCord
$RedDotCord
$OrangeDotCord

;First calculate distance black to orange. So xy,x1y1 should be 
$x = $BlackDotCord[0]
$y = $BlackDotCord[1]
$x1 = $OrangeDotCord[0]
$y1 = $OrangeDotCord[1]
$dist = Pixel_Distance($x,$y,$x1,$y1)
If $dist < 1 Then
MouseMove(~~?

I have to store every distance in table? 

Edited by StudentJack
ideas
Posted
#include <Misc.au3>
WinActivate("[CLASS:MSPaintApp]")
Global $WPos = WinGetPos("[CLASS:MSPaintApp]")
ConsoleWrite('$WPos[0] - ' & $WPos[0] & ' $WPos[1] - ' & $WPos[1] & ' $WPos[2] - ' & $WPos[2] & ' $WPos[3] - ' & $WPos[3] & @CRLF)
Global $dot = 0, $h = $WPos[1] + 155
Global $xPrev = 0, $yPrev = 0, $PDist

While 1
    If _IsPressed('1B') Then
        Exit
    EndIf
    For $i = 1 To 2
        If $i = 1 Then
            For $w = $WPos[0] + 15 To $WPos[2] Step 10
                $dot = PixelSearch($w, $h, $w + 10, $WPos[3] - 30, 0x000000, 0, 1)
                If Not @error Then
                    If $xPrev <> 0 Or $yPrev <> 0 Then
                    Pixel_Distance($xPrev, $yPrev, $dot[0], $dot[1])
                    EndIf
                    MouseMove($dot[0], $dot[1], 5)
                    $xPrev = $dot[0]
                    $yPrev = $dot[1]
                EndIf
            Next
        Else
            For $w = $WPos[2] To $WPos[0] + 15 Step -10
                $dot = PixelSearch($w, $h, $w - 10, $WPos[3] - 30, 0x000000, 0, 1)
                If Not @error Then
                    If $xPrev <> 0 Or $yPrev <> 0 Then
                    Pixel_Distance($xPrev, $yPrev, $dot[0], $dot[1])
                    EndIf
                    MouseMove($dot[0], $dot[1], 5)
                    $xPrev = $dot[0]
                    $yPrev = $dot[1]
                EndIf
            Next
        EndIf
    Next
WEnd
;=============================================================================
Func Pixel_Distance($x1, $y1, $x2, $y2) ;Pythagoras theorem for 2D
    Local $a, $b, $c
    If $x2 = $x1 And $y2 = $y1 Then
        Return 0
    Else
        $a = $y2 - $y1
        $b = $x2 - $x1
        $c = Sqrt($a * $a + $b * $B)
        ConsoleWrite($c &@CRLF)
        Return $c
    EndIf
EndFunc   ;==>Pixel_Distance

 

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted
  On 10/29/2018 at 8:36 PM, StudentJack said:

To calculate the distance we can use this: 

Expand  

Amazing... 

 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...