Jump to content

Mouse position in scrollable child GUI


bob.frew
 Share

Recommended Posts

Hi people,
I hope you can help.


I have a GUI with a scrollable child GUI containing an image.
I am trying to get the x,y position of the mouse relative to the image.

I'm using
opt ('MouseCoordMode', 2)
MouseGetPos() and GUIGetCursorInfo($Child_GUI) both return the position of the mouse on the window.

So unless there is another command I have overlooked ??? !!
The alternate way is if I knew the current top and left of the image that is actually visible.
From this it would be simple to calculate the image (x,y) that the mouse is over.

I have used #include "GUIScrollbars_Ex.au3"  for the scrollbars.
Had hoped the values I need would be lurking in that code but if they are I can't see them.

I suspect I am missing the point somewhere (again)

All help greatly appreciated.
            Bob

AutoIt is great - leave me alone and I'll play for hours

Link to comment
Share on other sites

  • Moderators

bob.frew,

Post the code you are using to create the GUI and child and I will take a look. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Opt ("ExpandEnvStrings", 0)
Opt ("ExpandVarStrings", 1)
Opt ("MustDeclareVars", 1)
opt ('MouseCoordMode', 2)        ; set relative coordinate system

;
#include <Array.au3>
#Include <File.au3>
#include <Date.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include "GUIScrollbars_Ex.au3"
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
; ------------------------------------------------ version specifics
Dim $version = "V2.1"
Dim $version_date = "Nov 2013"
Dim $ico = @ScriptDir & "\_images\im.ico"
Dim $tname = "   ImgPart " & $version

; ------------------------------------------------ GUI variables
Dim $Cwindow,$Main_msg,$Child_GUI
Dim $c_cancel_button,$c_start_button

Dim $window_colour = 0x00005b ;     dark blue
Dim $f_colour_front = 0x000000 ;     white
Dim $f_colour_back = 0x007c7c ;     pale green

Dim $font = "Arial Narrow"
Dim $font2 = "Century Gothic"
; ------------------------------------------------
Dim $cancelled = 0;
; ------------------------------------------------ get image file
Dim $file_in = FileOpenDialog("ImgPart - Choose file", "", "img (*.*)", 1 + 2 ) ;  e.g. "F:\_Current\Lang\AutoIt\Mint\_data\pdf\Mint_2012_01.pdf"
If @error Then
   MsgBox(4096,"ImgPart","No File(s) chosen. Prog ends ")
    Exit
EndIf
; ------------------------------------------------ get info on image
_GDIPlus_Startup ()
Local $hImage = _GDIPlus_ImageLoadFromFile($file_in)
If @error Then
    MsgBox(16, "Error", "Does the file exist?")
    Exit 1
EndIf
Dim $W = _GDIPlus_ImageGetWidth($hImage)
Dim $H = _GDIPlus_ImageGetHeight($hImage)
_GDIPlus_ImageDispose ($hImage)
_GDIPlus_ShutDown ()
; ------------------------------------------------
;MsgBox(4096,"ImgPart","$W = " & $W & ", $H = " & $H)

; ######################################################################################

createGUI()
showGUI()

Exit
; ######################################################################################


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Func createGUI()
    Local $tbase  = 640;
; MsgBox(4096,"ImgPart","$W = " & $W & ", $H = " & $H & ", tbase = " & $tbase)


    Local $twidth = 140;
    Local $r = 190;
