Jump to content

GUI button won't respond to mouse clicks


Recommended Posts

Here's an oddity I can't find a solution to - and the function description says it all. Essentially, the button works with the keyboard but not with the mouse. Odd...

$handle = _ToggleFormWithStop(0,"This is a test message")
Exit

;****************************************************************
;~ Name:        _ToggleFormWithStop()
;~ Description: Toggles a small form with a stop button on and off
;~ Parameters:  $handle - used to pass GUI handle back
;~          to the form when deleting or hiding it
;~          $msg - Message text to be displayed
;~          $sTitle - Title text for the window
;~ Usage:       Send a zero handle and text when initiating the form
;~          and just the handle to hide it
;~ Returns: Handle to the form window
;~ Author:      Manek Dubash (c) 2009
;~ Notes:       The STOP button works with the keyboard (eg space bar)
;~          but NOT when you click it with the mouse. Weird...
;****************************************************************
Func _ToggleFormWithStop($handle = 0, $sMessage = "", $sTitle = "")
    Local $nLongestString = StringLen($sMessage), $nCount = 0, $nMsg, $StopButton
; analyse message string:-
; find how many lines by counting carrage returns for $boxheight
; find longest string between @CRs for $boxwidth
    For $n = 1 To StringLen($sMessage)
        If StringMid($sMessage, $n, 1) = @CR Or StringMid($sMessage, $n, 1) = @CRLF Then
            $nCount += 1; count the carriage returns
            If $nLongestString < $n - $nLongestString Then $nLongestString = $n - $nLongestString; find longest string
        EndIf
    Next
    Local $boxwidth = _Min($nLongestString * 5.5, @DesktopWidth - 100), $boxheight = 85 + (15 * $nCount)
    If $handle = 0 Then; this is the first time the function's been called so fire up the text box
        $handle = GUICreate($sTitle, $boxwidth, $boxheight, @DesktopWidth / 12, @DesktopHeight / 12, $WS_CAPTION, $WS_EX_TOPMOST)
        GUICtrlCreateLabel($sMessage, 15, 16, $boxwidth - 10, $boxheight - 25)
        $StopButton = GUICtrlCreateButton("Stop", $boxwidth / 2 - 37, $boxheight - 37, 75)
;~      _GUICtrlButton_SetFocus($StopButton, True); not required
        GUISetState()
        While GUIGetMsg() <> $StopButton; keep looping until the button is pressed
            Sleep(50); don't hog the CPU
        WEnd
        GUIDelete(); kill the GUI
        Return 1; NB: also a True value
    Else; if function called with previously returned handle as parameter, kill the GUI
        GUIDelete($handle)
    EndIf
    Return $handle
EndFunc ;==>_ToggleFormWithStop

Thanks for any input as to how I could resolve this.

Edited by liteswap
Link to comment
Share on other sites

idk.. is your label covering up the button? i cannot test it Right now as i do not have scite on this computer

Link to comment
Share on other sites

idk.. is your label covering up the button? i cannot test it Right now as i do not have scite on this computer

No, the button is completely visible, and has focus: hitting the space bar activates it - but clicking it with the mouse results in zero response. Weird, no?
Link to comment
Share on other sites

Link to comment
Share on other sites

It would be nice if you provide ready to run a script, not just function.

Add GUICtrlSetBkColor(-1, 0xAAAAAA) after GUICtrlCreateLabel() and you own everything you see.

OK duly edited (you needed only to call the function....) to run as is.
Link to comment
Share on other sites

OK duly edited (you needed only to call the function....) to run as is.

...and fixed. Thank you for the hint with the label colour. A blank section of the label was covering the button - I hadn't checked that it was too big.

@CodyBarrett - apologies - you were spot on.

Link to comment
Share on other sites

Link to comment
Share on other sites

... and add follows.

#Include <Math.au3>
#Include <WindowsConstants.au3>

In this case, people may find it easier and faster to help you.

I agree, from the code above I saw this....

>09:06:48 Starting AutoIt3Wrapper v.1.10.1.14 Environment(Language:0409 Keyboard:00000409 OS:WIN_XP/Service Pack 3 CPU:X86 ANSI)

>Running AU3Check (1.54.14.0) from:C:\Program Files\AutoIt3

C:\Program Files\AutoIt3\Examples\My Stuff\Test.au3(31,113) : WARNING: $WS_CAPTION: possibly used before declaration.

