Jump to content

My first GUI: Mouse coords, cursor ID and color


wIck3d
 Share

Recommended Posts

This is a GUI that basically gives you the Mouse position(X,Y), the cursor ID and the color in hex and decimal of

where the mouse is pointing. Also it has buttons to copy any data you get and has a hotkey for copying the mouse

position. Moreover, it gives you the option of updating the data automatically or manually by pressing F4.

I know that AutoIt window info does most of that, but this script is helpful when working with someone else

and in need of these info but not wanting to install AutoIt on their pc.

Posted Image

This is my first GUI, any comments/suggestions on the GUI look or on the coding are appreciated.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $msg

  $automatic = 0                            
  $pos = MouseGetPos()                      
  $color = PixelGetColor($pos[0], $pos[1])  
  $cursorID = MouseGetCursor()              
    
  $gui = GUICreate("Mouse Coords & Color", 200, 180, -1, -1, -1, $WS_EX_TOPMOST)
  GUISetBkColor(0xDDDDDD)
  
  $menu = GUICtrlCreateMenu("File")
  $about = GUICtrlCreateMenuItem("About", $menu)
  $exit = GUICtrlCreateMenuItem("Exit", $menu)

  $pos_XY = GUICtrlCreateLabel("Coords: " & $pos[0] & ", " & $pos[1], 10, 20, 100,15)
  $color_Hex = GUICtrlCreateLabel("Color <Hex>: " & Hex($color,6), 10, 37, 130,15)              
  $cursID = GUICtrlCreateLabel("Cursor ID: " & $CursorID, 10, 54, 130,15)
  $color_Dec = GUICtrlCreateLabel("Color <Dec>: " & $color, 10, 71, 130,15)         

  $button_1 = GUICtrlCreateButton("Copy <F5>", 135, 16, 60, 19)
  $button_2 = GUICtrlCreateButton("Copy", 140, 34, 50, 19)
  $button_3 = GUICtrlCreateButton("Copy", 140, 52, 50, 19)
  $button_4 = GUICtrlCreateButton("Copy", 140, 70, 50, 19)

  GUICtrlCreateGroup("Options", 18, 91, 160, 60)
    $man = GUICtrlCreateRadio("Manual data update <F4>", 28, 105, 137, 20)
    $auto = GUICtrlCreateRadio("Automatic data update", 28, 125, 137, 20)       
    GUICtrlSetState($man, $GUI_CHECKED)
    
    
  Dim $AccelKeys[1][2]=[["{F5}", $button_1]]
    GUISetAccelerators($AccelKeys)
  HotKeySet("{F4}", "get_data")
    
  GUISetState()
  
  Do
    $msg = GUIGetMsg()
        
    If $automatic Then get_data()
        
    Switch $msg
        Case $button_1
          ClipPut($pos[0] & ", " & $pos[1])    
        Case $button_2
          ClipPut(Hex($color,6))
        Case $button_3
          ClipPut($cursorID)
        Case $button_4
          ClipPut($color)
        Case $man
          $automatic =0
        Case $auto
          $automatic =1
        Case $about
          MsgBox(0,"About","Programmed by w!ck3d, 11/02/08", 0, $gui)
        Case $exit
          ExitLoop
      EndSwitch
    
    sleep(10)
    WinSetOnTop($gui, 0, 1)
    Until $msg = $GUI_EVENT_CLOSE
    GUIDelete()


;a function to update the data if the new data is different.
Func get_data()    
    $pos2 = MouseGetPos()
    $color2 = PixelGetColor($pos2[0], $pos2[1])
    $cursorID2 = MouseGetCursor()
    
    If($pos[0] <> $pos2[0] or $pos[1] <> $pos2[1]) Then
      $pos = $pos2      
      GUICtrlSetData($pos_XY, "Coords: " & $pos[0] & ", " & $pos[1])
    EndIf
    
    If($color <> $color2) Then
      $color = $color2      
      GUICtrlSetData($color_Hex, "Color <Hex>: " & Hex($color,6))
      GUICtrlSetData($Color_Dec, "Color <Dec>: " & $color)
    EndIf
  
    If($cursorID <> $cursorID2) Then
      $cursorID = $cursorID2        
      GUICtrlSetData($cursID, "Cursor ID: " & $cursorID)
    EndIf

