Sign in to follow this  
Followers 0
Mat

GUIGetCur

11 posts in this topic

#1 ·  Posted (edited)

here is a better version using a suggestion from yashied using WinApi_GetParent.


I've been trying to do this for ages as using GUISwitch never seemed to work perfectly... Anyhow, Turns out I need to find all the windows under the Autoit PID and then use those... Here it is then :)

Func _GUIGetCur ($bFindHidden = False)
   If Not IsBool ($bFindHidden) Then Return SetError (1, 0, -1)

   Local $alist = WinList (), $aRet[1] = [0]

   ; List windows under @AutoitPID
   For $i = 1 to $alist[0][0]
      If $alist[$i][0] <> "" then
         If Not $bFindHidden And Not (BitAND (WinGetState ($aList[$i][0]), 2)) Then ContinueLoop
         If WinGetProcess ($alist[$i][0]) = @AutoitPID Then
            Redim $aRet[UBound ($aRet) + 1]
            $aRet[UBound ($aRet) - 1] =  $alist[$i][0]
            $aRet[0] += 1
         EndIf
      EndIf
   Next

   ; Check Number of GUI's found
   If $aRet[0] = 0 Then Return SetError (1, 0, -1) ; No windows
   If $aRet[0] = 1 Then Return WinGetHandle ($aRet[1])

   ; More than 1 GUI used, check for current!
   Local $ret = GUISwitch (WinGetHandle ($aRet[1]))
   GUISwitch ($ret)
   Return $ret
EndFunc ; ==> _GUIGetCur

Mat

Edit: Solved problem with returning a window title when only one window exists...

Edited by Mat

Share this post


Link to post
Share on other sites



I've been trying to do this for ages as using GUISwitch never seemed to work perfectly... Anyhow, Turns out I need to find all the windows under the Autoit PID and then use those... Here it is then :)

Hi Mat,

Looks like well thought out little function to me :)

Not to nitpick or anything but shouldn't this be renamed to something along the lines of "GUISetAndGetCur()" though?!

Cheers,

Share this post


Link to post
Share on other sites

No... It does reset it back to the original, so no switching actually occurs (Well... It does but its undone by the end of the function, You won't notice the difference)

Mat

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I made it for use with UDF's that use custom GUI's etc, so that it returns to the GUI that you want. that prbably makes no sense, so heres an example:

Global $hMyGUI, $hUDFGUI

; Example 1 - Not using any switch

$hMyGUI = GUICreate ("This is the user created GUI...", 400, 100, 0, 0)
   GUICtrlCreateLabel ("This is a user created label, at start!", 2, 2, 200, 20)
   GUISetState ()

_UDF1 ()

   GUICtrlCreateLabel ("This is a user created label, after UDF", 2, 24, 200, 20)

While GUIGetMsg () <> -3
   Sleep (10)
WEnd
GUIDelete ($hMyGUI)
GUIDelete ($hUDFGUI)

Func _UDF1 ()
   $hUDFGUI = GUICreate ("This is a UDF's GUI", 400, 100, 0, 150)
      GUICtrlCreateLabel ("This is a UDF Label, at start!", 2, 2, 200, 20)
      GUISetState ()
EndFunc ; ==> _UDF1

; Example 2 - using switch

$hMyGUI = GUICreate ("This is the user created GUI...", 400, 100, 0, 0)
   GUICtrlCreateLabel ("This is a user created label, at start!", 2, 2, 200, 20)
   GUISetState ()

_UDF2 ()

   GUICtrlCreateLabel ("This is a user created label, after UDF", 2, 24, 200, 20)

While GUIGetMsg () <> -3
   Sleep (10)
WEnd
GUIDelete ($hMyGUI)
GUIDelete ($hUDFGUI)

Func _UDF2 ()
   $hCur = _GUIGetCur ()
   $hUDFGUI = GUICreate ("This is a UDF's GUI", 400, 100, 0, 150)
      GUICtrlCreateLabel ("This is a UDF Label, at start!", 2, 2, 200, 20)
      GUISetState ()
      GUISwitch ($hCur) ; <<<< Switch back to original!
EndFunc ; ==> _UDF2

Func _GUIGetCur ($bFindHidden = False)
   If Not IsBool ($bFindHidden) Then Return SetError (1, 0, -1)

   Local $alist = WinList (), $aRet[1] = [0]

   ; List windows under @AutoitPID
   For $i = 1 to $alist[0][0]
      If $alist[$i][0] <> "" then
         If Not $bFindHidden And Not (BitAND (WinGetState ($aList[$i][0]), 2)) Then ContinueLoop
         If WinGetProcess ($alist[$i][0]) = @AutoitPID Then
            Redim $aRet[UBound ($aRet) + 1]
            $aRet[UBound ($aRet) - 1] =  $alist[$i][0]
            $aRet[0] += 1
         EndIf
      EndIf
   Next

   ; Check Number of GUI's found
   If $aRet[0] = 0 Then Return SetError (1, 0, -1) ; No windows
   If $aRet[0] = 1 Then Return WinGetHandle ($aRet[1])

   ; More than 1 GUI used, check for current!
   Local $ret = GUISwitch (WinGetHandle ($aRet[1]))
   GUISwitch ($ret)
   Return $ret
EndFunc ; ==> _GUIGetCur

Hope that shows it clearly... Its not needed when the GUI is deleted at the end of the UDF, but for some they need the GUI to stay shown.

Mat

Edit: @Sunaj :), There would be no point in a switch, as that would be reinventing the wheel, but square!

Edited by Mat

Share this post


Link to post
Share on other sites

Yes they could, but they shouldn't have to. If you look at a real example like my PopupMenu UDF, that creates a GUI in the background permanently, and to anyone who has not read the source code, that GUI does not exist... I haven't implemented this in there yet, but I will be soon. (incidentally, both of you replied in that thread too :), I am still working on an easy way to integrate Hotkey.au3 in there Yashied.

Mat

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

OK

Maybe this will be easier.

#Include <WinAPI.au3>

$hGUI1 = GUICreate('')
$hGUI2 = GUICreate('')

ConsoleWrite('Gui1: ' & $hGUI1 & @CR)
ConsoleWrite('Gui2: ' & $hGUI2 & @CR)

$Label = GUICtrlCreateLabel('', 0, 0, 0, 0)
$hWnd = _WinAPI_GetParent(GUICtrlGetHandle($Label))
GUICtrlDelete($Label)

ConsoleWrite('Current: ' & $hWnd & @CR)
Edited by Yashied

Share this post


Link to post
Share on other sites

That would be a lot easier Yashied... I didn't realise you could do that!!

I will have a look at how that works.

Thanks

Mat

Share this post


Link to post
Share on other sites

kk, I had a look in WinApi.au3 and its a simple DllCall, so to save on the include I've got this:

Func _GUIGetCur ()
   Local $hLabel = GUICtrlCreateLabel ("", -99, -99, 1, 1), $aRet

   $aRet = DllCall ("User32.dll", "hwnd", "GetParent", "hwnd", GUICtrlGetHandle ($hLabel))
    If @Error Then Return SetError(@Error, 0 * GUICtrlDelete ($hLabel), 0)
   GUICtrlDelete ($hLabel)

    Return $aRet[0]
EndFunc ; ==> _GUIGetCur

Thats a lot nicer Yashied, with no loops!

Mat

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