Jump to content

Recommended Posts

Posted

Hi all,
In the following script, I wanted the OK button to have focus as soon as the GUI is displayed, so when I hit the "Enter" key, the GUI should be validated with its default values.
To do that, I added a ControlFocus() line, like this :

GUISetState(@SW_SHOW)
ControlFocus($hGUI, "", $idButton_OK) ; <======
While 1
...

Unfortunately, it doesn't seem to be enough, as the OK button doesn't respond to the Enter key !
To have it respond to the Enter key, I have (for example) to key Tab, then Shift-Tab. Now the OK button has "plain" focus and it responds to Enter. Also the button is now surrounded with a thicker line. I'm aware that the OK button would respond with the Spacebar or a mouse clic, but why isn't the Enter key initially accepted too, without that workaround of Tab / Shift-Tab ?

Thanks for anyone who could help me on that matter.
Here is the part of the script concerning the GUI :

#include <EditConstants.au3>        ; Koda (keep it)
#include <GUIConstantsEx.au3>       ; Koda (keep it)
#include <WindowsConstants.au3>     ; Koda (used here only to get a non-minimizable GUI)
#include <MsgBoxConstants.au3>
#include <GuiButton.au3>            ; for _GUICtrlButton_SetText()
#include <Misc.au3>                 ; for _IsPressed()

Opt("WinTitleMatchMode", -2)        ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Opt("TrayIconDebug", 1)             ;0=no info, 1=debug line info

; ephemeral variables for GUICtrlRead() . If checked ok, copy them into $v..._now
Global $vInput, $vMouse_Coord_H, $vMouse_Coord_V

; GUI control values at 1st passage, then copied from precedent line
Global $vInput_now = "1", $vMouse_Coord_H_now = "420", $vMouse_Coord_V_now = "250"

; if a GUI control got inconsistent value during Check_Input(), then update 2 error variables
Global $sError_Title, $sError_Msg

While 1
   F10()
   If MsgBox(BitOr($MB_SYSTEMMODAL, $MB_YESNO), "", "Again ?") = $IDNO Then Exit
Wend