EndFunc

Edit: Fixes, Thanks to trancexx

1- Less cpu usage

2- Main gui not on top of About

3- Minor coding issues

2nd Edit: More Fixes, Thanks to trancexx again

1- Again less cpu usage

2- Minor coding issues

Edited by wIck3d
Link to comment
Share on other sites

If you think that it's not useful/nice, could you state why ?

I didn't say anything about usefulness or niceness. I was just curious about why TehWhale wrote that comment of his.

You have issue with evaluating array of mouse coordinates, it's causing extensive cpu usage, "About" message is covered by main gui, $color_Dec is partially covered with "Options" group and some other small things.

I'm sure that TehWale noticed all that before expressing his feelings about it.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $msg

$automatic = 0
$pos = MouseGetPos()
$color = PixelGetColor($pos[0], $pos[1])
$cursorID = MouseGetCursor()

$gui = GUICreate("Mouse Coords & Color", 200, 180, -1, -1, -1, $WS_EX_TOPMOST)
GUISetBkColor(0xDDDDDD)

$menu = GUICtrlCreateMenu("File")
$about = GUICtrlCreateMenuItem("About", $menu)
$exit = GUICtrlCreateMenuItem("Exit", $menu)

$pos_XY = GUICtrlCreateLabel("Coords: " & $pos[0] & ", " & $pos[1], 10, 20, 100, 15)
$color_Hex = GUICtrlCreateLabel("Color <Hex>: " & Hex($color, 6), 10, 37, 125, 15)
$cursID = GUICtrlCreateLabel("Cursor ID: " & $cursorID, 10, 54, 130)
$color_Dec = GUICtrlCreateLabel("Color <Dec>: " & $color, 10, 71, 125, 15)

$button_1 = GUICtrlCreateButton("Copy <F5>", 135, 16, 60, 19)
$button_2 = GUICtrlCreateButton("Copy", 140, 34, 50, 19)
$button_3 = GUICtrlCreateButton("Copy", 140, 52, 50, 19)
$button_4 = GUICtrlCreateButton("Copy", 140, 70, 50, 19)

GUICtrlCreateGroup("Options", 18, 90, 160, 60)
$man = GUICtrlCreateRadio("Manual data update <F4>", 28, 105, 137, 20)
$auto = GUICtrlCreateRadio("Automatic data update", 28, 125, 137, 20)
GUICtrlSetState($man, $GUI_CHECKED)


Dim $AccelKeys[1][2] = [["{F5}", $button_1]]
GUISetAccelerators($AccelKeys)
HotKeySet("{F4}", "get_data")

GUISetState()


While 1
    
    $msg = GUIGetMsg()

    If $automatic Then
        get_data()
    EndIf

    Switch $msg
        Case $button_1
            ClipPut($pos[0] & ", " & $pos[1])
        Case $button_2
            ClipPut(Hex($color, 6))
        Case $button_3
            ClipPut($cursorID)
        Case $button_4
            ClipPut($color)
        Case $man
            $automatic = 0
        Case $auto
            $automatic = 1
        Case $about
            MsgBox(0, "About", "Programmed by w!ck3d, 11/02/08", 0, $gui)
        Case $GUI_EVENT_CLOSE, $exit
            Exit
    EndSwitch

    Sleep(10)
    WinSetOnTop($gui, 0, 1)

WEnd


;a function to update the data if the new data is different.

Func get_data()
    
    $pos2 = MouseGetPos()
    $color2 = PixelGetColor($pos2[0], $pos2[1])
    $cursorID2 = MouseGetCursor()

    If $pos[0] <> $pos2[0] Or $pos[1] <> $pos2[1] Then
        $pos = $pos2
        GUICtrlSetData($pos_XY, "Coords: " & $pos[0] & ", " & $pos[1])
    EndIf

    If $color <> $color2 Then
        $color = $color2
        GUICtrlSetData($color_Hex, "Color <Hex>: " & Hex($color, 6))
        GUICtrlSetData($color_Dec, "Color <Dec>: " & $color)
    EndIf

    If $cursorID <> $cursorID2 Then
        $cursorID = $cursorID2
        GUICtrlSetData($cursID, "Cursor ID: " & $cursorID)
    EndIf