;    $Cwindow = GUICreate($tname, $W, $H+100, 10, 10, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS + $WS_HSCROLL + $WS_VSCROLL)
    $Cwindow = GUICreate($tname, 600, 700, 10, 10, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

    GUISetIcon($ico)
    GUISetBkColor($window_colour)
     ; use width and height as per image


    $c_cancel_button = GUICtrlCreatePic("", 30, $tbase, 60, 20)       ;
    GUICtrlSetImage($c_cancel_button, @ScriptDir & "\_images\Stop_60.jpg")



    GUISetState(@SW_SHOW, $Cwindow)
    ; --------------------------------------------------------------------


    $Child_GUI = GUICreate("Child", 600, 600, 0, 0, $WS_CHILD, -1, $Cwindow)

    GUICtrlCreatePic($file_in, 0, 0, $W, $H)
    GUISetState(@SW_SHOW, $Child_GUI)

    _GUIScrollbars_Generate($Child_GUI, $W, $H)
    Return
EndFunc   ;==>createCatalogueGUI
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Func showGUI()
    Local $hDLL = DllOpen("user32.dll")
    While 1
        $Main_msg = GUIGetMsg()
        If $Main_msg = $GUI_EVENT_CLOSE Then
            $cancelled = 1;
            ExitLoop
        EndIf

        If $Main_msg = $c_cancel_button Then
            $cancelled = 1;
            ExitLoop
        EndIf
#cs
        If $Main_msg = $c_start_button Then
            validate()
            If $valid=1 Then
               ExitLoop
            EndIf
        EndIf
#ce
    If _IsPressed("20", $hDLL) Then
        ; ConsoleWrite("_IsPressed - Space Bar was pressed." & @CRLF)

        While _IsPressed("20", $hDLL)      ; Wait until key is released.
            Sleep(250)
        WEnd
        ; ConsoleWrite("_IsPressed - Space Bar was released." & @CRLF)
        addPoint()
    EndIf

    Wend
    Return
EndFunc
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Func addPoint()
    Local $mouse = MouseGetPos()
    MsgBox(0, "Mouse x,y:  ", $mouse[0] & "," & $mouse[1])

    Local $a = GUIGetCursorInfo($Child_GUI)
    MsgBox(0, "GUI get Mouse x,y:  ", $a[0] & ", " & $a[1] )
;    GUICtrlSetData($x, $a[0])
;    GUICtrlSetData($y, $a[1])
    Return
EndFunc
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 

// Am only at start of this idea

 

Bob

Edited by Melba23
Added tags

AutoIt is great - leave me alone and I'll play for hours

Link to comment
Share on other sites

  • Moderators

bob.frew,

Got it. When you post code in future please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

M23

Edit: I hate scrollbars! This is proving more difficult than I thought - I am getting very nearly the correct answer but there is something not quite right and I cannot find out where. :wacko:

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

bob.frew,

Ready for a quick tutorial on scrollbars? :D

Scrollbar movement is based on the dimensions of virtual lines and characters of the text that might be present in the GUI. As we are scrolling an image it is quite likely that this will not exactly be the correct number of pixels to match a round number of lines or characters and so there will be either a small margin around it (more likely in my experience) or it will be slightly cropped. Still hanging in there? ;)

The image I was using was slightly cropped - which is why I said I was getting coordinates that were slightly off. In fact the coordinates were correct but the image was not quite a large as I thought it should be. Once I realised that all became clear. I have cleaned up the script a bit to clarify what is going on - and used an Accelerator key to fire the coordinate calculation as it is much cleaner than using _IsPressed:

#include <GUIConstantsEx.au3>
#include "GUIScrollbars_Ex.au3"
#include <WindowsConstants.au3>
#include <GDIPlus.au3>

Global $Cwindow, $Main_msg, $Child_GUI
Global $c_cancel_button, $c_start_button, $cImage, $cDummy

Global $version = "V2.1"
Global $version_date = "Nov 2013"
Global $ico = @ScriptDir & "\_images\im.ico"
Global $tname = "   ImgPart " & $version

Global $window_colour = 0x00005b ;     dark blue
Global $f_colour_front = 0x000000 ;     white
Global $f_colour_back = 0x007c7c ;     pale green

Global $font = "Arial Narrow"
Global $font2 = "Century Gothic"
Global $cancelled = 0;

Global $file_in = FileOpenDialog("ImgPart - Choose file", "", "img (*.*)", 1 + 2) ;  e.g. "F:\_Current\Lang\AutoIt\Mint\_data\pdf\Mint_2012_01.pdf"
If @error Then
    MsgBox(4096, "ImgPart", "No File(s) chosen. Prog ends ")
    Exit
EndIf

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($file_in)
If @error Then
    MsgBox(16, "Error", "Does the file exist?")
    Exit 1
EndIf
Global $W = _GDIPlus_ImageGetWidth($hImage)
Global $H = _GDIPlus_ImageGetHeight($hImage)

ConsoleWrite("Image: " & $W & " - " & $H & @CRLF)

_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

createGUI()

; Now we get the scrollbar factors to determine how each of them moves 
Global $aFactors = _Get_Factors()

While 1
    $Main_msg = GUIGetMsg()
    Switch $Main_msg
        Case $GUI_EVENT_CLOSE
            $cancelled = 1;
            ExitLoop

        Case $c_cancel_button
            $cancelled = 1;
            ExitLoop

    Case $cDummy
        addPoint()

    EndSwitch