; ===================================================================================
Func F10()
   $hGUI = GUICreate("Parameters", 276, 139, -1, -1, _
      BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU), @SW_SHOWDEFAULT) ; non-minimizable !
      ; same as BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX)

   GUICtrlCreateLabel("What kind of display (1/2/3) ?", 8, 8, 146, 17)
   $idInput = GUICtrlCreateInput($vInput_now, 223, 6, 33, 21, _
      BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
   GUICtrlSetLimit($idInput, 1)

   $idUpdown = GUICtrlCreateUpdown($idInput)
   GUICtrlSetLimit($idUpdown, 3, 1)

   GUICtrlCreateLabel("Mouse Horizontal coordinate :", 8, 34, 145, 17)
   $idMouse_Coord_H = GUICtrlCreateInput($vMouse_Coord_H_now, 223, 32, 33, 21, _
      BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
   GUICtrlSetLimit($idMouse_Coord_H, 4)

   GUICtrlCreateLabel("Mouse Vertical coordinate :", 8, 58, 133, 17)
   $idMouse_Coord_V = GUICtrlCreateInput($vMouse_Coord_V_now, 223, 56, 33, 21, _
      BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
   GUICtrlSetLimit($idMouse_Coord_V, 4)

   $idLabel4 = GUICtrlCreateLabel("(In doubt, clic Show coordinates)", 8, 80, 156, 17)
   $idButton_OK = GUICtrlCreateButton("OK", 9, 108, 59, 25)
   $idButton_Cancel = GUICtrlCreateButton("Cancel", 82, 108, 59, 25)
   $idButton_Mouse_Locator = GUICtrlCreateButton("Show coordinates", 155, 108, 115, 25)
   GUISetState(@SW_SHOW)

   ControlFocus($hGUI, "", $idButton_OK) ; <==========================================

   While 1
     $nMsg = GUIGetMsg()
      Select
         Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $idButton_Cancel
               MsgBox($MB_SYSTEMMODAL, "Escape/Close/Cancel", "    Parameters unchanged      ")
               ExitLoop ; from While 1

         Case $nMsg = $idButton_OK
            $idBad_Control = Check_Input($idInput, $idMouse_Coord_H, $idMouse_Coord_V)
            If $idBad_Control = 0 Then  ; all controls values checked in Check_Input() are ok
               $vInput_now = $vInput
               $vMouse_Coord_H_now = $vMouse_Coord_H
               $vMouse_Coord_V_now = $vMouse_Coord_V
               MsgBox($MB_SYSTEMMODAL, "Ok", "New parameters active")
               ExitLoop ; from While 1
            Else
               MsgBox(BitOr($MB_SYSTEMMODAL, $MB_ICONERROR), _
                  $sError_Title, _
                  $sError_Msg)
               ControlFocus($hGUI, "", $idBad_Control)
            EndIf

         Case $nMsg = $idButton_Mouse_Locator
            ControlHide($hGUI, "", $idLabel4)
            ControlHide($hGUI, "", $idButton_OK)
            ControlHide($hGUI, "", $idButton_Cancel)
            _GUICtrlButton_SetText($idButton_Mouse_Locator, "Hide coordinates")
            Local $aMgp = 0 ; for ToolTip (show mouse coordinates in a wandering tooltip)
            Local $sToolTip_Title = "lasting clic updates coords." ; dynamic title !
            Local $hDll = DllOpen("user32.dll") ; so _IsPressed() wont open it many times
            While 2 ; for fun !
               $nMsg2 = GUIGetMsg()
               Select
                  Case $nMsg2 = $GUI_EVENT_CLOSE    ; Esc acts as Close Button (without Opt)
                     $sToolTip_Title = ">>>>> Hide coordinates first <<<<<"

                  Case $nMsg2 = $idButton_Mouse_Locator
                     ToolTip("")
                     ControlShow($hGUI, "", $idLabel4)
                     ControlShow($hGUI, "", $idButton_OK)
                     ControlShow($hGUI, "", $idButton_Cancel)
                     _GUICtrlButton_SetText($idButton_Mouse_Locator, "Show coordinates")
                     ControlFocus($hGUI, "", $idButton_Mouse_Locator)
                     ExitLoop ; from While 2

                  Case Else
                     ; Assign a variable the coords of the cursor (array)
                     $aMgp = MouseGetPos()
                     ; Display a tooltip near the cursor with its coords
                     ToolTip("H: " & $aMgp[0] & ", V: " & $aMgp[1], $aMgp[0] +10, $aMgp[1]+10, _
                        $sToolTip_Title, 1, 1+4) ; 1+4 = balloon tip + tooltip always visible
                     If _IsPressed("01", $hDll) Then    ; "01" = left clic
                        $hTimer2 = TimerInit()          ; Begin the timer and keep its handle
                        While _IsPressed("01", $hDll)
                           Sleep(10)
                        Wend
                        $fDiff2 = TimerDiff($hTimer2)   ; Find the difference in time
                        ; MsgBox($MB_SYSTEMMODAL, "Time Difference", $fDiff2)
                        If $fDiff2 > 250 Then           ; 1/4s
                           GUICtrlSetData($idMouse_Coord_H, $aMgp[0])
                           GUICtrlSetData($idMouse_Coord_V, $aMgp[1])
                        EndIf
                     EndIf
               EndSelect
            WEnd
            DllClose($hDll)     ; close user32.dll, opened for _IsPressed()
      EndSelect
   WEnd

   GUIDelete($hGui)
EndFunc     ; ==> F10

; ===================================================================================
Func Check_Input($idInput, $idMouse_Coord_H, $idMouse_Coord_V)
   $vInput = GUICtrlRead($idInput)
   If $vInput < 1 or $vInput > 3 Then
      $sError_Title = "What kind of display ?"
      $sError_Msg = "Choose 1, 2 or 3"
      Return $idInput
   EndIf

   $vMouse_Coord_H = GUICtrlRead($idMouse_Coord_H)
   If $vMouse_Coord_H > @DesktopWidth Then
      $sError_Title = "Mouse Horizontal coord."
      $sError_Msg = "Choose between 0 and " & @DesktopWidth
      Return $idMouse_Coord_H
   EndIf

   $vMouse_Coord_V = GUICtrlRead($idMouse_Coord_V)
   If $vMouse_Coord_V > @DesktopHeight Then
      $sError_Title = "Mouse Vertical coord."
      $sError_Msg = "Choose between 0 and " & @DesktopHeight
      Return $idMouse_Coord_V
   EndIf

   ; implicit Return 0 , i.e. all controls values checked in Check_Input() are ok
EndFunc     ; ==> Check_Input

 

"I think you are searching a bug where there is no bug... don't listen to bad advice."

  • Developers
Posted

Have you tried using the style $BS_DEFPUSHBUTTON for the button that should be the default for Enter?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted (edited)

Brilliant & perfect, now it works like a charm
Many thanks Jos "je maintiendrai till the end of time" :)

So I deleted the line :
ControlFocus($hGUI, "", $idButton_OK)

And modified the line creating the ok button, adding the style you indicated me :
$idButton_OK = GUICtrlCreateButton("OK", 9, 108, 59, 25, $BS_DEFPUSHBUTTON)

For the record, I tried several things in this partial script :
* Use Koda for the 1st time (I liked it, great help especially for coords.)

* Have a single function Check_Input() to validate all input controls of a GUI, with only 1 MsgBox() for any error.

* Use same variable names in these 2 lines :
$idBad_Control = Check_Input($idInput, $idMouse_Coord_H, $idMouse_Coord_V)
Func Check_Input($idInput, $idMouse_Coord_H, $idMouse_Coord_V)

I mean... if in this script, you won't have issues keeping the same variable names in the calling function and in the Func statement, why choosing another set of variable names in Func() that makes it less readable if you can avoid it ?

* Also the "Show coordinates" button was fun to program, clic on it and see where it goes : a GUIGetMsg() within the "same" GUIGetMsg()

* And the GUI isn't minimizable (while keeping the same height as if it was, which doesn't seem feasible with Koda)

Thanks Jos, we're lucky to have you with us.

Edited by pixelsearch

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted

Thanks Francesco for your appreciation :)
By the way, I got another general question, related to this kind of GUI, when they are 1 "Yes" button + 1 "No" button in a GUI, because then, the "red cross" in the upper right corner of the GUI isn't really useful and maybe confusing.