EndFunc
Link to comment
Share on other sites

I didn't say anything about usefulness or niceness. I was just curious about why TehWhale wrote that comment of his.

You have issue with evaluating array of mouse coordinates, it's causing extensive cpu usage, "About" message is covered by main gui, $color_Dec is partially covered with "Options" group and some other small things.

I'm sure that TehWale noticed all that before expressing his feelings about it.

Thanks for pointing out these issues and fixing them. About the cpu usage, i didn't know what was exactly causing it and i was wondering how to fix it,

But i never thought of using sleep() since the help file says never to use a sleep inside a gui loop. Also, One of the fixes you made is putting

($pos[0] <> $pos2[0] Or $pos[1] <> $pos2[1]) instead of ($pos <> $pos2). Isn't both doing the same exact thing ? I mean even with memory usage ?

One more thing, why did you use a while instead of a do loop ?

Edited by wIck3d
Link to comment
Share on other sites

Thanks for pointing out these issues and fixing them. About the cpu usage, i didn't know what was exactly causing it and i was wondering how to fix it,

But i never thought of using sleep() since the help file says never to use a sleep inside a gui loop. Also, One of the fixes you made is putting

($pos[0] <> $pos2[0] Or $pos[1] <> $pos2[1]) instead of ($pos <> $pos2). Isn't both doing the same exact thing ? I mean even with memory usage ?

One more thing, why did you use a while instead of a do loop ?

You should really experiment more with this. Try changing things to see what happens.

If you have done that (but truly) then you wouldn't ask about '$pos <> $pos2'. That's the main fix and that was the cause of extensive cpu usage. Sleep(10) is just for what comes after that. The way that you implemented Sleep(10) in this moment is just for covering eyes.

While... Wend is more appropriate for this. Why? Compare your code using Do... Until and While.... Wend version and draw conclusions yourself.

You should do something about declarations of used variables as well, to avoid possible bugs.

Select... EndSelect of yours really looks like novice job, regardless of what you might think of it.

Edited by trancexx
Link to comment
Share on other sites

You should really experiment more with this. Try changing things to see what happens.

If you have done that (but truly) then you wouldn't ask about '$pos <> $pos2'. That's the main fix and that was the cause of extensive cpu usage. Sleep(10) is just for what comes after that. The way that you implemented Sleep(10) in this moment is just for covering eyes.

While... Wend is more appropriate for this. Why? Compare your code using Do... Until and While.... Wend version and draw conclusions yourself.

You should do something about declarations of used variables as well, to avoid possible bugs.

Select... EndSelect of yours really looks like novice job, regardless of what you might think of it.

Thanks for replying. I ran some experiments and i could see now how much different they are. I can't understand why though. I mean the line directly below it ($pos = $pos2) should be causing extensive cpu usage as well, shouldn't it ? Also why did you use sleep() if it was only caused by the arrays?

I compared While...Wend and Do...Until and couldn't see a difference. I think it's just a matter of how someone reads or understands the code. Correct me if there is something to it other than that.

I just looked at both Select...EndSelect and Switch...EndSwitch.. and I could see why you would call it a novice job. I used Select..EndSelect just because one of the examples on the help file was using it :mellow: .

Fixing...

w!ck3d

Edited by wIck3d
Link to comment
Share on other sites

Thanks for replying. I ran some experiments and i could see now how much different they are. I can't understand why though. I mean the line directly below it ($pos = $pos2) should be causing extensive cpu usage as well, shouldn't it ? Also why did you use sleep() if it was only caused by the arrays?

I compared While...Wend and Do...Until and couldn't see a difference. I think it's just a matter of how someone reads or understands the code. Correct me if there is something to it other than that.

I just looked at both Select...EndSelect and Switch...EndSwitch.. and I could see why you would call it a novice job. I used Select..EndSelect just because one of the examples on the help file was using it :mellow: .

Fixing...

w!ck3d

While... WEnd is more general.

About $pos = $pos2; that will not cause any problems because it's perfectly legit action to copy (store) one array to some variable (that holds another array in this case).

But using $pos <> $pos2 to detemine if one array equals another or not is wrong.

Sleep(10) is just to avoid extensive cpu usage when position of the mouse cursor is changing and $automatic is 1.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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