Rkey

Click a GUI label just after showing the GUI

3 posts in this topic

Hi Forum,

I'm having trouble accomplishing the following:

I'd like to click a label on the GUI just after showing the GUI itself (to eventually fire the event bind to the label)

Here is simple version of the code (see the line under "Trying to click the 1st link")

#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <SendMessage.au3>
#include <AutoItConstants.au3>

#Region GLOBAL VARIABLES
    Global $iGuiW = 400, $iGuiH = 400            ; GUI Size, Width and Height
    Global $iGuiTopH = 52, $iGuiBotH = 52        ; GUI Size, Top Height, Bottom Height
    Global $iLeftWidth = 250                    ; GUI Size, Left Menu Width
    Global $iGap = 10                            ; GUI Size, Space between links (vertical)
    Global $iRightWidth = 100
    Global $hMainGUI
#EndRegion GLOBAL VARIABLES

#Region OPTIONS
    Opt('MustDeclareVars', 1)        ; Variables must be pre-declared - removes the chance for misspelled variables causing bugs
    Opt("GUIOnEventMode", 1)         ; Change to OnEvent mode
#EndRegion OPTIONS

_MainGui()

Func _MainGui()
    ; Vars
    Local $hFooter, $nMsg, $aPos
    Local $iLinks = 5                                                                                        ; Number of links (minus 1)
    Local $sMainGuiTitle = "Main"                                                                            ; Title
    Local $sHeader = " Main - " & @ScriptName & " - " & @ComputerName                                        ; Header
    Local $sFooter = "2017©"                                                                                ; Footer
    GLobal $aLink[$iLinks], $aPanel[$iLinks], $aIdTreeviewFile[$iLinks]                                        ; Links and Panels
        $aLink[0] = $iLinks - 1
        $aPanel[0] = $iLinks - 1

    ; Shortcut to close to escape possible loop
    HotKeySet("{F1}", "OnF1")

    ; Create GUI
    $hMainGUI = GUICreate($sMainGuiTitle, $iGuiW, $iGuiH, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_TABSTOP))

    ;Adding Links and bind event
    For $i = 1 To $aLink[0]
            $aLink[$i] = _AddNewLink('Link ' & $i)
            GUICtrlSetOnEvent($aLink[$i], "_ShowMessage")
    Next

    ;show the main GUI
    GUISetState(@SW_SHOW, $hMainGUI)

    ;Trying to click the 1st link
    local $hwnd = GUICtrlGetHandle ($aLink[1])
    _DebugPrint($hwnd & ' search for handle')
    local $return = _SendMessage($hwnd, $BM_CLICK)
    _DebugPrint($return & ' return from sendmessage')

    While 1
        Sleep(100) ; Sleep to reduce CPU usage
    WEnd

EndFunc ;==>_MainGui

Func _ShowMessage()
    MsgBox(1, '', GUICtrlRead(@GUI_CtrlId))
EndFunc

Func _AddNewLink($sTxt, $iIcon = -44)
    Local $hLink = GUICtrlCreateLabel($sTxt, 36, $iGuiTopH + $iGap, $iLeftWidth - 46, 17)
    GUICtrlSetCursor(-1, 0)
    GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
    GUICtrlCreateIcon("shell32.dll", $iIcon, 10, $iGuiTopH + $iGap, 16, 16)
    GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
    $iGap += 22
    Return $hLink
EndFunc ;==>_AddNewLink

Func _AddControlsToPanel($hPanel)
    GUISwitch($hPanel)
EndFunc ;==>_AddControlsToPanel

; to create escape function in case X is blocked.
Func OnF1()
    Exit
EndFunc        ;==>OnF1

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( "DebugPrint -->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF)
EndFunc   ;==>_DebugPrint

Thanks in advance!

Rick

Share this post


Link to post
Share on other sites



Rkey,

Just use ControlClick:

;Trying to click the 1st link
    ControlClick($hMainGUI, "", $aLink[1])
    ;local $hwnd = GUICtrlGetHandle ($aLink[1])
    ;_DebugPrint($hwnd & ' search for handle')
    ;local $return = _SendMessage($hwnd, $BM_CLICK, 0)
    ;_DebugPrint($return & ' return from sendmessage')

For me that fires the  _ShowMessage function for the correct control.

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Thanks Melba, that's exactly what I was searching for.

Although I swear I looked at this function before, but couldn't get it to work (must be Friday....)

Thanks again, Cheers Rick

Share this post


Link to post
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