For example, see how MsgBox() reacts when there are only 2 buttons "Yes" and "No" :

#include <MsgBoxConstants.au3>
MsgBox($MB_YESNO, "2 buttons Yes/No", "You can't close with the red cross")

See ? It is programmed without the red cross showing, so you have to push "Yes" or "No", there isn't another problematic alternative. I notice that all programs don't react the same, concerning this subject. After you clic the red cross, some programs will discard the changes you did in the GUI, without warning you, only because you didn't validate the GUI with the "Yes" button, your "fault" !
Other programs will warn you etc...

Imho, the red cross should never validate a GUI. For example see how MsgBox() was nicely programmed when 3 buttons : Yes/No/Cancel

#include <MsgBoxConstants.au3>
$ichoice = MsgBox($MB_YESNOCANCEL, "3 buttons Yes/No/Cancel", _
   "You can close this message with the red cross")

Now the red cross appears again... because there is also a "Cancel" button : the return value (2) is the same, no matter you click the red cross or push "Cancel" or "Esc"

So my question is : is it easy to create a GUI without the red cross ?
This should be useful when there are only 2 buttons to press : "Yes" or "No"

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (edited)

Thanks Zedna, gonna read it carefully :)

Edited: I just read a couple of AutoIt forum pages concerning this matter ("how to create a GUI without the red cross") and discovered this link, where Melba23 brought a short and terrific solution, very impressive :

 

Edited by pixelsearch

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (edited)

Hi all,
I thought it would be great to write a script showing the syntax of the 8 possible combinations of the 3 GUI buttons (minimize / maximize / red cross) while creating the GUI. This was feasible only after I read Melba23's comment (link in precedent post) and it could be helpful for the AutoIt community, especially the newbies :)

Here's the script, followed by an image of the display when run :

#include <GUIMenu.au3> ; for GUI's 5 to 8
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

; Create GUI's 1 to 4 with red X enabled (1 means button enabled, 0 disabled)

$hGUI1= GUICreate("GUI 1", 175, 100, 50, 100) ; default style = minimize 1, Maximize 0, red X 1
GUISetState()