$handle = GUICreate($sTitle, $boxwidth, $boxheight, @DesktopWidth / 12, @DesktopHeight / 12, $WS_CAPTION,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\Test.au3(31,129) : WARNING: $WS_EX_TOPMOST: possibly used before declaration.

$handle = GUICreate($sTitle, $boxwidth, $boxheight, @DesktopWidth / 12, @DesktopHeight / 12, $WS_CAPTION, $WS_EX_TOPMOST)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\Test.au3(31,113) : ERROR: $WS_CAPTION: undeclared global variable.

$handle = GUICreate($sTitle, $boxwidth, $boxheight, @DesktopWidth / 12, @DesktopHeight / 12, $WS_CAPTION,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\Test.au3(29,70) : ERROR: _Min(): undefined function.

Local $boxwidth = _Min($nLongestString * 5.5, @DesktopWidth - 100)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\Test.au3 - 2 error(s), 2 warning(s)

!>09:06:48 AU3Check ended.rc:2

+>09:06:53 AutoIt3Wrapper Finished

>Exit code: 0 Time: 5.622

And I decided NOT to help

8)

NEWHeader1.png

Link to comment
Share on other sites

Indeed, it's the overlapping label control... ;]

Change it to not overlap or call it after creating the button control:

$StopButton = GUICtrlCreateButton("Stop", $boxwidth / 2 - 37, $boxheight - 37, 75)
GUICtrlCreateLabel($sMessage, 15, 16, $boxwidth - 10, $boxheight - 25)

Edit: Sigh, ugly way...

Just change it to not overlap. By the way, you can use GetTextExtentPoint32 to calculate the width and height of the string according to the selected font of the specified HDC.

Edited by Authenticity
Link to comment
Share on other sites

...and fixed. Thank you for the hint with the label colour. A blank section of the label was covering the button - I hadn't checked that it was too big.

@CodyBarrett - apologies - you were spot on.

TIP: When Creating Labels Use a background color for the label (or any static control) until you are sure that you have it positioned properly

$Frm_main = GUICreate("My Test Form")
$My_Label = GUICtrlCreateLabel("My Label", 10, 30, 120, 50)
GUICtrlSetBkColor(-1, 0xFFFF00)
$Btn = GUICtrlCreateButton("My Button", 120, 30, 60, 25)
GUISetState()
While 1
     $Msg = GUIGetMsg()
     Switch $Msg
          Case -3, $Btn
                Exit
          Case Else
     EndSwitch
Wend

Also don't forget that when you set sizes by character or line count you will be wrong if the user has large fonts set. Make sure you specify the font size and face. Authenticitys idea is better.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

One more thing. Here are a couple of functions you might like if you insist on setting control sizes by the text character and line counts. I have not made headers for them but they are pretty easy to figure out if you just follow the example.

;$sFile = ClipGet()
$sFile = @ScriptFullPath
$Count = _String_CountLines($sFile)
MsgBox(0, "Line Count", $Count & " Lines")
$Longest = _String_LongestLine($sFile)
    If NOT @Error Then
        If IsArray($Longest) Then
            MsgBox(0, "Longest String", "Character Count = " & $Longest[0] & @CRLF & @CRLF & $Longest[1])
        Else
            MsgBox(0, "Longest String", $Longest)
        EndIf
    EndIf


;;  If $iArray not == -1 or default then an array is returned where element 0 is the count and element 1 is the string
;;  If $iCount == -1 or default then the count is returned else the string is returned (only If an array is not being returned)
Func _String_LongestLine($sText, $iCount = -1, $iArray = -1)
   If FileExists($sText) Then $sText = FileRead($sText)
   If NOT $sText Then Return SetError(1,1, "Unable to read the text")
   $aText = StringRegExp($sText, "(.*?)(?:\v|\z)", 3)
   If @Error Then Return SetError(1,2, "RegExpression Error")
   Local $iLongest = StringLen($aText[0]), $sRtn = $aText[0]
   For $i = 1 To Ubound($aText) -1
      If StringLen($aText[$i]) > $iLongest Then
         $iLongest = StringLen($aText[$i])
         $sRtn = $aText[$i]
      EndIf
   Next
   If NOT StringRegExp($iArray, "(?i)-1|default") Then
       Local $aRtn[2]
       $aRtn[0] = $iLongest
       $aRtn[1] = $sRtn
      Return $aRtn
   EndIf
   If StringRegExp($iCount, "(?i)-1|default") Then Return $iLongest
   Return $sRtn
EndFunc

Func _String_CountLines($sText)
   If FileExists($sText) Then $sText = FileRead($sText)
   StringRegExpReplace($sText, "(?m:^).*\v|\z", "$1")
   Return @Extended
EndFunc
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Looks like you called it from the start Cody!!

8)

lmao all i did was think of a common problem that happens to me lol

Edited by CodyBarrett
Link to comment
Share on other sites

lmao all i did was think of a common problem that happens to me lol

Thanks Cody - I was hoping that someone had made the same booboo! And thanks too to Geosoft and Authenticity for the far more elegant way of doing what I was trying to do.

And this was my first posting of problem code - so I hold up my hands: next time there'll be a full working example. OK? ^_^

Edited by liteswap
Link to comment
Share on other sites

You're most welcome. I posted a more comprehensive version of the function in this post

#677075

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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