WEnd

Exit


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Func createGUI()
    Local $tbase = 640;
    Local $twidth = 140;
    Local $r = 190;
    $Cwindow = GUICreate($tname, 600, 700, 10, 10, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

    GUISetIcon($ico)
    GUISetBkColor($window_colour)

    $c_cancel_button = GUICtrlCreatePic("", 30, $tbase, 60, 20) ;
    $cImage = GUICtrlSetImage($c_cancel_button, "M:\Program\Au3 Scripts\Images\VistaX.jpg")

    $cDummy = GUICtrlCreateDummy()

    GUISetState(@SW_SHOW, $Cwindow)

    Local $aAccelKeys[1][2] = [["{SPACE}", $cDummy]]
    GUISetAccelerators($aAccelKeys)

    ; --------------------------------------------------------------------

    $Child_GUI = GUICreate("Child", 600, 600, 0, 0, $WS_CHILD, -1, $Cwindow)
    GUISetBkColor(0xFF0000)

    $cImage = GUICtrlCreatePic($file_in, 0, 0, $W, $H)
    GUISetState(@SW_SHOW, $Child_GUI)

    _GUIScrollbars_Generate($Child_GUI, $W, $H) ; Using the correct values cropped my image a bit.  I needed to add 25 to each

    Return

EndFunc   ;==>createGUI

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Func addPoint()

    ; Where is the cursor on the child?
    Local $a = GUIGetCursorInfo($Child_GUI)
    ; Add the amount the image has been scrolled (pixels = scroll units * relevant factor)
    $iX = $a[0] + _GUIScrollBars_GetScrollInfoPos($Child_GUI, $SB_HORZ) * $aFactors[0]
    $iY = $a[1] + _GUIScrollBars_GetScrollInfoPos($Child_GUI, $SB_VERT) * $aFactors[1]

    ConsoleWrite("Mouse: " & $iX & " - " & $iY & @CRLF)

    Return
    
EndFunc   ;==>addPoint
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Func _Get_Factors()

    Local $aFactors[2]
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    ; Determine text size
    Local $hDC = DllCall("user32.dll", "handle", "GetDC", "hwnd", $Child_GUI)
    If Not @error Then
        $hDC = $hDC[0]
        DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "ptr", DllStructGetPtr($tTEXTMETRIC))
        If @error Then
            $iError = @error
            $iExtended = @extended
            DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $Child_GUI, "handle", $hDC)
            Return SetError($iError, $iExtended, -2)
        EndIf
        DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $Child_GUI, "handle", $hDC)
    Else
        Return SetError(@error, @extended, -1)
    EndIf
    ; And store the factors
    $aFactors[0] = DllStructGetData($tTEXTMETRIC, "tmAveCharWidth") ; This is the average width of a character
    $aFactors[1] = DllStructGetData($tTEXTMETRIC, "tmHeight") + DllStructGetData($tTEXTMETRIC, "tmExternalLeading") ; This is the height of a line

    Return $aFactors

EndFunc
Please ask if you have any questions - scrollbars are really complex beasts and I still find them difficult to deal with even after all the experimentation I did to produce the UDF you are using to create them. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi M23,
many thanks for taking the time to look at my problem.
Very helpful indeed.

I had assumed (there's that word again) a couple of variables might have been lying about after the scroll bar had moved the display  so that I did not need to get involved with the scrollbars themselves.

Not being used to using DllCall the _Get_Factors() function phases me somewhat. I will have to come back to that later.
If all goes to plan I hope to be able to move the scrollbars to a given position once this first part is working ok. So will no doubt be doing some reading at that time.
For now you have got me moving forward again - 

Am sorry about posting the code outside a box. Have made a note to self for next time.
I like your use of Accelerator key. I had not come across those before.  

Thanks again

                       ... Bob 

 

AutoIt is great - leave me alone and I'll play for hours

Link to comment
Share on other sites

  • Moderators

bob.frew,

 

using DllCall the _Get_Factors() function phases me somewhat

Not the easiest bit of code for a beginner that! :D

However, all it does is to ask Windows about the font that would be used in that GUI - as I explained, Windows uses the font average width and line height to determine how to move the scrollbars. "DC" is the "device context" of the GUI - essentially the description of how it will look - and we ask Windows for ithe GUI's "TextMetrics" data from which we extract the required dimensions. Does that help clear the mist at all? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...