$hGUI2= GUICreate("GUI 2", 175, 100, 250, 100, _
   BitOr(BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_MAXIMIZEBOX)) ; mini 0, Maxi 1, red X 1
GUISetState()

$hGUI3= GUICreate("GUI 3", 175, 100, 450, 100, _
   BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX)) ; mini 1, Maxi 1, red X 1
GUISetState()

$hGUI4= GUICreate("GUI 4", 175, 100, 650, 100, _
   BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX)) ; mini 0, Maxi 0, red X 1
GUISetState()

; Create GUI's 5 to 8 with red X grayed (thanks Melba23)

$hGUI5= GUICreate("GUI5 ", 175, 100, 50, 250) ; default style = minimize 1, Maximize 0, red X 1
$hMenu5 = _GUICtrlMenu_GetSystemMenu($hGUI5)
_GUICtrlMenu_EnableMenuItem($hMenu5, $SC_CLOSE, $MF_GRAYED, False) ; red X 0
GUISetState()

$hGUI6= GUICreate("GUI 6", 175, 100, 250, 250, _
   BitOr(BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_MAXIMIZEBOX)) ; mini 0, Maxi 1, red X 1
$hMenu6 = _GUICtrlMenu_GetSystemMenu($hGUI6)
_GUICtrlMenu_EnableMenuItem($hMenu6, $SC_CLOSE, $MF_GRAYED, False) ; red X 0
GUISetState()

$hGUI7= GUICreate("GUI 7", 175, 100, 450, 250, _
   BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX)) ; mini 1, Maxi 1, red X 1
$hMenu7 = _GUICtrlMenu_GetSystemMenu($hGUI7)
_GUICtrlMenu_EnableMenuItem($hMenu7, $SC_CLOSE, $MF_GRAYED, False) ; red X 0
GUISetState()

$hGUI8= GUICreate("GUI 8", 175, 100, 650, 250, _
   BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX)) ; mini 0, Maxi 0, red X 1
$hMenu8 = _GUICtrlMenu_GetSystemMenu($hGUI8)
_GUICtrlMenu_EnableMenuItem($hMenu8, $SC_CLOSE, $MF_GRAYED, False) ; red X 0
GUISetState()

; What follows just for fun
$bRedX_GUI8 = False ; grayed

While 1
   $iChoice = MsgBox(BitOr($MB_SYSTEMMODAL, $MB_OKCANCEL), "8 buttons combinations", _
      "Status of Red X in GUI 8 is : " & (($bRedX_GUI8 = False) ? "Grayed" : "Enabled") & @CRLF & _
      "Press 'OK' to change its status" & @CRLF & _
      "Press 'Cancel' to Quit") ; 1 = OK, 2 = Cancel
   If $iChoice = 2 Then ; button Cancel
      For $i = 1 to 8
         GUIDelete(Eval("hGUI" & $i)) ; nice way to delete many GUI's in a loop !
      Next
      Exit
   Else                 ; button OK
      If Not $bRedX_GUI8 Then
         _GUICtrlMenu_EnableMenuItem($hMenu8, $SC_CLOSE, $MF_ENABLED, False) ; red X 1
      Else
         _GUICtrlMenu_EnableMenuItem($hMenu8, $SC_CLOSE, $MF_GRAYED, False) ; red X 0
      EndIf
      GUISetState(@SW_SHOW, $hGUI8)
      $bRedX_GUI8 = Not $bRedX_GUI8
   EndIf
Wend

; ====== syntax to change style (min & Max) after GUI creation =======
; GUISetStyle($GUI_SS_DEFAULT_GUI, -1, $hGUI1) ; default style = minimize 1, Maximize 0, red X 1
; GUISetStyle(BitOr(BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_MAXIMIZEBOX), -1, $hGUI1) ; mini 0, Maxi 1, red X 1
; GUISetStyle(BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX), -1, $hGUI1) ; mini 1, Maxi 1, red X 1
; GUISetStyle(BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), -1, $hGUI1) ; mini 0, Maxi 0, red X 1
; ====================================================================

5bb29830cd309_8buttonscombinations.jpg.424b4cd2023eb53b8540865a0050e300.jpg

Edited by pixelsearch
added help syntax for style change (min & Max)

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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...