Jump to content

Recommended Posts

here I am writing simple gui with text processing capabilities.

i have main gui, background picture, all buttons with labels over them 2 static buttons (1 present here)

Opt("GUIOnEventMode", 1) and includes

$main = GUICreate("Title", 961, 721); Main Window
$bgpic = GUICtrlCreatePic(@WorkingDir & "\960x720.jpg", 0, 0, 960, 720); use background picture

$quitBtn = GUICtrlCreateButton("Quit", 885, 685, 95, 40)
GUICtrlSetOnEvent($quitBtn, "_exit"); assign quit button to function "exit"
_GuiCtrlMakeTrans(-1, 1)
$quitLabel = GUICtrlCreateLabel("Quit", 885, 685, 95, 40, BitOR($SS_CENTER, $BS_BOTTOM))
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetColor(-1, 0x962129)

GUISetState(@SW_SHOW)

then i have the code for creating buttons dynamically from information in file

Dim $Button[$NumberFromText]
    $startX = 48
    $startY = 24
    $fromLeft = 240
    $bHeigh = 160
    $bWidth = 50

    Dim $1to4btn[0]

    For $x = 0 To _Min(UBound($Button) - 1, 3) ; creates maximum 4 button, label

        $Button = GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth); create buttons
        _GuiCtrlMakeTrans(-1, 1) ; transparency 255 Solid, 0 Transparent
        $serverLabel = GUICtrlCreateLabel(FileReadLine($configF, $SN), _ ; read 1st line and 5 below on each iteration
        $startX, $startY, $bHeigh, $bWidth, BitOR($SS_CENTER, $SS_CENTERIMAGE, $WS_EX_TRANSPARENT));
        GUICtrlSetFont(-1, 14, 400, 0, "Tahoma"); set font and size
        GUICtrlSetColor($serverLabel, 0x04A111); set color of font
        GUICtrlSetBkColor($serverLabel, $GUI_BKCOLOR_TRANSPARENT); make label Background transparent

        $SN += 5; read 5 lines below
        $startX = $startX + $fromLeft

        $btnArray=_ArrayAdd($1to4btn, GUICtrlGetHandle($Button))

    Next

since buttons are created under the label $Button and I couldn't conacenate $Button[$i] , I created array and place handles into it and connected static buttons with

GUICtrlSetOnEvent($cancelBtn,"cancelFunc") and it worked

now i tried using handles to assign functions to buttons but it fails with GUICtrlSetOnEvent()

also tried GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "funcForFirstButton", $1to4btn[0])

if I assign without handle it works by clicking anywhere on MainGUI

While loop includes only sleep(10)

 

Window starts as inactive and return code 7

If i click dynamically created buttons, window loses focus . clicking anywhere else focuses window

 

this is _GuiCtrlMakeTrans function (found here on forums some time ago)

Spoiler

Func _GuiCtrlMakeTrans($iCtrlID, $iTrans = 255)
    Local $pHwnd, $nHwnd, $aPos, $a

    $hWnd = GUICtrlGetHandle($iCtrlID);Get the control handle
    If $hWnd = 0 Then Return SetError(1, 1, 0)
    $pHwnd = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd);Get the parent Gui Handle
    If $pHwnd[0] = 0 Then Return SetError(1, 2, 0)
    $aPos = ControlGetPos($pHwnd[0], "", $hWnd);Get the current pos of the control
    If @error Then Return SetError(1, 3, 0)
    $nHwnd = GUICreate("", $aPos[2], $aPos[3], $aPos[0], $aPos[1], 0x80000000, 0x00080000 + 0x00000040, $pHwnd[0]);greate a gui in the position of the control
    If $nHwnd = 0 Then Return SetError(1, 4, 0)
    $a = DllCall("User32.dll", "hwnd", "SetParent", "hwnd", $hWnd, "hwnd", $nHwnd);change the parent of the control to the new gui
    If $a[0] = 0 Then Return SetError(1, 5, 0)
    If Not ControlMove($nHwnd, '', $hWnd, 0, 0) Then Return SetError(1, 6, -1);Move the control to 0,0 of the newly created child gui
    GUISetState(@SW_SHOW, $nHwnd);show the new child gui
    WinSetTrans($nHwnd, "", $iTrans);set the transparency
    If @error Then Return SetError(1, 7, 0)
    GUISwitch($pHwnd[0]);switch back to the parent Gui

    Return $nHwnd;Return the handle for the new Child gui
EndFunc   ;==>_GuiCtrlMakeTrans

also created simple button on the same mainGUI and tried but didn't work

$try=GUICtrlCreateButton("TRY",250,150,100,35)
GUICtrlSetOnEvent(-1,"FunctionOne") ; shows messagebox and write console

 

Edited by shotiko
added undocumented UDF
Link to comment
Share on other sites

I believe GUICtrlSetOnEvent is expecting an AutoIt ControlID, not a handle.

Your last example looks fine to me.  I'd suspect there's something elsewhere in your code breaking it if that doesn't work.

Can you show more code or post a "complete" script that reproduces the issue when executed?

Link to comment
Share on other sites

problem with state 7 (no focus) lies in _GUICtrlMakeTrans function. which i couldn't fix.

i've also found workaround with buttons

Instead of

$Button = GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth); create buttons
$btnArray=_ArrayAdd($1to4btn, GUICtrlGetHandle($Button))

i use

Assign("Button", GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth)); create buttons
_ArrayAdd($cButtons, Eval("Button"), 1) ; this adds Button controlIDs to array

and i decided to switch back to GUIGetMsg

Edited by shotiko
Link to comment
Share on other sites

now new problem arises with Eval("Button"): Now i have

Assign("Button", GUICtrlCreateButton($x, $startX, $startY, $bHeigh, $bWidth)); create buttons
_ArrayAdd($cButtons, Eval("Button"), 1) ; this adds Button controlIDs to array

While 1
    Switch GUIGetMsg()
        Case $cButtons[0] ; Server 1
             _somefunction()
        Case $cButtons[1] ; Server 2
            _somefunction()
            ConsoleWrite('s2 click' & @CRLF)
        Case $cButtons[2] ; Server 3
            _somefunction()
            ConsoleWrite('s3 click' & @CRLF)
        Case $cButtons[3] ; Server 4
            _somefunction()
            ConsoleWrite('s4 click' & @CRLF)
WEnd

problem is that if i don't have all maximum buttons created then Case $cButtons[1] and the others return error: subscript

so what's the workaround here since i can't nest switch and if

Edited by shotiko
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...