Sign in to follow this  
Followers 0
pbecks1963

Help with getting Ok-button active/Window to front

3 posts in this topic

#1 ·  Posted (edited)

Hello y'all,

I have a this script:

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

GUICreate('MyGUI', @DesktopWidth, @DesktopHeight, 0, 0, BitOR($WS_POPUP, $WS_EX_TOPMOST))
GUICtrlCreatePic("Enquete.bmp", 0, 0, @DesktopWidth, @DesktopHeight)
GUISetState()

; Set filenames
$file_to_write = @ScriptDir & "\InquiryAnswers.txt"
$file_to_read = @ScriptDir & "\InquiryQuestions.txt"

; open file to read and store the handle
$handle_read = FileOpen($file_to_read, 0)
; open file to Write and store the handle
$handle_write = FileOpen($file_to_write, 1); append mode ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

While 1
             

    ; Set or Reset array for this set of answers
    Local $aAnswers[1] = [0]
         
    ; Loop through each line of the file to get the questions
    While 1
         
        ; read each line from a file - starting at 1 for each pass
        If $aAnswers[0] = 0 Then
            $line_read = FileReadLine($handle_read, 1)
        Else
            $line_read = FileReadLine($handle_read)
        EndIf
        ; exit the loop if end of file
        If @error = -1 Then ExitLoop
         
        ; get answer to question
        $answer = _Get_Answer($line_read)

         
        ; Increase count
        $aAnswers[0] += 1
        ; Increase array size
                 
        ReDim $aAnswers[$aAnswers[0] + 1]
        ; Save answer
        $aAnswers[$aAnswers[0]] = $answer
         
    WEnd
         
    ; Collect answers
    $sAllAnswers = ""
    For $i = 1 To $aAnswers[0] - 1
        $sAllAnswers &= $aAnswers[$i] & ","
    Next
             
    $sAllAnswers &= $aAnswers[$i]

    FileWriteLine($handle_write, $sAllAnswers)
         
    ; Write file
    MsgBox(0, "", "Thank you!",2)

         

WEnd
         
; close the file handle for read
FileClose($handle_read)
; close the file handle for write
FileClose($handle_write)
         


Exit
         
Func _Get_Answer($sQuestion)
    
             

    $hGUI = GUICreate("Questions", 700, 180, Default, Default, BitOR($WS_CAPTION, $WS_POPUP), $WS_EX_TOPMOST)
         
    GUISetFont(24, Default, Default, "MS Comic Sans")

    GUICtrlCreateLabel($sQuestion, 10, 10, 700, 40)
         
    $hInput = GUICtrlCreateInput("", 10, 60, 650, 40)
         
    $hOK_Button = GUICtrlCreateButton("OK", 10, 120, 120, 50)

         
    GUISetState()
         
    While 1

        Switch GUIGetMsg()
                
            Case $hOK_Button
                $sAnswer = GUICtrlRead($hInput)
                GUIDelete($hGUI)
                Return $sAnswer
                
        EndSwitch
         
    WEnd
         
EndFunc   ;==>_Get_Answer

What it does is show a window with a question and get the answer to that question. This is looped (in the rest of the script) until the end of the file (which holds the questions) is reached. HOWEVER, every time a new question is asked, the window isn't active (i have to click with the left mouse button on the window to get it active) and the OK-button needs to be clicked (in stead of a plain <ENTER> which i prefer) in order to go to the next question.

How can i change this?

ps: i noticed that when i remove lines 5, 6 and 7 the popup windows are always active! But i need this background pic to be displayed ALl the time..

howwww....???

Edited by pbecks1963

Share this post


Link to post
Share on other sites



pbecks1963,

To ensure your input has focus, add a line to set the focus to it when the GUI is displayed.

If you want to use Enter to terminate the data input, use an Accelerator key.

Look for the <<<<<<<<<<<<<<<<< lines:

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

GUICreate('MyGUI', @DesktopWidth, @DesktopHeight, 0, 0, BitOR($WS_POPUP, $WS_EX_TOPMOST))
GUICtrlCreatePic("M:\Program\Au3 Scripts\Images\facepalm.jpg", 0, 0, @DesktopWidth, @DesktopHeight)
GUISetState()

; Set filenames
$file_to_write = @ScriptDir & "\InquiryAnswers.txt"
$file_to_read = @ScriptDir & "\InquiryQuestions.txt"

; open file to read and store the handle
$handle_read = FileOpen($file_to_read, 0)
; open file to Write and store the handle
$handle_write = FileOpen($file_to_write, 1); append mode 

While 1

    ; Set or Reset array for this set of answers
    Local $aAnswers[1] = [0]

    ; Loop through each line of the file to get the questions
    While 1

        ; read each line from a file - starting at 1 for each pass
        If $aAnswers[0] = 0 Then
            $line_read = FileReadLine($handle_read, 1)
        Else
            $line_read = FileReadLine($handle_read)
        EndIf
        ; exit the loop if end of file
        If @error = -1 Then ExitLoop

        ; get answer to question
        $answer = _Get_Answer($line_read)

        ; Increase count
        $aAnswers[0] += 1
        ; Increase array size

        ReDim $aAnswers[$aAnswers[0] + 1]
        ; Save answer
        $aAnswers[$aAnswers[0]] = $answer

    WEnd

    ; Collect answers
    $sAllAnswers = ""
    For $i = 1 To $aAnswers[0] - 1
        $sAllAnswers &= $aAnswers[$i] & ","
    Next

    $sAllAnswers &= $aAnswers[$i]

    FileWriteLine($handle_write, $sAllAnswers)

    ; Write file
    If MsgBox(4, "", "Thank you!" & @CRLF & @CRLF & "Do you want to exit?") = 6 Then Exit ; <<<<<<<<<<<<<<<<<<<<<<<<<<

WEnd

; close the file handle for read
FileClose($handle_read)
; close the file handle for write
FileClose($handle_write)

Exit

Func _Get_Answer($sQuestion)

    $hGUI = GUICreate("Questions", 700, 180, Default, Default, BitOR($WS_CAPTION, $WS_POPUP), $WS_EX_TOPMOST)

    GUISetFont(24, Default, Default, "MS Comic Sans")

    GUICtrlCreateLabel($sQuestion, 10, 10, 700, 40)

    $hInput = GUICtrlCreateInput("", 10, 60, 650, 40)

    $hOK_Button = GUICtrlCreateButton("OK", 10, 120, 120, 50)

    ; Set accelerators for Enter <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Dim $AccelKeys[1][2]=[["{ENTER}", $hOK_Button]]
    GUISetAccelerators($AccelKeys)

    GUISetState()

    _WinAPI_SetFocus(GUICtrlGetHandle($hInput)) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<< Set focus to input

    While 1

        Switch GUIGetMsg()

            Case $hOK_Button
                $sAnswer = GUICtrlRead($hInput)
                GUIDelete($hGUI)
                Return $sAnswer
        EndSwitch

    WEnd

EndFunc   ;==>_Get_Answer

I also amended your MsgBox so you can exit gracefully. Forcing people to use Task Manager to stop the script because you have made a full screen app with no exit code will not win you many friends. :(

M23


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

#3 ·  Posted (edited)

Thank you!

ps: I don't want people to be able to exit bacause then we would have to start this survey/inquiry again and again. This script runs on 2 dedicated pc's (with no or little human assistance) and at the end of the day I terminate the script and collect the data. (import it as csv in Excel)

Edited by pbecks1963

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
Sign in to follow this  
Followers 0